Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 =
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions