Statistical Computations using Multithreading

Write a multi threaded program that calculates the mean, median, and standard deviation for a list of integers. This program should receive a series of integers on the commandline and will then create three separate worker threads. The first thread will determine the mean value, the second will determine the median and the third will calculate the standard deviation of the integers. The variables representing the mean, median, and standard deviation values will be stored globally. The worker threads will set these values, and the parent thread will output the values once the workers have exited.

MULTITHREADED STATISTICAL COMPUTATION


1. AIM

To write a multithreaded program that:

  • Accepts a list of integers from the command line

  • Creates three worker threads

    • Thread 1 → computes Mean

    • Thread 2 → computes Median

    • Thread 3 → computes Standard Deviation

  • Stores the results in global variables

  • Parent thread waits for all workers and then prints the results


2. THEORY

Multithreading

Multithreading allows multiple tasks to run concurrently within a single process.
In Linux, POSIX threads (pthreads) are used for creating and managing threads.

Key Functions

FunctionPurpose
pthread_create()    Creates a new thread
pthread_join()    Waits for a thread to finish
pthread_exit()    Terminates a thread
pthread_t    Thread identifier

Global Variables for Shared Data

Threads can access global variables without explicit communication.
In this program:

double mean, median, stddev;

are updated by worker threads and printed by the main thread.

Statistical Definitions

  1. Mean (Average)

    Mean=i=1nxin\text{Mean} = \frac{\sum_{i=1}^{n} x_i}{n}
  2. Median

    • Middle value in a sorted list

    • If even number of elements, average of the two middle values

  3. Standard Deviation

    σ=(xiMean)2n\sigma = \sqrt{\frac{\sum (x_i - \text{Mean})^2}{n}}

3. ALGORITHM

Parent Thread

  1. Read integers from command line.

  2. Store them in a global array.

  3. Create three worker threads:

    • Thread 1 → calculate mean

    • Thread 2 → calculate median

  4. Wait for Thread 1 to finish.

  5. Create Thread 3 → calculate standard deviation

  6. Wait for all threads to finish using pthread_join().

  7. Print the computed results.


Thread 1 (Mean Calculation)

  1. Sum all integers.

  2. Compute mean as sum ÷ count.

  3. Store result in the global variable mean.


Thread 2 (Median Calculation)

  1. Sort the array.

  2. If odd count → median = middle element

  3. If even count → median = average of middle two

  4. Store result in global variable median.


Thread 3 (Standard Deviation Calculation)

  1. Subtract mean from each number.

  2. Square the difference and sum it.

  3. Divide by count and take square root.

  4. Store result in stddev.


4. PROGRAM

#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <math.h> // Global variables double mean = 0.0; double median = 0.0; double stddev = 0.0; int *numbers; int count; //---------------------- Mean Thread ----------------------- void *calc_mean(void *arg) { double sum = 0.0; for (int i = 0; i < count; i++) sum += numbers[i]; mean = sum / count; pthread_exit(NULL); } //-------------------- Median Thread ----------------------- int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); } void *calc_median(void *arg) { qsort(numbers, count, sizeof(int), compare); if (count % 2 == 0) median = (numbers[count/2 - 1] + numbers[count/2]) / 2.0; else median = numbers[count/2]; pthread_exit(NULL); } //---------------- Std. Deviation Thread ------------------- void *calc_stddev(void *arg) { double variance_sum = 0.0; for (int i = 0; i < count; i++) variance_sum += pow(numbers[i] - mean, 2); stddev = sqrt(variance_sum / count); pthread_exit(NULL); } //--------------------------- MAIN -------------------------- int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: %s <list of integers>\n", argv[0]); exit(1); } count = argc - 1; numbers = malloc(count * sizeof(int)); for (int i = 1; i < argc; i++) numbers[i-1] = atoi(argv[i]); pthread_t t1, t2, t3; pthread_create(&t1, NULL, calc_mean, NULL); pthread_create(&t2, NULL, calc_median, NULL); pthread_join(t1, NULL); pthread_create(&t3, NULL, calc_stddev, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); printf("\nResults:\n"); printf("Mean = %.2f\n", mean); printf("Median = %.2f\n", median); printf("Standard Deviation = %.2f\n", stddev); free(numbers); return 0; }

5. COMPILATION & EXECUTION

Compile

gcc multithread_stats.c -o stats -lpthread -lm

Run

./stats 10 20 30 40 50

6. SAMPLE OUTPUT

Results: Mean = 30.00 Median = 30.00 Standard Deviation = 14.14

7. RESULT

A multithreaded program was successfully implemented using POSIX threads to compute the mean, median, and standard deviation of a list of integers.
Each computation was performed in a separate thread, and the parent thread printed the consolidated output.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command