5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition...

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

Transcript of 5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition...

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

5.1 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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.

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

5.2 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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 declarationsprocedure P1 (…) { …. }

procedure Pn (…) {……}

Initialization code (…) { … }}

}

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

5.3 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Schematic view of a Monitor

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

5.4 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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

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

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

Monitor with Condition Variables

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

5.6 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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

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

5.7 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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);

}

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

5.8 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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; }

}

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

5.9 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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.)

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

5.10 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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

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

5.11 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Pthreads Synchronization

Pthreads API is OS-independent

It provides:

mutex locks

condition variables

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

5.12 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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.

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

5.13 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Pthread Mutex locks

#include <errno.h>#include <errno.h>

#include <pthread.h>#include <pthread.h>

……

pthread_mutex_t mutex; ///< declare global (i.e., not inside of any function)pthread_mutex_t mutex; ///< declare global (i.e., not inside of any function)

……

//perform this one-time initialization (usually in main)//perform this one-time initialization (usually in main)

int ret = pthread_mutex_init( &mutex, NULL ); //default attributesint ret = pthread_mutex_init( &mutex, NULL ); //default attributes

if (ret) { perror( "main: mutex init error" ); exit(-1); }if (ret) { perror( "main: mutex init error" ); exit(-1); }

……

//lock in thread code//lock in thread code

ret = pthread_mutex_lock( &mutex );ret = pthread_mutex_lock( &mutex );

if (ret) { printf( “mutex lock error \n"); }if (ret) { printf( “mutex lock error \n"); }

//critical section here//critical section here

//unlock in thread code//unlock in thread code

pthread_mutex_unlock( &mutex );pthread_mutex_unlock( &mutex );

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

5.14 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

#include <stdio.h>#include <stdlib.h>#include <pthread.h>#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);}

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

5.15 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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 );}

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

5.16 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

output

Counter value: 1Counter value: 2Counter value: 3Counter value: 4Counter value: 5Counter value: 6Counter value: 7Counter value: 8Counter value: 9Counter value: 10Final value of counter is: 10

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

5.17 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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: 1Counter value: 1Counter value: 1Counter value: 1Counter value: 1Counter value: 1Counter value: 1Counter value: 1Counter value: 1Counter value: 1Final value of counter is: 1

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

5.18 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

POSIX SEM extension

#include <semaphore.h>#include <semaphore.h>

……

sem_t sem; ///< declare global (i.e., not inside of any function)sem_t sem; ///< declare global (i.e., not inside of any function)

……

/* create the semaphore and initialize to 1 *//* create the semaphore and initialize to 1 */

/*argument 2: 0 means share within threads of the same process *//*argument 2: 0 means share within threads of the same process */

/*argument 3: initial value is 1*//*argument 3: initial value is 1*/

sem_init(&sem, 0, 1); sem_init(&sem, 0, 1); ……

sem_wait( &sem );sem_wait( &sem );

//critical section here//critical section here

sem_post( &sem ); /* signal */sem_post( &sem ); /* signal */

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

5.19 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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

P1 and P2 each hold one disk drive and each needs another one

Example

semaphores A and B, initialized to 1

P0 P1

wait (A); wait(B)

wait (B); wait(A)

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

5.20 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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

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

5.21 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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);

}

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

5.22 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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

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

5.23 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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 {P0, P1, …, Pn} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

Deadlock can arise if four conditions hold simultaneously.

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

5.24 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Resource-Allocation Graph

V is partitioned into two types:

P = {P1, P2, …, Pn}, the set consisting of all the processes in the system

R = {R1, R2, …, Rm}, the set consisting of all resource types in the system

request edge – directed edge Pi Rj

assignment edge – directed edge Rj Pi

A set of vertices V and a set of edges E.

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

5.25 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Resource-Allocation Graph (Cont.)

Process

Resource Type with 4 instances

Pi requests instance of Rj

Pi is holding an instance of Rj

Pi

Pi

Rj

Rj

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

5.26 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Example of a Resource Allocation Graph

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

5.27 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Resource Allocation Graph With A Deadlock

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

5.28 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th Edition

Graph With A Cycle But No Deadlock

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

5.29 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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

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

5.30 Silberschatz, Galvin and Gagne ©2013Operating System Concepts Essentials – 9th 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