Practical Session 5 Synchronization

Slides:



Advertisements
Similar presentations
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Secure Operating Systems Lesson 5: Shared Objects.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
1 Operating Systems, 112 Practical Session 5, Synchronization 1.
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Nachos Phase 1 Code -Hints and Comments
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Solutions to Second 4330/6310 Quiz Spring First question Which of the following statements are true or false (2 points) and why? (3 points)
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
CSE 120 Principles of Operating
Practical Session 5 Synchronization
CS703 – Advanced Operating Systems
Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Atomic Operations in Hardware
Atomic Operations in Hardware
ICS143A: Principles of Operating Systems Lecture 15: Locking
Chapter 5: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Practical Session 5 Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Practical Session 5, Synchronization
Grades.
Implementing Mutual Exclusion
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Syllabus 1. Introduction - History; Views; Concepts; Structure
Presentation transcript:

Practical Session 5 Synchronization Operating Systems Practical Session 5 Synchronization 1

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

Conditions for a good Solution Mutual Exclusion – No two processes are in the critical section (CS) at the same time Deadlock Freedom – If processes are trying to enter the CS one will eventually enter it Starvation Freedom – When a process tries to enter its CS, it will eventually succeed 3

Solution Archetypes Busy wait Sleep & Wake up Wastes CPU Priority inversion with busy waiting (*): Task L (low priority) runs and gains exclusive use of resource R. H (high priority) task is introduced and attempts to acquire R and must therefore wait. Note that since H is busy waiting it may still be scheduled (its state remains runnable). Result: L can’t run and release R because H is of higher priority and is scheduled to run before it. H on the other hand can’t proceed past its busy wait loop. Deadlock! Sleep & Wake up Also prone to priority inversion but will not deadlock. Can you think of a scenario? Priority inversion may also occur with semaphores although results won’t be as bad: 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. 4

Peterson’s Solution Process = 0 Process = 1 int interested[2]; int giveup; interested[0]=interested[1]=FALSE void enter_region(…){ int other = 1; interested[0] = TRUE; giveup = 0; while (giveup == 0 && interested[1] == TRUE); } void leave_region(…){ interested[0] = FALSE;} int interested[2]; int giveup; interested[0]=interested[1]=FALSE void enter_region(…){ int other = 0; interested[1] = TRUE; giveup = 1; while (giveup == 1 && interested[0] == TRUE); } void leave_region(…){ interested[1] = FALSE;} If we only examine code lines, then the number of scheduling combinations is 6!/(3!*3!)=20, however, since the while loop is comprised of three different operations (assessing both parts + an AND operation) there are significantly more scheduling alternatives: 10!/(5!*5!) 6

Question 1 N=1; interested[0]=interested[1]=FALSE; int giveup; 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 */ giveup = process; 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 */ Consider the following variant of Peterson’s solution where the two red lines are swapped. Does this variant solve the mutual exclusion problem? 7

Peterson’s Solution Process = 0 Process = 1 int giveup; int interested[2]; interested[0]=interested[1]=FALSE void enter_region(…){ int other = 1; giveup = 0; interested[0] = TRUE; while (giveup == 0 && interested[1] == TRUE); } void leave_region(…){ interested[0] = FALSE;} void enter_region(…){ int other = 0; giveup = 1; interested[1] = TRUE; while (giveup == 1 && interested[0] == TRUE); } void leave_region(…){ interested[1] = FALSE;} 8

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

while (giveup != process && interested[other] == TRUE) Question 2 Assume the while statement in Peterson's solution is changed to: while (giveup != process && interested[other] == TRUE) 10

while (turn != process && interested[other] == TRUE) 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 violation and one in which we will NOT receive a Mutual Exclusion violation. 11

Peterson’s Solution Process = 0 Process = 1 interested[0]=interested[1]=FALSE int giveup; int interested[2]; void enter_region(…){ int other = 1; interested[0] = TRUE; giveup = 0; while (giveup != 0 && interested[1] == TRUE); } void leave_region(…){ interested[0] = FALSE;} void enter_region(…){ int other = 0; interested[1] = TRUE; giveup = 1; while (giveup != 1 && interested[0] == TRUE); } void leave_region(…){ interested[1] = FALSE;} 12

Answer for Q.2 No mutual exclusion violation: One enters and exits and only afterwards the second receives a time quanta. P0 – interested[0]=true, P0 – giveup = 0, P1- interested[1]=true, P1 – giveup =1, P1 passes while loop and enters CS, P0 – stuck in while loop. 3. Many more…. 13

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

Dekker’s algorithm Process = 0 Process = 1 1.bool flag[2] = {false, false}; 2.int turn = 0 ;   3. void enter_region(…){ 4. flag[0] = true; while (flag[1]) { 6. if (turn==1) { 7. flag[0] = false; 8. while (turn == 1) ; flag[0] = true;}}} 10. void leave_region(…){ 11. turn = 1; 12. flag[0] = false; } 1.bool flag[2] = {false, false}; 2.int turn = 0 ;   3. void enter_region(…){ 4. flag[1] = true; while (flag[0]) { 6. if (turn==0) { 7. flag[1] = false; 8. while (turn == 0) ; flag[1] = true;}}} 10. void leave_region(…){ 11. turn = 0; 12. flag[1] = false; } 15

Mutual exclusion for n processes: A tournament tree Level 2 1 2 3 4 5 6 7 Level 1 Level 0 Processes A tree-node is identified by: [level, node#]

Lamport’s Bakery Algorithm int value[N]={0, 0,…,0}; /* processes get “waiting numbers” */ int busy[N]={FALSE,…, FALSE}; /* just a flag... */ void enter_region(int i ) /* 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] < value[i] || (value[k] == value[i] && k < i) )); /* wait */ } void leave_region(int i ) { value[i ] = 0; (a, b) < (c, d) is equivalent to: (a < c) or ((a == c) and (b < d)) 25

Question 4 Assume we have the following atomic command: void swap(bool *a, bool *b) { bool temp = *a; *a = *b; *b = temp; } Solve the N processes mutual exclusion problem using the swap command. 26

Answer Q.4 void swap(bool *a, bool *b) {         bool temp = *a;         *a = *b;         *b = temp; } bool lock = FALSE; void enter(int i) { bool key = TRUE; while (key) { swap(&key, &lock); } } void leave(int i) {      lock = FALSE; } 27

Answer Q.4 void swap(bool *a, bool *b) {         bool temp = *a;         *a = *b;         *b = temp; } bool lock = FALSE; bool waiting[N] = {FALSE, FALSE, …, FALSE}; void enter(int i) { waiting[i] = TRUE; bool key = TRUE; while (TRUE) { swap(&key, &lock); if ( !waiting[i] || !key) break; } } void leave(int i) { waiting[i]= FALSE ;         int j = (i + 1) % N;         while ( (j != i) && waiting[j]==FALSE ) {                j = (j + 1) % N;         }         if (j == i) {                lock = FALSE;         } else {                waiting[j] = FALSE;         } } The idea: Seek the next (chronologically ordered) interested process. If such a process exist, grant it the ability to enter the CS. Otherwise reset the lock so that other processes will be able to enter the CS at a later time. The idea: Use a local variable “key” which will be set to true. Let all processes swap it with current lock and compete over this line. Only the first process to grab it will have a value of false to the key. This process will enter the CS. 28

Semaphores Enable simple synchronization An interface of atomic functions supplying mutual exclusion Programmers don’t need to bother with synchronization algorithms

Counting semaphore Init(i) down(S) [the ‘p’ operation] S = i down(S) [the ‘p’ operation] If (S≤0) the process is blocked. It will resume execution only after it is woken-up Else S-- up(S) [the `v’ operation] If (there are blocked processes) wake-up one of them Else S++ Semaphore’s interface doesn’t enforce the implementation of starvation freedom. All operations are ATOMIC! That is, up(s) is more than just s:=s+1 There is no way to access the semaphore’s internal value. Any attempt to access it is a mistake. Always remember to initialize the semaphore

XV6 - Spinlock spinlock.h // Mutual exclusion lock. struct spinlock { uint locked; // Is the lock held? // For debugging: char *name; // Name of lock. struct cpu *cpu; // The cpu holding the lock. uint pcs[10]; // The call stack (an array of program counters) // that locked the lock. }; Spinlock is used to synchronized between different CPU’s running the kernel code and using common resources such as access to external controllers and memory 31

XV6 - Spinlock Int xchg(int *addr, int value) { int temp = *addr; XV6 uses an atomic x86 operation called xchg: Int xchg(int *addr, int value) { int temp = *addr; *addr = value; return temp; } Spinlock is used to synchronized between different CPU’s running the kernel code and using common resources such as access to external controllers and memory 32

XV6 - Spinlock spinlock.c spinlock.c // Release the lock. void release(struct spinlock *lk) { if(!holding(lk)) panic("release"); lk->pcs[0] = 0; lk->cpu = 0; // Tell the C compiler and the processor to not move loads or stores // past this point, to ensure that all the stores in the critical // section are visible to other cores before the lock is released. // Both the C compiler and the hardware may re-order loads and // stores; __sync_synchronize() tells them both not to. __sync_synchronize(); // Release the lock, equivalent to lk->locked = 0. // This code can't use a C assignment, since it might // not be atomic. A real OS would use C atomics here. asm volatile("movl $0, %0" : "+m" (lk->locked) : ); popcli(); } spinlock.c // Acquire the lock. // Loops (spins) until the lock is acquired. // Holding a lock for a long time may cause // other CPUs to waste time spinning to acquire it. void acquire(struct spinlock *lk) { pushcli(); // disable interrupts to avoid deadlock. if(holding(lk)) panic("acquire"); // The xchg is atomic. while(xchg(&lk->locked, 1) != 0) ; // Tell the C compiler and the processor to not move loads or stores // past this point, to ensure that the critical section's memory // references happen after the lock is acquired. __sync_synchronize(); // Record info about lock acquisition for debugging. lk->cpu = mycpu(); getcallerpcs(&lk, lk->pcs); } pushcli disables interrupts so the CPU will not return to handle a new request when in the middle of important code. If we would allow the CPU to receive interrupts it could return to handle an event via alltraps and try to take the lock again and get a panic (or deadlock if the panic would be removed). The pushcli / popcli functions work as a stack disabling interrupts until the stack is empty. Notice that this is a type of busy wait, we keep trying to take the lock using the xchg action (atomic). This makes acquire a blocking action for the entire CPU! (not process) 33

XV6 - Scheduler proc.c proc.c c->proc = p; switchuvm(p); // Switch to chosen process. It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. c->proc = p; switchuvm(p); p->state = RUNNING; swtch(&(c->scheduler), p->context); switchkvm(); // Process is done running for now. // It should have changed its p->state before coming back. c->proc = 0; } release(&ptable.lock); } proc.c // Per-CPU process scheduler. // Each CPU calls scheduler() after setting itself up. // Scheduler never returns. It loops, doing: // - choose a process to run // - swtch to start running that process // - eventually that process transfers control // via swtch back to the scheduler. void scheduler(void) { struct proc *p; struct cpu *c = mycpu(); c->proc = 0; for(;;){ // Enable interrupts on this processor. sti(); // Loop over process table looking for process to run. acquire(&ptable.lock); for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ if(p->state != RUNNABLE) continue; 34

Homework

Question 5a – midterm 2010 Let A be an n processes (finite) mutual exclusion algorithm which is applied with the following guarantee: whenever A is used, any process will attempt to access the critical section only once. Prove or disprove the following claim: A is a starvation free algorithm if and only if it is a deadlock free algorithm.

Answer Q.5a The first part of the proof is trivial: starvation freedom also implies deadlock freedom. What about the second one? Assume that there are n processes and that A is free of deadlocks. Now, let us falsely assume that A is not a starvation free algorithm and contradict this assumption. If A is not a starvation free algorithm, then there must exist some process pi which goes through infinitely many steps but still can’t enter the CS. Reductio ad absurdum

Answer Q.5a, continued While pi goes through its steps other processes manage to enter the CS (we assumed deadlock freedom). As each process pj may only enter the CS once, at some point all n-1 processes will go through (in and out of) the CS and we will be left with pi. At this point, pi is the only process attempting to enter the CS. Following our initial assumption, there can be no deadlocks and pi will enter the CS. This contradicts our false assumption that A is not a starvation free algorithm. QED Reductio ad absurdum

Question Q.5b – midterm 2010 Consider the following simple algorithm similar to that shown in class: Is this algorithm deadlock free? Prove or disprove by providing an accurate scenario which leads to a deadlock. Test-and-set(w) do atomically prev:=w w:=1 return prev 1 int lock initially 0 2 boolean interested[n] initially {false,…,false} Code for procss i{0,…,n-1} 3 interested[i]:=true 4 await ((test-and-set(lock)=0) OR (interested[i] =false)) 5 Critical Section 6 j:=(i+1) modulo n 7 while (j ≠ i) AND (interested[j] = false) 8 j:=(j+1) modulo n 9 if (j=i) 10 lock:=0 11 else 12 interested[j]:=false 13 interested[i]=false

Answer Q.5b The following scenario results in a deadlock: A process p0 is executed and executes all lines up until the beginning of line 13. (Note that ‘lock’ is freed) Another process p1 is executed and allowed to run all the way through the CS. At this point p1 iterates through the other processes and identifies that ‘interested[0]=true’. As a result p1 resets the value ‘interested[0]:=false’ (line 12) and does not free the lock. It then proceeds to line 13 and exits. p0 runs line 13. At this point the system is in a state of deadlock. Any process attempting to enter the CS will be stopped by line 4. Reductio ad absurdum