preempting scheduler activations
play

Preempting Scheduler Activations Scheduler activations are - PDF document

Preempting Scheduler Activations Scheduler activations are completely preemptable Deadlocks Thomas Plagemann User space With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen Kernel space Preempting Scheduler Activations


  1. Preempting Scheduler Activations � Scheduler activations are completely preemptable Deadlocks Thomas Plagemann User space With slides from C. Griwodz, K. Li, A. Tanenbaum and M. van Steen Kernel space Preempting Scheduler Activations Resources � Maintaining the run queue needs to be a � Examples of computer resources protected critical section � CPU � Let’s use spin locks for protection � Memory � Disk drive Thread 1 Upcall � Tape drives Acquire spin lock � Printers Start queue maintenance � Plotter Interrupt Thread 1 � Loudspeaker Acquire spin lock -> block Resources Resources � Active resource � Processes � Provides a service � Need access to resources in reasonable order � E.g. CPU, network adaptor � Passive resource � Typical way to use a resource � System capabilities that are required by active resources � Request � E.g. memory, network bandwidth � Use � Release � Exclusive resource � Suppose a process holds resource A and requests � Only one process at a time can use it resource B � E.g. loudspeaker, processor � Shared resource � At same time another process holds B and requests A � Can be used by multiple processes � Both are blocked and remain so � E.g. memory, bandwidth 1

  2. Resources Resources � Single resource � Process must wait if acquire block � Exists only once in the system request is denied � E.g. loudspeaker � Requesting process may � Multiple resource be blocked use � Exists several time in the system � May fail with error code � E.g. processor in a multiprocessor system � Preemptable resource � Deadlocks � Resource that can be taken away from a process acquire � Occur only when � E.g. CPU can be taken away from processes in user space processes are granted � Non-preemptable resource exclusive access to � Taking it away will cause processes to fail resources use � E.g. Disk, files Deadlocks Four Conditions for Deadlock � Formal definition : 1. Mutual exclusion condition A set of processes is deadlocked Each resource assigned to 1 process or is available � if each process in the set is waiting for an event 2. Hold and wait condition that only another process in the set can cause Process holding resources can request additional � 3. No preemption condition � Usually the event is release of a currently held Previously granted resources cannot forcibly taken away � resource 4. Circular wait condition � None of the processes can … Must be a circular chain of 2 or more processes � � Run Each is waiting for resource held by next member of the � � Release resources chain � Be awakened Deadlock Modeling Deadlock Example � Modeled with directed graphs � A utility program � A deadlock � Copies a file from a tape to disk A S C T � Prints the file to a A printer � Resources R B U D tape disk printer � Tape � Disk � Printer � Resource R assigned to process A B � Process B is requesting/waiting for resource S � Process C and D are in deadlock over resources T and U 2

  3. Deadlock Modeling Deadlock Modeling � How deadlock occurs � How deadlock can be avoided A requests R A requests R A A B requests S C requests T Requests R Requests R C requests T A requests S Requests S Requests S A requests S B requests S Releases S Releases S B requests T B requests T Releases R Releases R Processes A B C Processes A B C C requests R C requests R B B A releases S Requests S Requests S A releases R Requests T Requests T C releases R Releases T Releases T C releases T Releases S Releases S R S T R S T Resources Resources C C Requests T Requests T Requests R Requests R Releases R Releases R Releases T Releases T Deadlocks: Strategies The Ostrich Algorithm � Ignore the problem � Pretend there is no problem � It is user’s fault � Detection and recovery � Reasonable if � Fix the problem afterwards � Deadlocks occur very rarely � Dynamic avoidance � Cost of prevention is high � Careful allocation � UNIX and Windows take this approach � Prevention � It is a trade-off between � Negate one of the four conditions � Convenience � Correctness Deadlock Detection and Recovery Deadlock Detection and Recovery One Resource of Each Type One Resource of Each Type R A B init notebook L C S D T E add current node CN to L F U V cycle detected is CN two times in L? W G backtracking follow the arc CN = previous node CN = next node is an unmarked � A cycle can be found within the graph, outgoing arc at CN? denoting deadlock 3

  4. Deadlock Detection and Recovery Deadlock Detection and Recovery Multiple Resources of Each Type Multiple Resources of Each Type Existing resources Available resources ( ) ( ) s s r r e e s s E , E , E ,..., E A , A , A ,..., A v s m v s i r i r m r s e r s e d o d o 1 2 3 m 1 2 3 m r n r n e R e R e t n e t n p t a - p t a - o D o D a c a c l C l C T P S T P S 4 2 3 1 2 1 0 0 E=( ) A=( ) Current allocation matrix Request matrix ⎡ ⎤ ⎡ ⎤ C C C ... C R R R ... R 11 12 13 1 11 12 13 1 m m ⎢ ⎥ ⎢ ⎥ Current allocation matrix Request matrix C C C ... C R R R ... R ⎢ ⎥ ⎢ ⎥ 21 22 23 2 m 21 22 23 2 m 0 0 1 0 ⎢ ⎥ ⎢ ⎥ 2 0 0 1 ... ... ... ... ... ... ... ... ... ... ⎢ ⎥ ⎢ ⎥ C= 2 0 0 1 1 0 1 0 R= ⎣ ⎦ ⎣ ⎦ C C C ... C R R R ... R n 1 n 2 n 3 nm n 1 n 2 n 3 nm 0 1 2 0 2 1 0 0 Deadlock Detection and Recovery Deadlock Avoidance Recovery Resource Trajectories B finished � Recovery through preemption release � Take a resource from some other process Printer � Depends on nature of the resource release � Recovery through rollback Plotter � Checkpoint a process periodically request � Use this saved state � Restart the process if it is found deadlocked request � Recovery through killing processes start � Crudest but simplest way to break a deadlock A � Kill one of the processes in the deadlock cycle request request release release � The other processes get its resources Two process Printer � Choose process that can be rerun from the beginning resource trajectories Plotter Deadlock Avoidance Deadlock Avoidance Safe and Unsafe States Safe and Unsafe States has max has max has max has max has max has max has max has max has max A 3 9 A 3 9 A 3 9 A 3 9 A 3 9 A 3 9 A 4 9 A 4 9 A 3 9 B 2 4 B 4 4 B 0 B 0 B 0 B 2 4 B 2 4 B 4 4 B 0 C 2 7 C 2 7 C 2 7 C 2 7 C 2 7 C 2 7 C 7 7 C 0 C 2 7 Free: 3 Free: 1 Free: 5 Free: 0 Free: 7 Free: 3 Free: 2 Free: 0 Free: 4 state is safe state is safe 4

  5. Deadlock Avoidance Deadlock Avoidance Banker’s Algorithm for a Single Resource Banker's Algorithm for a Single Resource � Each process has a credit Resource allocation state � System knows how many resources a process has max has max has max requests at most before releasing resources A 0 6 0 6 6 A 3 5 1 1 6 6 6 A 1 1 6 � Total resources may not satisfy all credits B 0 5 0 5 5 B 5 1 3 1 5 5 B 2 2 5 � Keep track of resources assigned and needed C 4 0 0 4 4 C 4 2 2 4 4 C 2 2 4 � Check on each allocation whether it is safe D 7 0 0 7 7 D 7 4 4 7 7 D 4 4 7 � Safe: there exists a sequence of other states that Free: 10 Free: 1 Free: 6 Free: 5 Free: 4 Free: 2 Free: 2 all processes can terminate correctly Deadlock Detection and Recovery Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources Banker’s Algorithm for Multiple Resources s s r r e e v s s v s s m m i r i r r s e r s e d r o d r o n n e e n R e e n R t t p t a - p t a - o D o D a c a c P l S C P l S C T T E=( 6 3 4 2 ) E=( 6 3 4 2 ) P=( 5 3 2 2 ) P=( 4 2 2 1 ) Assigned resources Resources still needed Assigned resources Resources still needed 1 0 2 0 2 1 2 1 A=( ) A=( ) A 3 0 1 1 A 1 1 0 0 A 3 0 1 1 A 1 1 0 0 B 0 1 0 0 B 0 1 1 2 B 0 1 0 0 B 0 1 1 2 C 1 1 1 0 C 3 1 0 0 C 1 1 1 0 C 3 1 0 0 D 1 1 0 1 D 0 0 1 0 D 0 0 0 0 D - - - - E 0 0 0 0 E 2 1 1 0 E 0 0 0 0 E 2 1 1 0 An example for the deadlock An example for the deadlock detection algorithm detection algorithm Deadlock Detection and Recovery Deadlock Detection and Recovery Banker’s Algorithm for Multiple Resources Banker’s Algorithm for Multiple Resources s s r r e e v s s v s s m m i r i r r s e r s e d r n o d r n o e R e R e n e n t t p t a - p t a - o D o D a c a c T P l S C T P l S C E=( 6 3 4 2 ) E=( 6 3 4 2 ) 1 2 1 0 1 1 1 0 P=( ) P=( ) Assigned resources Resources still needed Assigned resources Resources still needed A=( 5 1 3 2 ) A=( 5 2 3 2 ) A 0 0 0 0 A - - - - A 0 0 0 0 A - - - - B 0 1 0 0 B 0 1 1 2 B 0 0 0 0 B - - - - C 1 1 1 0 C 3 1 0 0 C 1 1 1 0 C 3 1 0 0 D 0 0 0 0 D - - - - D 0 0 0 0 D - - - - E 0 0 0 0 E 2 1 1 0 E 0 0 0 0 E 2 1 1 0 An example for the deadlock An example for the deadlock detection algorithm detection algorithm 5

Recommend


More recommend