In: Computer Science
If there are 32 concurrent processes, how will you modify the following code?
Process i
do {
while (turn == j);
critical section;
turn = j;
remainder section
} while (true);
There are two methods mostly common to handle the critical section:
method 1- Peterson's solution:
It is a classic based software solution to the critical-section problem. It is restricted to two processes that alternate execution between their critical sections and remainder sections. Peterson’ section requires two data items to be shared between the two processes i.e.
Variable turn indicates whose turn is to enter its critical section.
Flag array indicated whether the process is ready to enter its critical section.
If turn == i,
process Pi is allowed to enter in its critical section.
If flag[j] is TRUE,
process j is ready to enter in its critical section
Modified Code:
do{
flag[i] = TRUE;
turn = j;
while(flag[j] && turn == j)
critical section
flag[i] = FALSE
remainder section
}while(TRUE)
Method 2 - Semaphores:
It is a synchronization tool that is used to overcome the problems generated by the instructions,
TestAndSet()
Swap()
A semaphore S is an integer variable that can be accessed through two standard atomic operations that are
wait()
signal()
wait():
wait(S) { While S <= 0 ; // no operation S--; }
Signal():
signal(S) { S++; }
When one process is modifying the value of semaphore then no other process can simultaneously manipulate the same semaphore value.
Code:
do{
waiting(mutex);
ciritical section
signal(mutex);
reminding section
}while(true)