Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started