Multi threaded code

Multi Threaded Code

Using Threads

  1. POSIX threads

In all the following questions include a short answer and on which man page you found
the information.
(1) What is the naming conventions for pthread function calls?
(2) Enumerate the synchronization mechanisms for POSIX threads?
(3) Why is it a good programming practice to call pthread_cond_waitfrom
within a loop?
(4) Which thread executes after a thread is woken up from a condition variable.
(5) What happens if a thread blocks on a condition variable while holding two
mutexes.
(6) What is a detached thread.

  1. Probabilistic Analysis of Pthreads.

In this assignment you will write two pthreads programs that evaluate some aspects
of the pthreads system. The first and simpler of the two, lets threads write to the same file
using mutexes to avoid race conditions. The main thread creates N (N is an optional command line argument by dafault 100) threads and all of them immediately block on condition variable start_line. After all threads are created, the main thread wakes them up
and then waits until they all terminate with join. The threads after waking up lock the
mutexfilemutand write their PID followed by newline to file pthread_statsand
then release the mutex. After they do this M times each (the second optional command
line argument that defaults to 1000) they join the main thread.
In the second pthread program of the assignment the main program creates N threads
designated as red and then another N threads designated as green. All of them immediately block on condition variable start_line. After all threads are created, the main
thread wakes them up and then waits until the all terminate with join. The threads after
waking up lock the mutexfilemutand write their PID followed by the word red or
green followed by newline to file pthread_stats. The difference in this second version is that all the red threads write first their PID M times releasing and re-acquiring the
mutex, then all the green ones another M times, then the red threads again, and so on,
until they have written L (another optional command line argument that defaults to 10)
times each.
Both programs accept optional arguments. The first one is N, the second one is M
and the third one (applies only to the second program) is L.
To do the analysis write a small program in C, AWK, or python (I strongly recommend AWK but python is also OK) to calculate the mean and standard deviation of the“runs”. A run is the number of consecutive writes by the same thread. 

Solution

#include <stdlib.h>

#include <stdio.h>

#include <math.h>

#include <string.h>

intgetNumLines(FILE *fis)

{

int c;

int lines = 0;

while(!feof(fis))

{

c = fgetc(fis);

if(c == ‘\n’)

{

lines++;

}

}

fseek(fis, 0, SEEK_SET);

return lines;

}

typedefstruct Return { double mean; double stddev;}Return;

Return analysis(char *fileName)

{

FILE *fis = fopen(fileName, “r”);

constint SIZE = getNumLines(fis);

double *vec = (double *) calloc(SIZE,sizeof(double));

intidNew, idOld = -1;

chartmp[1024];

int IDX = 0.0;

vec[0] = 1.0;

while(!feof(fis))

{

fscanf(fis, “%d”, &idNew);

if(idNew == idOld)

vec[IDX]++;

else

{

IDX++;

vec[IDX]=1.0;

}

idOld = idNew;

fscanf(fis, “%s”, tmp);

}

double mean = 0;

for(int i = 0; i < IDX; ++i)

mean += vec[i];

mean = mean/((double) IDX);

doublestddev = 0;

for(int i = 0; i < IDX; ++i)

stddev += powf(vec[i]-mean,2.0);

stddev = sqrtf(stddev/(double)IDX);

free(vec);

fclose(fis);

Return ret = {mean,stddev};

return ret;

}

int main()

{

charfileName[128];

chartmp[64];

{//run over N

FILE *fp = fopen(“NRun.dat”, “w+”);

for(int N = 50; N < 150; N+=10)

{

strcpy(fileName, “ANALYSISMN/pthread_stats”);

sprintf(tmp, “%d”,N);

strcat(fileName,tmp);

strcat(fileName,”_1000.dat”);

Return  ret= analysis(fileName);

fprintf(fp, “%d %f %f\n”,N,ret.mean,ret.stddev);

}

fclose(fp);

}

{//run over M

FILE *fp = fopen(“MRun.dat”, “w+”);

for(int M = 500; M < 1500; M+=100)

{

strcpy(fileName, “ANALYSISMN/pthread_stats100_”);

sprintf(tmp, “%d”,M);

strcat(fileName,tmp);

strcat(fileName,”.dat”);

Return  ret= analysis(fileName);

fprintf(fp, “%d %f %f\n”,M,ret.mean,ret.stddev);

}

fclose(fp);

}

return 0;

}