Process Synchronization


Published on

Published in: Technology, Education
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Process Synchronization

    2. 2. INTRODUCTION <ul><li>Cooperating Processes is one that can be affected or will affect other process executing in the system. </li></ul><ul><li>Concurrent access to share data may result in to data inconsistency </li></ul><ul><li>So we will discuss mechanisms to ensure the orderly execution of cooperating processes that share logical address space, so that consistency is maintained . </li></ul>
    3. 3. CRITICAL SECTION <ul><li>n processes all competing to use some shared data </li></ul><ul><li>Each process has a code segment, called critical section , in which the shared data is accessed. </li></ul><ul><li>Set of instructions that must be controlled so as to allow exclusive access to one process. </li></ul><ul><li>execution of the critical section by processes is mutually exclusive in time . </li></ul>
    4. 4. CRITICAL SECTION <ul><ul><li>Do{ </li></ul></ul><ul><ul><li>critical section </li></ul></ul><ul><ul><li>reminder section </li></ul></ul><ul><ul><li>}while(1) </li></ul></ul><ul><li>Each process must request permission to enter critical section – ENTRY SECTION </li></ul><ul><li>Critical section may follow by EXIT SECTION . </li></ul><ul><li>Remaining code is REMAINDER SECTION </li></ul>Cont… Entry section exit section GENERAL STRUCTURE OF A TYPICAL PROCESS Pi
    5. 5. CRITICAL SECTION <ul><li>Solution to the Critical Section Problem must meet three conditions... </li></ul><ul><li>Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections </li></ul><ul><li>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 </li></ul><ul><li>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 </li></ul>Cont…
    6. 6. TWO-PROCESS SOLUTION <ul><li>Algorithm is only for 2 processes at a time </li></ul><ul><li>Processes are </li></ul><ul><ul><li>P 0 </li></ul></ul><ul><ul><li>P 1 </li></ul></ul><ul><li>Or can also be represented as P i and P j , </li></ul><ul><li>i.e. j=1-i </li></ul>
    7. 7. TWO-PROCESS SOLUTION -Algorithm 1 <ul><li>If process turn == i , P i is allowed to execute in critical section </li></ul><ul><li>But, </li></ul><ul><ul><li>Guarantees mutual exclusion. </li></ul></ul><ul><ul><li>Does not guarantee progress --- enforces strict alternation of processes entering CS's </li></ul></ul><ul><ul><li>( if P j decides not to re-enter or crashes outside CS, then P i cannot ever get in) . </li></ul></ul><ul><li>Let the processes share common integer variable turn </li></ul><ul><li>Let int turn =0 (or 1) </li></ul>Do{ Critical section remainder section }while(1) For Process P i while (turn != i); Turn = j;
    8. 8. TWO-PROCESS SOLUTION -Algorithm 2 <ul><li>Shared variables </li></ul><ul><ul><li>boolean flag[2]; // “interest” bits initially flag [0] = flag [1] = false. </li></ul></ul><ul><ul><li>flag [i] = true  P i declares interest in entering its critical section </li></ul></ul><ul><li>Process P i // where the “other” process is P j </li></ul>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. 9. <ul><li>Satisfies mutual exclusion, but not progress requirement. </li></ul><ul><ul><li>If flag[ i] == flag[ j] == true, then deadlock - no progress </li></ul></ul><ul><ul><li>but barring this event, a non-CS guy cannot block you from entering </li></ul></ul><ul><ul><li>Can make consecutive re-entries to CS if other not interested </li></ul></ul>TWO-PROCESS SOLUTION -Algorithm 2
    10. 10. <ul><li>Combined shared variables & approaches of algorithms 1 and 2. </li></ul>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. 11. <ul><li>Meets all three requirements; solves the critical-section problem for two processes ( the best of all worlds - almost!). </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>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. </li></ul></ul>TWO-PROCESS SOLUTION -Algorithm 3
    12. 12. MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm <ul><li>Algo solves the critical-section problem for n-process. </li></ul><ul><li>Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section. </li></ul><ul><li>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. </li></ul><ul><ul><li>Since process name are unique </li></ul></ul><ul><li>The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5... </li></ul>
    13. 13. <ul><li>The common data structure are: </li></ul><ul><ul><li>boolean choosing[n]; </li></ul></ul><ul><ul><li>Int number[n]; </li></ul></ul><ul><li>Initially, </li></ul><ul><ul><li>boolean choosing[n]=false; </li></ul></ul><ul><ul><li>int number[n]=o; </li></ul></ul><ul><li>Notation </li></ul><ul><ul><li>( a,b ) < c,d ) if a < c or if a = c and b < d </li></ul></ul><ul><ul><li>max ( a 0 ,…, a n -1 ) is a number, k , such that k  a i for i = 0, … , n – 1 </li></ul></ul>MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
    14. 14. <ul><li>The structure of process P i in the bakery algorithm is as follow… </li></ul>MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
    15. 15. <ul><li>choosing[i] = true; //process does not “compete” while choosing a # </li></ul><ul><li>number[i] = max(number[0], number[1], …, number [n – 1]) + 1; </li></ul><ul><li>choosing[i] = false; </li></ul><ul><li>/*scan all processes to see if if Pi has lowest number: */ </li></ul><ul><li>for (k = 0; k < n; k++) { /* check process k, book uses dummy variable “j” */ </li></ul><ul><li>while (choosing[ k ]); /*if Pk is choosing a number wait till done */ </li></ul><ul><li>while ((number[k] != 0) && (number[k], k < number[ i ], i)) ; </li></ul><ul><li>/*if (Pk is waiting (or in CS) and Pk is “ahead” of Pi then Pi waits */ </li></ul><ul><li>/*if Pk is not in CS and is not waiting, OR Pk is waiting with a larger number, </li></ul><ul><li>then skip over Pk - when Pi gets to end of scan, it will have lowest number and </li></ul><ul><li>Will fall thru to the CS */ </li></ul><ul><li>/*If Pi is waiting on Pk, then number[k] will go to 0 because Pk will eventually </li></ul><ul><li>Get served –thus causing Pi to break out of the while loop and check out the </li></ul><ul><li>status of the next process if any */ </li></ul><ul><li>/*the while loop skips over the case k == i */ </li></ul><ul><li>} </li></ul>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. 16. SEMAPHORSES <ul><li>Solve critical-section problem, Synchronization tool- semaphores </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>For signaling, special variables, called semaphores are used </li></ul></ul><ul><li>Semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operation : wait and signal. </li></ul><ul><li>They are termed as </li></ul><ul><ul><li>P for wait (to test) </li></ul></ul><ul><ul><li>V for signal (to increment) </li></ul></ul>
    17. 17. SEMAPHORSES <ul><li>It can only be accessed via two indivisible ( atomic ) operations. </li></ul>Definition-Wait wait(S) { while(S<=0) ; //no-op S--; } Definition-signal Signal(S) { S++; }
    18. 18. <ul><li>A queue is used to hold processes waiting on a semaphore. </li></ul>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. 19. SEMAPHORES -Two Type <ul><li>Counting semaphore – integer value can range over an unrestricted domain. </li></ul><ul><li>Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. </li></ul><ul><li>Can implement a counting semaphore S as a binary semaphore </li></ul>
    20. 20. SEMAPHORES -Binary Semaphores <ul><li>Struct b-semaphore { </li></ul><ul><li>int value; /* boolean, only 0 or 1 allowed */ </li></ul><ul><li>struct process queue; </li></ul><ul><li>} </li></ul><ul><li>Void wait-b (b-semaphore s) { /* see alternate def. Below */ </li></ul><ul><li>if (s.value == 1) s.value = 0; // Lock the “door” & let process enter CS </li></ul><ul><li>else { place this process in s.queue and block it;} </li></ul><ul><li> // no indication of size of queue ... compare to general semaphore </li></ul><ul><li>} // wait unconditionally leaves b-semaphore value at 0 </li></ul><ul><li>Void signal-b(b-semaphore s) { //s.value==0 is necessary but not sufficient </li></ul><ul><li>if (s.queue is empty) s.value = 1; // condition for empty queue </li></ul><ul><li>else move a process from s.queue to ready list; </li></ul><ul><li>} // if only 1 proc in queue, leave value at 0, “moved” proc will go to CS </li></ul><ul><li>********************************************************************* </li></ul><ul><li>Alternate definition of wait-b (simpler): </li></ul><ul><li>Void wait-b (b-semaphore s) { </li></ul><ul><li>if (s.value == 0) {place this process in s.queue and block it;} </li></ul><ul><li>s.value =0; // value was 1, set to 0 </li></ul><ul><li>} </li></ul>