Question: Implement Peterson s Solution for two processes in C . Each process will repeatedly access a critical section, which updates a shared variable. The program
Implement Petersons Solution for two processes in C Each process will repeatedly access a critical section, which updates a shared variable. The program should ensure that the critical section is accessed by only one process at a time.
Assignment Details:
Shared Variables:
int turn: A shared variable that indicates whose turn it is to enter the critical section.
int flag: An array of two elements where each element indicates whether a process is ready to enter the critical section.
Processes:
You will simulate two processes P and P using two separate threads. The threads will run concurrently, each trying to access the critical section multiple times.
Critical Section:
Both processes will modify a shared variable eg incrementing a counter or printing a message in the critical section.
Petersons Solution Algorithm: Implement the following logic for each process:
Entry section for process Psimilar for P with appropriate changes
flag; P is ready to enter critical section
turn ; Set turn to Ps turn
while flag && turn ; Wait if P is ready and it's Ps turn
Critical section
Modify shared variable eg increment counter or print
Exit section
flag; P is leaving critical section
NonCritical Section: Each process should simulate performing some noncritical work eg a random delay or printing a nonshared variable
Termination: After each process has entered the critical section a predefined number of times eg times each the threads should terminate.
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
