5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Problems with Semaphores Incorrect use of semaphore operations:

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks Adapted by Donghui Zhang from the original version by Silberschatz et al.
Advertisements

Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 27 CPU Simulator III.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
Chapter 6 Process Synchronization: Part 2. Problems with Semaphores Correct use of semaphore operations may not be easy: –Suppose semaphore variable called.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Deadlocks  (How to Detect Them and Avoid Them) A:
Chapter 7 Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks 7.1 System Model.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
Modified from Silberschatz, Galvin and Gagne Lecture 13 Chapter 7: Deadlocks.
02/18/2008CSCI 315 Operating Systems Design1 Deadlock Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Chapter 7.1: Deadlocks.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Bridge Crossing Example Traffic only in one direction. Each section.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Objectives Understand the Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 8: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks Modified.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7 Deadlocks. 7.2 Modified By Dr. Khaled Wassif Operating System Concepts – 7 th Edition Silberschatz, Galvin and Gagne ©2005 Chapter 7: Deadlocks.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 28 Handling Deadlock.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Deadlocks System Model RAG Deadlock Characterization
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 7 Deadlocks Spring 2015 COMP 4850 Introduction to.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Deadlock. Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
7.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 7: Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Deadlock and Starvation
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization
Deadlock and Starvation
Chapter 7: Deadlocks Source & Copyright: Operating System Concepts, Silberschatz, Galvin and Gagne.
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Chapter 5: Process Synchronization (Con’t)
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Deadlocks.
Chapter 7: Synchronization Examples
Deadlocks Session - 13.
Lecture 26 Syed Mansoor Sarwar
Presentation transcript:

5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Problems with Semaphores Incorrect use of semaphore operations: signal (mutex) …. wait (mutex) wait (mutex) … wait (mutex) Omitting of wait (mutex) or signal (mutex) (or both) Deadlock and starvation are possible.

5.2 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Abstract data type, internal variables only accessible by code within the procedure Only one process may be active within the monitor at a time monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } procedure Pn (…) {……} Initialization code (…) { … } }

5.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Schematic view of a Monitor

5.4 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Condition Variables condition x, y ; Two operations are allowed on a condition variable: x.wait() – a process that invokes the operation is suspended until x.signal() x.signal() – resumes one of processes (if any) that invoked x.wait()  If no x.wait() on the variable, then it has no effect on the variable

5.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Monitor with Condition Variables

5.6 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Condition Variables Choices If process P invokes x.signal(), and process Q is suspended in x.wait(), what should happen next? Both Q and P cannot execute in parallel. If Q is resumed, then P must wait Options include Signal and wait – P waits until Q either leaves the monitor or it waits for another condition Signal and continue – Q waits until P either leaves the monitor or it waits for another condition Both have pros and cons – language implementer can decide Monitors implemented in Concurrent Pascal compromise  P executing signal immediately leaves the monitor, Q is resumed Implemented in other languages including Mesa, C#, Java

5.7 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Monitor Solution to Dining Philosophers monitor DiningPhilosophers { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); }

5.8 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Solution to Dining Philosophers (Cont.) void test (int i) { if ((state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; }

5.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Each philosopher i invokes the operations pickup() and putdown() in the following sequence: DiningPhilosophers.pickup(i) ; EAT DiningPhilosophers.putdown(i) ; No deadlock, but starvation is possible Solution to Dining Philosophers (Cont.)

5.10 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Linux Synchronization Linux: Prior to kernel Version 2.6, disables interrupts to implement short critical sections Version 2.6 and later, fully preemptive Linux provides: Semaphores Atomic integers spinlocks On single-cpu system, spinlocks replaced by enabling and disabling kernel preemption

5.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Pthreads Synchronization Pthreads API is OS-independent It provides: mutex locks condition variables

5.12 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Pthread Mutex locks pthread_mutex_lock: locks mutex. blocks if mutex is already locked, and remains blocked until mutex is unlocked. pthread_mutex_unlock: unlocks mutex. pthread_mutex_trylock: similar to lock but does not block if mutex is already locked. Returns EBUSY if already locked. pthread_mutex_destroy: mutex becomes unitialized, user must explicitly free its memory. All return 0 if successful. Return >0 if error.

5.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Pthread Mutex locks #include #include … pthread_mutex_t mutex; ///< declare global (i.e., not inside of any function) … //perform this one-time initialization (usually in main) int ret = pthread_mutex_init( &mutex, NULL ); //default attributes if (ret) { perror( "main: mutex init error" ); exit(-1); } … //lock in thread code ret = pthread_mutex_lock( &mutex ); if (ret) { printf( “mutex lock error \n"); } //critical section here //critical section here //unlock in thread code pthread_mutex_unlock( &mutex );

5.14 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition #include #include #include #define NUM_THREADS 10 void *functionC(); //static initialization, unlocked pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; main() { pthread_t threads[NUM_THREADS]; int rc1; int i; /* Create independant threads each of which will execute functionC */ for ( i = 0; i < NUM_THREADS; i++ ) { if( (rc1 = pthread_create( &threads[i], NULL, &functionC, NULL)) ) { printf("Thread creation failed: %d\n", rc1); } } /* Wait till threads are complete before main continues. */ for ( i = 0; i < NUM_THREADS; i++ ) { pthread_join( threads[i], NULL); } printf( "Final value of counter is: %i\n", counter ); exit(0); }

5.15 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Cont’d void *functionC() { int x; pthread_mutex_lock( &mutex1 ); x = counter; x = x + 1; counter = x; printf("Counter value: %d\n",counter); pthread_mutex_unlock( &mutex1 ); }

5.16 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition output Counter value: 1 Counter value: 2 Counter value: 3 Counter value: 4 Counter value: 5 Counter value: 6 Counter value: 7 Counter value: 8 Counter value: 9 Counter value: 10 Final value of counter is: 10

5.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Replace functionC as below and see output void *functionC() { int x; x = counter; sleep( 1 ); x = x + 1; counter = x; printf("Counter value: %d\n",counter); } Output1: Counter value: 1 Counter value: 1 Counter value: 1 Counter value: 1 Counter value: 1 Counter value: 1 Counter value: 1 Counter value: 1 Counter value: 1 Counter value: 1 Final value of counter is: 1

5.18 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition POSIX SEM extension #include #include … sem_t sem; ///< declare global (i.e., not inside of any function) … /* create the semaphore and initialize to 1 */ /*argument 2: 0 means share within threads of the same process */ /*argument 3: initial value is 1*/ sem_init(&sem, 0, 1); … sem_wait( &sem ); //critical section here //critical section here sem_post( &sem ); /* signal */

5.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set Example System has 2 disk drives P 1 and P 2 each hold one disk drive and each needs another one Example semaphores A and B, initialized to 1 P 0 P 1 wait (A); wait(B) wait (B); wait(A)

5.20 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Bridge Crossing Example Traffic only in one direction Each section of a bridge can be viewed as a resource If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback) Several cars may have to be backed up if a deadlock occurs Starvation is possible Note – Most OSes do not prevent or deal with deadlocks

5.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Deadlock Example /* thread one runs in this function */ void *do_work_one(void *param) { pthread_mutex_lock(&first_mutex); pthread_mutex_lock(&second_mutex); /** * Do some work */ pthread_mutex_unlock(&second_mutex); pthread_mutex_unlock(&first_mutex); pthread_exit(0); } /* thread two runs in this function */ void *do_work_two(void *param) { pthread_mutex_lock(&second_mutex); pthread_mutex_lock(&first_mutex); /** * Do some work */ pthread_mutex_unlock(&first_mutex); pthread_mutex_unlock(&second_mutex); pthread_exit(0); }

5.22 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Deadlock Example with Lock Ordering void transaction(Account from, Account to, double amount) { mutex lock1, lock2; lock1 = get_lock(from); lock2 = get_lock(to); acquire(lock1); acquire(lock2); withdraw(from, amount); deposit(to, amount); release(lock2); release(lock1); } Transactions 1 and 2 execute concurrently. Transaction 1 transfers $25 from account A to account B, and Transaction 2 transfers $50 from account B to account A

5.23 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Deadlock Characterization Mutual exclusion: only one process at a time can use a resource Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task Circular wait: there exists a set {P 0, P 1, …, P n } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0. Deadlock can arise if four conditions hold simultaneously.

5.24 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Resource-Allocation Graph V is partitioned into two types: P = {P 1, P 2, …, P n }, the set consisting of all the processes in the system R = {R 1, R 2, …, R m }, the set consisting of all resource types in the system request edge – directed edge P i  R j assignment edge – directed edge R j  P i A set of vertices V and a set of edges E.

5.25 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Resource-Allocation Graph (Cont.) Process Resource Type with 4 instances P i requests instance of R j P i is holding an instance of R j PiPi PiPi RjRj RjRj

5.26 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Example of a Resource Allocation Graph

5.27 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Resource Allocation Graph With A Deadlock

5.28 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Graph With A Cycle But No Deadlock

5.29 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Basic Facts If graph contains no cycles  no deadlock If graph contains a cycle  if only one instance per resource type, then deadlock if several instances per resource type, possibility of deadlock

5.30 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Methods for Handling Deadlocks Ensure that the system will never enter a deadlock state: Deadlock prevention Deadlock avoidence Allow the system to enter a deadlock state and then recover Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX