


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
N process critical section problem, The bakery algorithm, 2 process critical section problem, Boolean array, Process names, Common data structures, Observations. Above mentioned are key points of this lecture handout. Virtual University handout for introduction to operating system are in detail and explanatory.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Operating Systems--[CS-604] Lecture No. 20
Chapter 7 of the textbook Lecture 20 on Virtual TV
2-Process Critical Section Problem (continued) n-Process Critical Section Problem The Bakery Algorithm
We discussed two solutions for the 2-process critical section problem in lecture 19 but both were not acceptable because they did not satisfy the progress condition. Here is a good solution for the critical section problem that satisfies all three requirements of a good solution.
The processes share two variables: boolean flag[2]; int turn;
The boolean array of ‘flag’ is initialized to false, whereas ‘turn’ maybe 0 or 1. The structure of the process is as follows:
do { flag[i]=true; turn=j; while(flag[j] && turn==j); critical section flag[i]=false; remainder section } while(1)
To enter its critical section, P (^) i sets flag[i] to true, and sets ‘turn’ to j, asserting that if the other process wishes to enter its critical section, it may do so. If both try to enter at the
same time, they will attempt to set ‘turn’ to i and j. However, only one of these assignments will last, the other will occur but be overwritten instantly. Hence, the eventual value of ‘turn’ will decide which process gets to enter its critical section. To prove mutual exclusion, note that P (^) i enters its critical section only if either flag[j]=false or turn=i. Also, if both processes were executing in their critical sections at the same time, then flag[0]= = flag[1]= = true. These two observations suggest that P 0 and P 1 could not have found both conditions in the while statement true at the same time, since the value of ‘turn’ can either be 0 or 1. Hence only one process say P 0 must have successfully exited the while statement. Hence mutual exclusion is preserved. To prove bounded wait and progress requirements, we note that a process Pi can be prevented the critical section only if it is stuck in the while loop with the condition flag[j]= =true and turn=j. If Pj is not ready to enter the critical section, then flag[j]=flase and Pi can enter its critical section. If Pj has set flag[j]=true and is also executing its while statement then either turn=i or turn=j. If turn=i then Pi enters its critical section, otherwise Pj. However, whenever a process finishes executing in its critical section, lets assume Pj , it resets flag[j] to false allowing Pi to enter its critical section. If P (^) j resets flag[j]=true, then it must also set ‘turn’ to i, and since Pi does not change the value of ‘turn’ while executing in its while statement, P (^) i will enter its critical section (progress) after at most one entry by Pj (bounded waiting).
In this section we extend the critical section problem of two processes to include n processes. Consider a system of n processes (Po , P 1 …… Pn-1 ). Each process has a segment of code called a critical section in which the process may be changing common variables, updating a table, writing a file and so on. The important feature of the system in that, when one process enters its critical section, no other process is allowed to execute in its critical section. Thus the execution of critical sections by the processes is mutually exclusive in time. The critical section problem is to design a protocol to serialize executions of critical sections. Each process must request permission to enter its critical section. Many solutions are available in the literature to solve the N-process critical section problem. We will discuss a simple and elegant solution, known as the Bakery algorithm.
The bakery algorithm is due to Leslie Lamport and is based on a scheduling algorithm commonly used in bakeries, ice-cream stores, and other locations where order must be made out of chaos. On entering the store, each customer receives a number. The customer with the lowest number is served next. Before entering its critical section, process receives a ticket number. Holder of the smallest ticket number enters its critical section. Unfortunately, the bakery algorithm cannot guarantee that two processes (customers) will not receive the same number. In the case of a tie, the process with the lowest ID is served first. If processes Pi and Pj receive the same number, if i < j, then Pi is served first; else Pj is served first. The ticket numbering scheme always generates numbers in the increasing order of enumeration; i.e., 1, 2, 3, 4, 5 ...
Process Number P0 3 P1 0 P2 7 P3 4 P4 8 The following table shows the status of all the processes as they execute the ‘for’ loops in their entry sections. The gray cells show processes waiting in the second while loops in their entry sections. The table shows that P0 never waits for any process and is, therefore, the first process to enter its critical section, while all other processes wait in their second while loops for j = = 0, indicating that they are waiting for P0 to get out of its critical section and then they would make progress (i.e., they will get out the while loop, increment j by one, and continue their execution). You can make the following observations by following the Bakery algorithm closely with the help of this table:
P1 not interested to get into its critical section ⇒ number[1] is 0 P2, P3, and P4 wait for P P0 gets into its CS, get out, and sets its number to 0 P3 get into its CS and P2 and P4 wait for it to get out of its CS P2 gets into its CS and P4 waits for it to get out P4 gets into its CS Sequence of execution of processes: <P0, P3, P2, P4>
j P0 P2 P3 P
0 (3,0) < (3,0) (3,0) < (7,2) (3,0) < (4,3) (3,0) < (8,4)
1 Number[1] = 0 Number[1] = 0 Number[1] = 0 Number[1] = 0
2 (7,2) < (3,0) (7,2) < (7,2) (7,2) < (4,3) (7,2) < (8,4)
3 (4,3) < (3,0) (4,3) < (7,2) (4,3) < (4,3) (4,3) < (8,4)
4 (8,4) < (3,0) (8,4) < (7,2) (8,4) < (4,3) (8,4) < (8,4)