edited by
26,997 views
59 votes
59 votes

Consider Peterson's algorithm for mutual exclusion between two concurrent processes i and j. The program executed by process is shown below.

repeat
    flag[i] = true;
    turn = j;
    while (P) do no-op;
    Enter critical section, perform actions, then
    exit critical section
    Flag[i] = false;
    Perform other non-critical section actions.
Until false;

For the program to guarantee mutual exclusion, the predicate P in the while loop should be

  1. flag[j] = true and turn = i
  2. flag[j] = true and turn = j
  3. flag[i] = true and turn = j
  4. flag[i] = true and turn = i
edited by

5 Answers

0 votes
0 votes

Here, Question should specify this code section is executed by Process Pi. At first look it creates confusion. 

code for Process Pi:

L1:repeat
L2:    flag[i] = true;
L3:    turn = j;
L4:    while (flag[j]==True && turn==j) // becoz you are the one who sets turn variable to j
           do no-op;                    // if other process is interested then you should wait.
L5:    Enter critical section, perform actions, then
L6:    exit critical section
L7:    Flag[i] = false;
L8:    Perform other non-critical section actions.
L9:Until false;

Similarly code for Pj:

L1:repeat
L2:    flag[j] = true;
L3:    turn = i;
L4:    while (flag[i]==True && turn==i) // becoz you are the one who sets turn variable to j
        do no-op;                       // if other process is interested then you should wait.
L5:    Enter critical section, perform actions, then
L6:    exit critical section
L7:    Flag[j] = false;
L8:    Perform other non-critical section actions.
L9:Until false;

This way solution ensures mutual exclusion. Hence option (b) is correct answer.

Answer:

Related questions

49 votes
49 votes
4 answers
1
Kathleen asked Sep 14, 2014
66,221 views
Consider a machine with $64$ MB physical memory and a $32$-bit virtual address space. If the page size s $4$ KB, what is the approximate size of the page table?$\text{16 ...
25 votes
25 votes
2 answers
2
Kathleen asked Sep 14, 2014
10,507 views
Which of the following requires a device driver?RegisterCacheMain memoryDisk