Operating System Concepts and Techniques Lecture 16 Deadlock and starvation-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques,

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 7: Deadlocks.
Deadlock CS Introduction to Operating Systems.
7.5 Deadlock Avoidance The algorithm is simply to ensure that the system will always remain in safe state. Therefore, if a process requests a resource.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 7: Deadlocks.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 7 – Deadlock and Indefinite Postponement Outline 7.1 Introduction 7.2Examples of Deadlock.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Tanenbaum Ch 6 Silberschatz Ch 7
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
DPNM Lab. Dept. of CSE, POSTECH
Classical Problems of Concurrency
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 7 – Deadlock and Indefinite Postponement
Deadlock Detection with One Resource of Each Type (1)
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 14, 2005 Chapter 7: Deadlocks The Deadlock.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
Chapter 7: Deadlocks Adapted to COP4610 by Robert van Engelen.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance 3.6. Deadlock prevention.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems (OS)
Deadlocks Silberschatz Ch. 7 and Priority Inversion Problems.
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
CHAPTER 8: DEADLOCKS System Model Deadlock Characterization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Chapter 7 Deadlocks. 7.1 Introduction Deadlock Common approaches to deal with deadlock – Prevention – Avoidance – Detection and recovery.
Operating System Concepts and Techniques Lecture 15 Deadlock and starvation-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques,
Lecture 12 Handling Deadlock – Prevention, avoidance and detection.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 7 Operating Systems.
Chapter 7: Deadlocks. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Deadlocks Introduction to Operating Systems: Module 7.
Deadlocks Copyright ©: University of Illinois CS 241 Staff1.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
Deadlock CS Introduction to Operating Systems.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
Synchronization Deadlocks and prevention
Chapter 7: Deadlocks.
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Deadlock Detection & recovery
Chapter 7 – Deadlock and Indefinite Postponement
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Operating System 6 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CENG334 Introduction to Operating Systems
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Presentation transcript:

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., To order: or

Banker’s algorithm for multiple resources 1. 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 2. Suppose that all the requested resources are given to the process (it is not actually given) 3. 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 4. If all processes are marked it is safe to allocate the requested resources; otherwise declare that the allocation is unsafe and return 2

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 3

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 4

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 5

Circular wait detection example 1. Process 1 requests CD drive 2. Process 2 requests speaker set 3. Process 1 requests speaker set 4. 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 6 Process IDWaiting forOther Attributes 1speaker set 2 Process IDWaiting forOther attributes 1 CD drive 2 speaker set

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 7

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

Table set up

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 } 10

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

Attempt 2 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_fork(i);// Put left fork down put_fork((i+1)%n);// Put right fork down } No deadlock, but starvation persists 12

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

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

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

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 16

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?