Question: Consider the partial program below which includes a main ( ) and threaded runner function that use a semaphore. This program contains a conceptual problem
Consider the partial program below which includes a main and threaded runner function that use a semaphore. This program contains a conceptual problem with the way it uses the semaphore, what is it Identify the problem, and explain why it occurs. Assume all syntax is correct.
Useful Functions:
int seminitsemt sem int pshared,
unsigned int count;
int semwaitsemt sem;
int sempostsemt sem;
#define NUMACTIVE number of threads allowed in critical section at once
#define NUMTHREADS number of threads total
semt sem,
void threadjobvoid
if semwait&sem
fprintfstderr "Failed to lock!
;
else if semwait&sem
do some noncritical stuff
critical section goes here
sempost&sem;
more noncritical stuff
int mainvoid
pthreadt tidsNUMTHREADS;
seminit&sem, NUMACTIVE; initialize semaphore
for int i; i NUMTHREADS; i
pthreadcreate&tidsi NULL, thread job, NULL;
For int i #l O; i L NUM THREADS; i
pthreadjointidsi NULL;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
