Lecture 2 Part 2 Process Synchronization Operating System














- Slides: 14
Lecture 2 Part 2 Process Synchronization Operating System Concepts Essentials – 2 nd Edition Silberschatz, Galvin and Gagne © 2013
Background n Processes can execute concurrently l May be interrupted at any time, partially completing execution n Concurrent access to shared data may result in data inconsistency register 1 = counter register 1 = register 1 + 1 counter = register 1 n Example: counter = 5 l Counter++; //counter = 6 l Counter_ _; //counter = 5 T 0: producer execute register 1 = counter {register 1 = 5} register 2 = counter register 2 = register 2 - 1 counter = register 2 T 1: producer execute register 1 = register 1 + 1 {register 1 = 6} T 2: consumer execute register 2 = counter {register 2 = 5} T 3: consumer execute register 2 = register 2 − 1 {register 2 = 4} T 4: producer execute counter = register 1 {counter = 6} T 5: consumer execute counter = register 2 {counter = 4} n Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Operating System Concepts Essentials – 2 nd Edition 3. 2 Silberschatz, Galvin and Gagne © 2013
Critical Section Problem n Consider system of n processes {p 0, p 1, … pn-1} n Each process has critical section segment of code l Process may be changing common variables, updating table, writing file, etc l Goal: When one process in critical section, no other may be in its critical section n Critical section problem is to design protocol to solve this n Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section Operating System Concepts Essentials – 2 nd Edition 3. 3 Silberschatz, Galvin and Gagne © 2013
Critical Section n General structure of process Pi Operating System Concepts Essentials – 2 nd Edition 3. 4 Silberschatz, Galvin and Gagne © 2013
Solution to Critical-Section Problem 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 2. 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 3. 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 Operating System Concepts Essentials – 2 nd Edition 3. 5 Silberschatz, Galvin and Gagne © 2013
Critical-Section Handling in OS Two approaches depending on if kernel is preemptive or nonpreemptive l Preemptive – allows preemption of process when running in kernel mode l Non-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPU Operating System Concepts Essentials – 2 nd Edition 3. 6 Silberschatz, Galvin and Gagne © 2013
Critical-Section solutions 1. Peterson’s Solution : n Good algorithmic description of solving the problem n Two process solution, Pi, Pj int turn = i ; l boolean flag[2] ; flag[ i ] = true ; flag[ j ] = true ; l 2. Synchronization Hardware : Many systems provide hardware support for implementing the critical section code. All solutions below based on idea of locking l Protecting critical regions via locks l Uniprocessors – could disable interrupts n Modern machines provide special atomic hardware instructions 4 Atomic = non-interruptible l Both solutions old and complex Operating System Concepts Essentials – 2 nd Edition 3. 7 Silberschatz, Galvin and Gagne © 2013
Solution to Critical-section Problem Using Locks do { acquire lock critical section release lock remainder section } while (TRUE); Operating System Concepts Essentials – 2 nd Edition 3. 8 Silberschatz, Galvin and Gagne © 2013
3. Mutex Locks n OS designers build software tools to solve critical section problem n Simplest is mutex lock n Protect a critical section by first acquire() a lock then release() the lock l Boolean variable indicating if lock is available or not n Calls to acquire() and release() must be atomic Usually implemented via hardware atomic instructions n But this solution requires busy waiting n This lock therefore called a spinlock l Operating System Concepts Essentials – 2 nd Edition 3. 9 Silberschatz, Galvin and Gagne © 2013
acquire() and release() n acquire() { while (!available) ; /* busy wait */ available = false; ; } n release() { available = true; } n do { acquire lock critical section release lock remainder section } while (true); Operating System Concepts Essentials – 2 nd Edition 3. 10 Silberschatz, Galvin and Gagne © 2013
4. Semaphore n Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer variable n Can only be accessed via two indivisible (atomic) operations n wait() and signal() 4 Originally called P() and V() n Definition of the wait() operation wait(S) { l while (S <= 0) ; // busy wait S--; } n Definition of the signal() operation signal(S) { S++; } Operating System Concepts Essentials – 2 nd Edition 3. 11 Silberschatz, Galvin and Gagne © 2013
Semaphore Usage n Counting semaphore – integer value can range over an unrestricted domain n Binary semaphore – integer value can range only between 0 and 1 l Same as a mutex lock n Can solve various synchronization problems n Consider P 1 and P 2 that require M 1 to happen before M 2 Create a semaphore “synch” initialized to 0 P 1: M 1 ; signal(synch); // execute synch P 2: wait(synch); // wait for synch M 2 ; n Can implement a counting semaphore S as a binary semaphore Operating System Concepts Essentials – 2 nd Edition 3. 12 Silberschatz, Galvin and Gagne © 2013
n https: //www. youtube. com/watch? v=i. CIM_je. QZE 4 Operating System Concepts Essentials – 2 nd Edition 3. 13 Silberschatz, Galvin and Gagne © 2013
Deadlock and Starvation n Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes n Let S and Q be two semaphores initialized to 1 P 0 P 1 wait(S); wait(Q); . . . signal(S); signal(Q); wait(S); . . . signal(Q); signal(S); n Starvation – indefinite blocking l A process may never be removed from the semaphore queue in which it is suspended. Operating System Concepts Essentials – 2 nd Edition 3. 14 Silberschatz, Galvin and Gagne © 2013