Lab 6
Lab 6
Tutorial 6
Process 1 Process 2
flag[1] = false; flag[2] = false;
... ...
1. Identify the critical section, entry section, and exit section in the program code.
2. Discuss if this solution satisfies the essential criteria for a proper critical section solution.
It does not even satisfy mutual exclusion. The following scheduling pattern for the processes will end up with
both into the critical section. One example is sufficient to falsify the condition:
Process 1 is scheduled to run
Process 1 arrives and reach the Entry selection
Process 1 found flag[2] is false and break from the while loop
Process 1 is pre-empted
Process 2 runs and arrives and reach the Entry selection
Process 2 found flag[1]is false and break from the while loop
Process 2 set flag[2] to true and moves into the critical selection
Process 2 is pre-empted
Process 1 set flag[1] to true and moves into the critical selection
Stage 3: The Paterson's Solution
In the lecture we showed the Paterson's solution to satisfy the three conditions to a critical section solution.
The following shows the version given in the lecture.
Process 1 Process 2
/* PROCESS 1 */ /* PROCESS 2 */
while (true) { while (true) {
while (flag[2] == true && turn == 2) while (flag[1] == true && turn == 1)
; /* busy wait */ ; /* busy wait */
In each of the following situations, state what is stored in each of the variables flag[1], flag[2], and turn.
Only Process 1 arrived and reached the while loop, and Process 2 never arrived.
If process 2 never arrives , then flag[2] is False. When Process 1 reached the while loop, then flag[1]is
True and turn is 2.The while condition is False. Process 1 is not trapped by the while loop.
Only Process 1 arrived and entered the critical section, and Process 2 never arrived.
If process 2 never arrives , then flag[2] is False. When Process 1 is in the critical selection, then flag[1]
is True and turn is 2
Only Process 2 arrived and reached the while loop, and Process 1 never arrived.
If process 1 never arrives , then flag[1] is False. When Process 2 reached the while loop, then flag[2]is
True and turn is 1.The while condition is False. Process 2 is not trapped by the while loop.
Process 1 and Process 2 arrived at the same time and they both reached the while loop.
Both flag[1]and flag[2] are True because Process 1 and 2 have arrived. The variable turn maybe 1 or
2,depending on marginally which Process arrived a bit earlier. In this case, for the two while
loops,only 1 of them would trap the Process, and the other one will let the Process entering the
critical section.
...
Signal(S) Signal(S)
Exit
Process 1 is not allowed to do Task A nor Process 2 is not allowed to do Task B before Process 3 comes and
send the Signal after doing task C
2. If Process 3 terminates after sending the Signal, will both Task A and Task B be executed?
Yes, both should be executed if Process 3 sends out the signal.One of Process 1 or 2 will receive the
signal and return from the Wait function call and do the Task. After completing the Task, the process
will send a signal to allow another process to do the Task.