310 likes | 543 Views
Monitors. High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name { shared variable declarations procedure body P1 (…) { . . . } procedure body P2 (…) { . . . }
E N D
Monitors • High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name { shared variable declarations procedure bodyP1(…) { . . . } procedurebodyP2 (…) { . . . } procedure bodyPn(…) { . . . } { initialization code } }
Monitors • To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; • Condition variable can only be used with the operations wait and signal. • The operation x.wait();means that the process invoking this operation is suspended until another process invokes x.signal(); • The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect.
Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // following slides void putdown(int i) // following slides void test(int i) // following slides void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } }
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); } void test(int i) { if ( (state[(I + 4) % 5] != eating) && (state[i] == hungry)&& (state[(i + 1) % 5] != eating)) { state[i] = eating; self[i].signal(); } } Dining Philosophers
The POSIX API for Synchronization –Semaphores #include <semaphores.h> int sem_init(sem_t *sem,int pshared,unsigned int value); int sem_wait(sem_t *sem); int sem_post(sem_t *sem); int sem_destroy(sem_t *sem);
Mutexes #include <semaphores.h> int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); intpthread_mutex_lock(pthread_mutex_t * mutex); intpthread_mutex_unlock(pthread_mutex_t * mutex); intpthread_mutex_destroy(pthread_mutex_t * mutex);
Condition variables int pthread_cond_init (pthread_cond_t *cptr, pthread_condattr_t *attr); int pthread_cond_wait (pthread_cond_t *cptr, pthread_mutex_t *mptr); int pthread_cond_signal (pthread_cond_t *cptr); int pthread_cond_broadcast (pthread_cond_t *cptr); int pthread_cond_destroy (pthread_cond_t *cptr);
Methods of Handling Deadlocks • 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, …, P0} 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 P0 is waiting for a resource that is held by P0.
Resource-Allocation Graph • ProcessResource Type with 4 instances • Pirequests instance of Rj • Pi is holding an instance of Rj Pi Rj Pi Rj
Example of a Resource Allocation Graph No Deadlock DeadLock
Resource Allocation Graph With A Cycle But No Deadlock • 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.
So what can we do ? • Ensure that the system will never enter a deadlock state. • 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.
Deadlock Prevention • Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. • Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. • Low resource utilization; starvation possible
Deadlock Prevention • No Preemption – • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. • Preempted resources are added to the list of resources for which the process is waiting. • Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. • Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. prevention strategies are not practical
Deadlock Avoidance • Requires that the system has some additional a priori information available. • Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need • The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. • When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. • System is in safe state if there exists a safe sequence of all processes. • Avoidance ensure that a system will never enter an unsafe state.
Banker’s Algorithm – Assumptions • Each process must a priori claim maximum use. • When a process requests a resource it may have to wait. • When a process gets all its resources it must return them in a finite amount of time
State after P4 runs to completion A safe state! Grant P2 an R3
Deadlock Detection • OS does not prevent deadlocks. • OS grants resources whenever possible. • OS checks for deadlock by checking for circular waiting by either method: • Checking at resource request • early detection of deadlock • frequent checks consume processor time • Checking periodically
Deadlock detection – Step3: P1&P2 cannot continue P1&P2 must have a deadlock!
OS options after a deadlock is detected • Abort all deadlocked processes • Back up all deadlocked processes to some previously defined checkpoint • Successively abort deadlocked processes until deadlock no longer exists • Successively preempt resources until deadlock no longer exists
OS options: which deadlocked process to abort? • Least amount of processor time consumed so far • Least number of lines of output produced so far • Most estimated time remaining • Least total resources allocated so far • Lowest priority