Click here to load reader

7.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods

  • View
    221

  • Download
    1

Embed Size (px)

Text of 7.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 7: Deadlocks The...

  • Slide 1
  • 7.1 Silberschatz, Galvin and Gagne 2005 Operating System Principles Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection Recovery from Deadlock
  • Slide 2
  • 7.2 Silberschatz, Galvin and Gagne 2005 Operating System Principles Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks To present a number of different methods for preventing or avoiding deadlocks in a computer system.
  • Slide 3
  • 7.3 Silberschatz, Galvin and Gagne 2005 Operating System Principles The Deadlock Problem Definition: 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. P 1 and P 2 each hold one disk drive and each needs another one. Example semaphores A and B, initialized to 1 P 0 P 1 wait (A);wait(B); wait (B);wait(A); signal(A); signal(B); signal(B); signal(A);
  • Slide 4
  • 7.4 Silberschatz, Galvin and Gagne 2005 Operating System Principles 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.
  • Slide 5
  • 7.5 Silberschatz, Galvin and Gagne 2005 Operating System Principles 7.1 System Model Resource types R 1, R 2,..., R m CPU cycles, memory space, I/O devices, files, semaphores Each resource type R i has W i instances. If a process request an instance of a resource type, the allocation of any instance of the type will satisfy the request Each process utilizes a resource as follows: 1. Request: open( ), allocate( ), wait( ) 2. use 3. Release: close( ), free( ), signal( )
  • Slide 6
  • 7.6 Silberschatz, Galvin and Gagne 2005 Operating System Principles Deadlock in Multi-thread Applications Multiple threads can compete for shared resources pthread_mutex_t first_mutex; pthread_mutex_t second_mutex; pthread_mutex_init(&first_mutex, NULL); pthread_mutex_init(&second_mutex, NULL); initialization 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_one 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); } thread_two
  • Slide 7
  • 7.7 Silberschatz, Galvin and Gagne 2005 Operating System Principles 7.2 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 n } 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 n1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0. Deadlock can arise if four necessary conditions hold simultaneously:
  • Slide 8
  • 7.8 Silberschatz, Galvin and Gagne 2005 Operating System Principles Resource-Allocation Graph V is partitioned into two types: P = {P 1, P 2, , P n }, the set consisting of all the processes in the system. R = {R 1, R 2, , R m }, the set consisting of all resource types in the system. Each instance is represented as a dot inside the rectangle. request edge directed edge P i R j assignment edge directed edge R j P i, and one of the dots in the rectangle must be designated It is a set of vertices V and a set of edges E.