Statistical Computations using Multithreading
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
| Function | Purpose |
|---|---|
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:
are updated by worker threads and printed by the main thread.
Statistical Definitions
-
Mean (Average)
-
Median
-
Middle value in a sorted list
-
If even number of elements, average of the two middle values
-
-
Standard Deviation
3. ALGORITHM
Parent Thread
-
Read integers from command line.
-
Store them in a global array.
-
Create three worker threads:
-
Thread 1 → calculate mean
-
Thread 2 → calculate median
-
-
Wait for Thread 1 to finish.
-
Create Thread 3 → calculate standard deviation
-
Wait for all threads to finish using
pthread_join(). -
Print the computed results.
Thread 1 (Mean Calculation)
-
Sum all integers.
-
Compute mean as sum ÷ count.
-
Store result in the global variable
mean.
Thread 2 (Median Calculation)
-
Sort the array.
-
If odd count → median = middle element
-
If even count → median = average of middle two
-
Store result in global variable
median.
Thread 3 (Standard Deviation Calculation)
-
Subtract mean from each number.
-
Square the difference and sum it.
-
Divide by count and take square root.
-
Store result in
stddev.
4. PROGRAM
5. COMPILATION & EXECUTION
Compile
Run
6. SAMPLE OUTPUT
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
Post a Comment