Question: Problem 8 . ( 6 points ) : Processes vs . threads. This problem tests your understanding of the some of the important differences between

Problem 8.(6 points):
Processes vs. threads. This problem tests your understanding of the some of the important differences
between processes and threads. Consider the following C program:
#include "csapp.h"
int main()
{
/* Global variables */
int cnt;
sem_t mutex;
int i;
pthread_t tid[2];
sem_init(&mutex, 0,1); /* mutex=1*/
/* Helper function */
void *incr (void *vargp)
{
P (&mutex) ;
cnt++;
V(&mutex);
return NULL;
}
/* Processes */
cnt=0;
for
incr (NULL) ;
if fork ()==0
incr (NULL) ;
exit(0);
}
}
for (i=0;i2;i++)
wait(NULL) ;
printf("Procs: , cnt =%d?
, cnt) ;
/* Threads */
cnt=O;
for
incr (NULL);
pthread_create(&tid[i], NULL, incr, NULL);
}
for (i=0;i2;i++)
pthread_join(tid[i], NULL) ;
printf("Threads: cnt =%d?
,cnt;
exit(0);
}
A. What is the output of this program?
Procs: ,cnt=
Threads: c nt =
 Problem 8.(6 points): Processes vs. threads. This problem tests your understanding

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!