Question: Question 4 . [ Points 7 ] Write a program in C + + for two threads to share a single resource ( e .
Question Points Write a program in C for two threads to share a single resource eg a
license Use Petersons solution to implement it Each thread independently requests for
the resource, but only one thread at a time can get it Once a thread gets the resource, it
keeps it for a random time, and releases it Then, if another thread is waiting on it it will
flagi true;
turn i;
while flagj && turn j;
lock TRUE;
flagi FALSE;
get it Here, the variables turn, flag and flag are shared variables. Variable turn is set by
either thread. Variable flag is set by thread and flag by thread Let each thread
request and release the resource times, each time holding it for a random time and
releasing it Write the program in any language of your choice provided it allows you to
create threads. Output should look something like this.
Time: :: Thread requested the resource
Time: :: Thread received the resource
Time: :: Thread requested the resource
Time: :: Thread released the resource
Time: :: Thread received the resource
Time: :: Thread requested the resource
Time: :: Thread released the resource
Time: :: Thread received the resource
Time: :: Thread requested the resource
Time: :: Thread released the resource
Time: :: Thread received the resource
Time: :: Thread requested the resource
Time: :: Thread released the resource
Time: :: Thread received the resource
Time: :: Thread requested the resource
Time: :: Thread released the resource
Time: :: Thread received the resource
Time: :: Thread released the resource
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
