Presentation is loading. Please wait.

Presentation is loading. Please wait.

Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {

Similar presentations


Presentation on theme: "Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {"— Presentation transcript:

1 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 (…) {... } procedure body Pn (…) {... } { initialization code }

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

3 Schematic View of a Monitor

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

5 Dining Philosophers 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(); }

6 The POSIX API for Synchronization – Semaphores #include 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);

7 Mutexes #include int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); int pthread_mutex_lock(pthread_mutex_t * mutex); int pthread_mutex_unlock(pthread_mutex_t * mutex); int pthread_mutex_destroy(pthread_mutex_t * mutex);

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

9 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 {P 0, P 1, …, P 0 } 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 0 is waiting for a resource that is held by P 0.

10 Resource-Allocation Graph 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

11 Example of a Resource Allocation Graph No DeadlockDeadLock

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

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

14 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

15 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

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

17 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

18 Banker’s Algorithm Example -P2 requests 1 R3 R1R2R3R1R2R3R1R2R3 P1322 100936 P2613 611Resources P3314 211 P4422 002R1R2R3 Claim matrix Allocation matrix 012 Available vector

19 Assume request is granted. Is it ‘safe’ or not? R1R2R3R1R2R3R1R2R3 P1322 100936 P2613 612Resources P3314 211 P4422 002R1R2R3 Claim matrix Allocation matrix 011 Available vector

20 P1 cannot go first. Resources not enough R1R2R3R1R2R3R1R2R3 P1322 100936 P2613 612Resources P3314 211 P4422 002R1R2R3 Claim matrix Allocation matrix 011 Available vector

21 State after P2 runs to completion R1R2R3R1R2R3R1R2R3 P1322 100936 P2000 000Resources P3314 211 P4422 002R1R2R3 Claim matrix Allocation matrix 623 Available vector

22 State after P1 runs to completion R1R2R3R1R2R3R1R2R3 P1000 000936 P2000 000Resources P3314 211 P4422 002R1R2R3 Claim matrix Allocation matrix 723 Available vector

23 State after P3 runs to completion R1R2R3R1R2R3R1R2R3 P1000 000936 P2000 000Resources P3000 000 P4422 002R1R2R3 Claim matrix Allocation matrix 934 Available vector

24 State after P4 runs to completion R1R2R3R1R2R3R1R2R3 P1000 000936 P2000 000Resources P3000 000 P4000 000R1R2R3 Claim matrix Allocation matrix 936 Available vector A safe state! Grant P2 an R3

25 Deadlock Detection OS does not prevent deadlocks. OS grants resources whenever possible. OS checks for deadlock by checking for circular waiting by either method: 1. Checking at resource request early detection of deadlock frequent checks consume processor time 2. Checking periodically

26 Deadlock detection – example - current situation R1R2R3R4R5R1R2R3R4R5R1R2R3R4R5 P101001 1011021121 P200101 11000 Resource vector P300001 00010 P410101 00000R1R2R3R4R5 Requests (these are actual requests not theoretical claims as in Bankers algorithm) Allocation 00001 Available vector

27 Deadlock detection – Step1: Mark P4 R1R2R3R4R5R1R2R3R4R5R1R2R3R4R5 P101001 1011021121 P200101 11000Resource vector P300001 00010 P410101 00000R1R2R3R4R5 RequestsAllocation00001 Available vector

28 Deadlock detection – Step2: P3 is able to continue R1R2R3R4R5R1R2R3R4R5R1R2R3R4R5 P101001 1011021121 P200101 11000Resource vector P300001 00000 P410101 00000R1R2R3R4R5 RequestsAllocation00011 Available vector

29 Deadlock detection – Step3: P1&P2 cannot continue R1R2R3R4R5R1R2R3R4R5R1R2R3R4R5 P101001 1011021121 P200101 11000Resource vector P300001 00000 P410101 00000R1R2R3R4R5 RequestsAllocation00011 Available vector P1&P2 must have a deadlock!

30 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

31 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


Download ppt "Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {"

Similar presentations


Ads by Google