The document discusses various techniques for process synchronization and solving the critical section problem where multiple processes need exclusive access to shared resources. It describes the critical section problem and requirements that must be met (mutual exclusion, progress, and bounded waiting). It then summarizes several algorithms to solve the problem for two processes and multiple processes, including using semaphores which are basic synchronization tools using wait and signal operations.
INTRODUCTION Cooperating Processesis one that can be affected or will affect other process executing in the system. Concurrent access to share data may result in to data inconsistency So we will discuss mechanisms to ensure the orderly execution of cooperating processes that share logical address space, so that consistency is maintained .
3.
CRITICAL SECTION n processes all competing to use some shared data Each process has a code segment, called critical section , in which the shared data is accessed. Set of instructions that must be controlled so as to allow exclusive access to one process. execution of the critical section by processes is mutually exclusive in time .
4.
CRITICAL SECTION Do{critical section reminder section }while(1) Each process must request permission to enter critical section – ENTRY SECTION Critical section may follow by EXIT SECTION . Remaining code is REMAINDER SECTION Cont… Entry section exit section GENERAL STRUCTURE OF A TYPICAL PROCESS Pi
5.
CRITICAL SECTION Solutionto the Critical Section Problem must meet three conditions... Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Cont…
6.
TWO-PROCESS SOLUTION Algorithmis only for 2 processes at a time Processes are P 0 P 1 Or can also be represented as P i and P j , i.e. j=1-i
7.
TWO-PROCESS SOLUTION -Algorithm1 If process turn == i , P i is allowed to execute in critical section But, Guarantees mutual exclusion. Does not guarantee progress --- enforces strict alternation of processes entering CS's ( if P j decides not to re-enter or crashes outside CS, then P i cannot ever get in) . Let the processes share common integer variable turn Let int turn =0 (or 1) Do{ Critical section remainder section }while(1) For Process P i while (turn != i); Turn = j;
8.
TWO-PROCESS SOLUTION -Algorithm2 Shared variables boolean flag[2]; // “interest” bits initially flag [0] = flag [1] = false. flag [i] = true P i declares interest in entering its critical section Process P i // where the “other” process is P j do { flag[i] = true; // declare your own interest while (flag[ j]) ; //wait if the other guy is interested critical section flag [i] = false; // declare that you lost interest remainder section // allows other guy to enter } while (1);
9.
Satisfies mutual exclusion,but not progress requirement. If flag[ i] == flag[ j] == true, then deadlock - no progress but barring this event, a non-CS guy cannot block you from entering Can make consecutive re-entries to CS if other not interested TWO-PROCESS SOLUTION -Algorithm 2
10.
Combined shared variables& approaches of algorithms 1 and 2. TWO-PROCESS SOLUTION -Algorithm 3 For Process P i do { flag [i] = true; // declare your interest to enter turn = j; // assume it is the other’s turn-give PJ a chance while (flag [ j ] and turn == j) ; critical section flag [i] = false; remainder section } while (1);
11.
Meets all threerequirements; solves the critical-section problem for two processes ( the best of all worlds - almost!). Turn variable breaks any deadlock possibility of previous example, AND prevents “hogging” – P i setting turn to j gives P J a chance after each pass of P i ’s CS Flag[ ] variable prevents getting locked out if other guy never re-enters or crashes outside and allows CS consecutive access other not interested in entering. TWO-PROCESS SOLUTION -Algorithm 3
12.
MULTIPLE-PROCESS SOLUTIONS -Bakery Algorithm Algo solves the critical-section problem for n-process. Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section. If processes P i and P j receive the same number, if i < j , then P i is served first; else P j is served first. Since process name are unique The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...
13.
The common datastructure are: boolean choosing[n]; Int number[n]; Initially, boolean choosing[n]=false; int number[n]=o; Notation ( a,b ) < c,d ) if a < c or if a = c and b < d max ( a 0 ,…, a n -1 ) is a number, k , such that k a i for i = 0, … , n – 1 MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
14.
The structure ofprocess P i in the bakery algorithm is as follow… MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
15.
choosing[i] = true; //process does not “compete” while choosing a # number[i] = max(number[0], number[1], …, number [n – 1]) + 1; choosing[i] = false; /*scan all processes to see if if Pi has lowest number: */ for (k = 0; k < n; k++) { /* check process k, book uses dummy variable “j” */ while (choosing[ k ]); /*if Pk is choosing a number wait till done */ while ((number[k] != 0) && (number[k], k < number[ i ], i)) ; /*if (Pk is waiting (or in CS) and Pk is “ahead” of Pi then Pi waits */ /*if Pk is not in CS and is not waiting, OR Pk is waiting with a larger number, then skip over Pk - when Pi gets to end of scan, it will have lowest number and Will fall thru to the CS */ /*If Pi is waiting on Pk, then number[k] will go to 0 because Pk will eventually Get served –thus causing Pi to break out of the while loop and check out the status of the next process if any */ /*the while loop skips over the case k == i */ } do { /* Has four states: choosing, scanning, CS, remainder section */ critical section /* no longer a candidate for CS entry */ remainder section } while (1); number[i] = 0;
16.
SEMAPHORSES Solve critical-sectionproblem, Synchronization tool- semaphores Two or more processes can cooperate by means of simple signals, such that a process can be forced to stop at a specific place until it has received a specific signal For signaling, special variables, called semaphores are used Semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operation : wait and signal. They are termed as P for wait (to test) V for signal (to increment)
17.
SEMAPHORSES It canonly be accessed via two indivisible ( atomic ) operations. Definition-Wait wait(S) { while(S<=0) ; //no-op S--; } Definition-signal Signal(S) { S++; }
18.
A queue isused to hold processes waiting on a semaphore. struct semaphore { int count; queueType queue; } void wait(semaphore s) { s.count--; if (s.count < 0) { place this process in s.queue; block this process } } void signal(semaphore s) { s.count++; if (s.count <= 0) { remove a process P from s.queue; place process P on ready list; } }
19.
SEMAPHORES -Two Type Counting semaphore – integer value can range over an unrestricted domain. Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. Can implement a counting semaphore S as a binary semaphore
20.
SEMAPHORES -Binary SemaphoresStruct b-semaphore { int value; /* boolean, only 0 or 1 allowed */ struct process queue; } Void wait-b (b-semaphore s) { /* see alternate def. Below */ if (s.value == 1) s.value = 0; // Lock the “door” & let process enter CS else { place this process in s.queue and block it;} // no indication of size of queue ... compare to general semaphore } // wait unconditionally leaves b-semaphore value at 0 Void signal-b(b-semaphore s) { //s.value==0 is necessary but not sufficient if (s.queue is empty) s.value = 1; // condition for empty queue else move a process from s.queue to ready list; } // if only 1 proc in queue, leave value at 0, “moved” proc will go to CS ********************************************************************* Alternate definition of wait-b (simpler): Void wait-b (b-semaphore s) { if (s.value == 0) {place this process in s.queue and block it;} s.value =0; // value was 1, set to 0 }