1 / 16

Operating Systems, 371-1-1631 Winter Semester 2011

Operating Systems, 371-1-1631 Winter Semester 2011. Practical Session 5, Synchronization. 1. Motivation. Multiprocessing needs some tools for managing shared resources Printers Files Data Bases. 2. Conditions for a good Solution.

hea
Download Presentation

Operating Systems, 371-1-1631 Winter Semester 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating Systems, 371-1-1631Winter Semester 2011 Practical Session 5, Synchronization 1

  2. Motivation • Multiprocessing needs some tools for managing shared resources • Printers • Files • Data Bases 2

  3. Conditions for a good Solution • Mutual Exclusion – No two processes are in the critical section(CS) at the same time • Deadlock Freedom – If two or more processes are trying to enter a CS onewill eventually enter it. • Starvation Freedom – A process trying to enter a CS will eventually manage to enter it. 3

  4. Solution Archetypes • Busy wait • Wastes CPU • Priority inversion:Task L (low priority) runs and gains exclusive use of resource R. H (high priority) task is introduced and attempts to acquire R – blocked. M (medium priority) becomes runnable before L releases R. Result: L can’t run and release R, H is waiting for R (and L) as long as M keeps running. • Sleep & Wake up 4

  5. Peterson’s Solution N=2; interested[0]=interested[1]=FALSE; int turn; /* Should be named NOT_MY_TURN */ int interested[N]; /* all initially 0 (FALSE) */ voidenter_region(int process){ /* who is entering 0 or 1 ? */int other = 1- process; /* opposite of process */interested[process] = TRUE; /* signal that you're interested */turn = process; /* set flag – NOT my turn */while (turn == process && interested[other] == TRUE); /* null statement */ } voidleave_region(int process){ /* who is leaving 0 or 1 ? */interested[process] = FALSE; /* departure from critical region */ } 5

  6. Peterson’s Solution Process = 0 Process = 1 interested[0]=interested[1]=FALSE int turn; int interested[2]; voidenter_region(int process){ int other = 1; interested[0] = TRUE; turn = 0; while (turn == 0 && interested[1] == TRUE); } voidleave_region(int process){ interested[0] = FALSE;} interested[0]=interested[1]=FALSE int turn; int interested[2]; voidenter_region(int process){ int other = 1- process; interested[1] = TRUE; turn = 1; while (turn == 1 && interested[0] == TRUE); } voidleave_region(int process){ interested[1] = FALSE;} 6

  7. Question 1 N=1; interested[0]=interested[1]=FALSE; int turn; /* Should be named NOT_MY_TURN */ int interested[N]; /* all initially 0 (FALSE) */ void enter_region(int process){ /* who is entering 0 or 1 ? */int other = 1- process; /* opposite of process */ turn = process; /* set flag – NOT my turn */interested[process] = TRUE; /* signal that you're interested */while (turn == process && interested[other] == TRUE); /* null statement */ } void leave_region(int process){ /* who is leaving 0 or 1 ? */interested[process] = FALSE; /* departure from critical region */ } What happens when the two red lines are in this order? 7

  8. Peterson’s Solution Process = 0 Process = 1 voidenter_region(int process){ int other = 1; turn = 0;interested[0] = TRUE; while (turn == 0 && interested[1] == TRUE); } voidleave_region(int process){ interested[0] = FALSE;} interested[0]=interested[1]=FALSE int turn; int interested[2]; voidenter_region(int process){ int other = 1- process; turn = 1; interested[1] = TRUE;while (turn == 1 && interested[0] == TRUE); } voidleave_region(int process){ interested[1] = FALSE;} 8

  9. Answer We will get a mutual exclusion violation: P1 does turn:=1; P0 does turn:=0; P0 does interested[0]:=true; P0 does while(turn == 0 && interested [1]); // (#t and #f)  #f P0 enters the CS. P1 does interested [1]:=true; P1 does while(turn == 1 && interested [0]); // (#f and #t)  #f P1 enters the CS. now both processes are in the critical section. 9

  10. Question 2 Assume the while statement in Peterson's solution is changed to:while (turn != process && interested[other] == TRUE) Describe a scheduling scenario in which we will receive a Mutual Exclusion and one in which we will NOT receive a Mutual Exclusion violation. 10

  11. Question 2 N=2; interested[0]=interested[1]=FALSE; int turn; /* Should be named NOT_MY_TURN */ int interested[N]; /* all initially 0 (FALSE) */ voidenter_region(int process){ /* who is entering 0 or 1 ? */int other = 1- process; /* opposite of process */interested[process] = TRUE; /* signal that you're interested */turn = process; /* set flag – NOT my turn */ while (turn != process && interested[other] == TRUE) /* null statement */ } voidleave_region(int process){ /* who is leaving 0 or 1 ? */interested[process] = FALSE; /* departure from critical region */ } 11

  12. Peterson’s Solution Process = 0 Process = 1 voidenter_region(int process){ int other = 1; interested[0] = TRUE; turn = 0; while (turn != 0 && interested[1] == TRUE); } voidleave_region(int process){ interested[0] = FALSE;} interested[0]=interested[1]=FALSE int turn; int interested[2]; voidenter_region(int process){ int other = 1- process; interested[1] = TRUE; turn = 1; while (turn != 1 && interested[0] == TRUE); } voidleave_region(int process){ interested[1] = FALSE;} 12

  13. Answer for Q.2 No Mutex violation: • One enters and exits and only afterwards the second receives a time quanta. • P0 – interested[0]=true, P0 – turn = 0, P1- interested[1]=true, P1 – turn =1, P1 passes while loop and enters CS, P0 – stuck in while loop. 13

  14. Answer for Q.2 Mutex violation: Process A executes: `interested [process] = TRUE', `turn = process‘ and falls through the while loop shown above. While in the critical section, the timer fires and process B executes: `interested [process] = TRUE', `turn = process' and falls through the while loop shown above. Both processes are in the critical section. 14

  15. Bakery Algorithm int value[N]={0, 0,…,0}; /* processes get “waiting numbers” */ int busy[N]={FALSE,…, FALSE}; /* just a flag... */ voidenter_region(inti ) /* process i entering.. */ { busy[i] = TRUE; /* guard the value selection */ value[i] = max(value[0], value[1], …, value[N-1]) + 1; /* LAST in line … */ busy[i] = FALSE; for(k=0; k < N; k ++){ while (busy[k] == TRUE) ; /* wait before checking */ while((value[k] != 0) && ((value[k], k) < (value[i], i))); /* wait */ } } void leave_region(inti ) { value[i ] = 0;} 15

  16. Semaphores Two atomic operations: up & down. down(S) if S≤0 block process. S-- up(S) S++ if there are blocked processes wake one up. NOTE: Semaphore’s interface doesn’t enforce the implementation of starvation freedom. 16

More Related