0% found this document useful (0 votes)
15 views2 pages

Lab 9

The document describes the Peterson algorithm for mutual exclusion between two processes. Each process sets its flag to true to indicate it wants to enter the critical section and sets the turn variable to the other process's index. It then waits in a busy-wait loop until the other process's flag is false and it is its turn before entering the critical section. After leaving the critical section, the process sets its flag to false and both processes repeat this in an infinite loop to allow alternating access to the critical section.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Lab 9

The document describes the Peterson algorithm for mutual exclusion between two processes. Each process sets its flag to true to indicate it wants to enter the critical section and sets the turn variable to the other process's index. It then waits in a busy-wait loop until the other process's flag is false and it is its turn before entering the critical section. After leaving the critical section, the process sets its flag to false and both processes repeat this in an infinite loop to allow alternating access to the critical section.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

do {

flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
// critical section
flag[i] = FALSE;
// remainder section
} while (TRUE);
Abdullah Rehman

F2021266327

Peterson algorithm

flag[i] = TRUE: This line indicates that Process 1 is interested in entering the
critical section. It sets its flag to true, indicating its intention to enter the critical
section.

turn = j: Process 1 sets the turn variable to the other process's index (j), indicating
that it's the other process's turn to enter the critical section.

while (flag[j] && turn == j): This is the busy-wait loop. Process 1 waits until the
other process's flag is false (flag[j] == FALSE) or it's Process 1's turn (turn == j).

Critical Section: The code inside the critical section is where the actual work is
done that needs to be protected from concurrent access by other processes.

flag[i] = FALSE: Process 1 resets its flag to false, indicating that it has completed
its critical section.

Remainder Section: The code after the critical section is the remainder section,
which contains non-critical code that can be executed concurrently by other
processes.

} while (TRUE): The entire block is enclosed in an infinite loop, ensuring that
Process 1 repeats this process indefinitely
do {
flag[j] = TRUE;
turn = i;
while (flag[i] && turn == i);
// critical section
flag[j] = FALSE;
// remainder section

} while (TRUE);

flag[j] = TRUE: Process 2 sets its flag to true, indicating its intention to enter the
critical section.

turn = i: Process 2 sets the turn variable to the other process's index (i), indicating
that it's the other process's turn to enter the critical section.

while (flag[i] && turn == i): Process 2 waits until the other process's flag is false
(flag[i] == FALSE) or it's Process 2's turn (turn == i).

Critical Section: The code inside the critical section is where the actual work is
done that needs to be protected from concurrent access by other processes.

flag[j] = FALSE: Process 2 resets its flag to false, indicating that it has completed
its critical section.

Remainder Section: The code after the critical section is the remainder section,
which contains non-critical code that can be executed concurrently by other
processes.

} while (TRUE): The entire block is enclosed in an infinite loop, ensuring that
Process 2 repeats this process indefinitely.

You might also like