Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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.

2 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 (…) { … } }

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

4 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.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Monitor with Condition Variables

6 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

7 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); }

8 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; }

9 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.)

10 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

11 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

12 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.

13 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 );

14 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); }

15 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 ); }

16 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

17 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

18 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 */

19 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)

20 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

21 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); }

22 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

23 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.

24 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.

25 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

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

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

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

29 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

30 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


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

Similar presentations


Ads by Google