Question: I think my code is complete i would like some advise thoguh. #include #include #include #define NUM _ THREADS 2 #define COUNT _ LIMIT 2
I think my code is complete i would like some advise thoguh.
#include
#include
#include
#define NUMTHREADS
#define COUNTLIMIT
typedef struct
int value;
pthreadmutext lock;
Counter;
Counter counter PTHREADMUTEXINITIALIZER;
int threadbonuscount ;
void incrementcounterthreadvoid arg
int i ;
while i COUNTLIMIT
pthreadmutexlock&counter.lock; Entry Section
if countervalue
counter.value ; Critical Section
threadbonuscount;
else
counter.value; Critical Section
pthreadmutexunlock&counter.lock; Exit Section
i;
Remainder Section
printfIm thread I did d updates and I got the bonus for d times, counter d
i threadbonuscount, counter.value;
pthreadexitNULL;
void incrementcounterthreadvoid arg
int i ;
while i COUNTLIMIT
pthreadmutexlock&counter.lock; Entry Section
counter.value; Critical Section
pthreadmutexunlock&counter.lock; Exit Section
i;
Remainder Section
printfIm thread I did d updates, counter d
i counter.value;
pthreadexitNULL;
int main
pthreadt threadsNUMTHREADS;
if pthreadcreate&threads NULL, incrementcounterthread NULL
perrorFailed to create thread;
exitEXITFAILURE;
if pthreadcreate&threads NULL, incrementcounterthread NULL
perrorFailed to create thread;
exitEXITFAILURE;
for int i ; i NUMTHREADS; i
if pthreadjointhreadsi NULL
perrorFailed to join thread";
exitEXITFAILURE;
printfFrom parent, counter d
counter.value;
if pthreadmutexdestroy&counter.lock
perrorFailed to destroy mutex";
exitEXITFAILURE;
pthreadexitNULL;
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
