1 / 18

Operating System Concepts and Techniques Lecture 16

This lecture discusses deadlock and starvation in operating systems, covering concepts such as the Banker's algorithm, deadlock avoidance, detection and recovery, circular wait detection, and examples like the Dining Philosophers problem.

Download Presentation

Operating System Concepts and Techniques Lecture 16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating System Concepts and Techniques Lecture 16 Deadlock and starvation-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011. To order: www.iUniverse.com, www.barnesandnoble.com, or www.amazon.com

  2. Banker’s algorithm for multiple resources • If the number of requested resources from each type is greater than the number of available resources of the corresponding type the request is not valid return from this algorithm; otherwise continue with the next step • Suppose that all the requested resources are given to the process (it is not actually given) • See if there is a process that can complete with the available resources; if there is one, mark it as completed; suppose that all of its resources are taken back and are added to available resources; repeat this step for unmarked processes until there are no more unmarked processes or there is no process left that can complete its job with the available resources • If all processes are marked it is safe to allocate the requested resources; otherwise declare that the allocation is unsafe and return

  3. Deadlock avoidance adv/disadv • Advantage • Deadlock will not occur • Disadvantage • Every process has to declare its maximum simultaneous needs in advance • Has to process every request, takes time

  4. Deadlock Detection and Recovery • Allow deadlock to occur, but use a monitoring process, detect it and notify the system • The system recovers from this situation by killing an involved process (or rolling back one of the transactions) • To detect, a method for detecting circular wait is needed

  5. Circular wait detection algorithm Step 1. If the resource r is available, allocate it to Process p and place p in the possessing field of this resource in the resource table. A circular-wait cannot occur when the required resource is available. Return from this algorithm; otherwise continue Step 2. Place r in the “waiting for” field of Process p in the process table. Step 3. Assign p to p1 and r to r1. Step 4. Look at the resource table and assign the process that possesses resource r1 to p2. If p2 is the same as p, a circular wait has occurred. Announce this and return from this algorithm; otherwise go to the process table; if process p2 is not waiting for a resource, a circular wait does not exist; return from this algorithm; Otherwise assign the resource for which p2 is waiting for, to r2. Step 5. Replace p1 by p2 and r1 by r2; repeat from Step 4 on Brief: Go back can forth between process table and resource table

  6. Circular wait detection example 1. Process 1 requests CD drive 2. Process 2 requests speaker set 3. Process 1 requests speaker set • Process 2 requests CD drive • Below tables are after processing request 3, let’s process request 4 Process 2 waits for CD (process table)  CD is given to process 1 (resource table)  Process 1 waits for speaker set (process table)  P2 has the speaker set  circular wait

  7. Starvation • Starvation is the condition in which a process requires some resources but will not ever receive it, however the cause is not a closed circular wait • For example: in a priority-based system, the scheduler always picks the task with the highest priority, therefore, the execution of a low priority task may be delayed indefinitely, if new higher priority tasks keep arriving at a rate that gives no chance to the lower priority task to execute

  8. Dining philosophers • The dining philosopher problem, though comical and unreal, is one of the simplest problems that best conveys the difficulty of designing deadlock and starvation-free solutions to IPC problems in modern operating systems • A round table with five chairs, five spaghetti plates, and five forks is set • Five philosophers sit and each has his own spaghetti plate automatically refilled as the spaghetti is eaten • Each philosopher (process) has two states, eat, think • A philosopher cannot continuously eat or think and must change his state every once in a while • Every philosopher has to use two forks to eat, his left and right forks (shared resources)

  9. 0 1 0 1 4 2 4 2 3 3 Table set up

  10. Attempt 1 void philosopher (int i) // i =0,1,…,n-1, is the philosopher number { while (1) { // Do this loop forever think; // Think for a while grab_fork(i); // Grab left fork grab_fork((i+1)%n); // Grab right fork eat; // Eat as you like for a limited time put_fork(i); // Put left fork put_fork((i+1)%n); // Put right fork } }

  11. Attempt 1… • In the multiprogramming environment, it is possible that all philosopher get to the point where each one has taken the left fork and tries to take the right fork  circular wait • In another scenario, Processes 0 and 3, on the one hand, and 2 and 4, on the other hand, are so synchronized that whenever 0 and 3 are eating, 2 and 4 are thinking and as soon as 0 and 3 put down their forks, 2 and 4 grab their forks immediately and start eating. Also, whenever 2 and 4 put down their forks, 0 and 3 grab them immediately and start eating. If this cycle repeats forever, Philosopher 1 will starve to death and, thus, a starvation state is possible.

  12. Attempt 2 void philosopher (inti) // i =0,1,…,n-1, is the philosopher number { while (1) { // Do this loop forever think; // Think for a while grab_forks(i); // Grab both forks eat; // Eat as you like put_fork(i); // Put left fork down put_fork((i+1)%n); // Put right fork down } } • No deadlock, but starvation persists

  13. Dining philosopher’s solution #define n 5 semaphore mutex=1; semaphore available[n]={0}; // All elements of available are set to zero int forks[n] ; int waiting[n]; // Is the philosopher waiting? void main (void) { int pid; for (int i=0; i<n; i++) forks[i] = 1; for (int i=0; i<n; i++) { pid = fork(); // Create one philosopher process if (pid==0)philosopher(i); // This is the created philosopher } }

  14. Dining philosopher’s solution... void philosopher (int i) // i =0,1,…,n-1, is the philosopher number { while (1) { // Do this loop forever think; // Think for a while grab_forks(i); // Grab both forks eat; // Eat as you like put_forkL(i); // Put left fork down put_forkR(i); // Put right fork down } } void grab_forks (int i) { int success = 0; // To be set to one if both forks are available down (&mutex); // Forks are protected waiting[i]=0; // To be set to one if philosopher has to wait if (fork[i] && fork[(i+1)%n] { // If both forks are available fork[i] = 0; // Grab left fork fork[(i+1)%n] = 0; // Grab right fork success = 1; } else waiting[i]=1; // The philosopher has to wait up (&mutex); // Leave forks critical region if (!success) down (&available[i]); // The philosopher has to await forks } 

  15. Dining philosopher’s solution… void put_forkL (int i) { down (&mutex) // Forks are protected if (waiting[(i-1)%n] && fork[(i-1)%n]) { fork[(i-1)%n]=0; waiting[(i-1)%n]=0; up(&available[(i-1)%n]); } else fork[i] = 1; // Return this fork up (&mutex); // Leave forks critical region } void put_forkR (int i) { down (&mutex) // Forks are protected if (waiting[(i+1)%n] && fork[(i+2)%n]) { fork[(i+2)%n]=0; waiting[(i+1)%n] = 0; up(&available[(i+1)%n]); } else fork[(i+1)%n] = 1; // Return this fork up (&mutex); // Leave forks critical region }

  16. Summary • Banker’s algorithm could also be used for systems with many types of resources • The most common method concerning deadlock is deadlock detection and recovery, especially for systems which support transactions • Deadlock should not include all processes, but it has to include at least two processes • Starvation was another concept which was discussed in this chapter • Using the dining philosophers problem as an example, a deadlock-free and starvation-free solution was investigated

  17. Find out • The exact differences between deadlock and starvation • Two more classic problems on deadlock and starvation similar to dining philosophers • Other solutions for dining philosophers problem • Cycles in the process-resource wait graph which are not circular wait cycles • Whether the shortest job next policy could cause deadlock • Policies other than priority scheduling which can cause starvation

  18. Any questions?

More Related