Computer Operating Systems Lecture PDF

Summary

This lecture covers fundamental concepts in computer operating systems, focusing on resource management and deadlock avoidance techniques. The Banker's Algorithm is presented as a method for preventing deadlocks in systems with multiple resource types and processes.

Full Transcript

Computer Operating Systems Computer Operating Systems 1 Course Reference  Textbook: A. Silberschatz, P. Galvin, and G. Gagne, Operating Systems Concepts, 10th Edition, Wiley, 2018, ISBN: 978-1-119-32091-3. Computer Operating Systems...

Computer Operating Systems Computer Operating Systems 1 Course Reference  Textbook: A. Silberschatz, P. Galvin, and G. Gagne, Operating Systems Concepts, 10th Edition, Wiley, 2018, ISBN: 978-1-119-32091-3. Computer Operating Systems 2 Background  A computer system consists of a finite number of resources to be distributed among a number of competing processes.  An operating system is a resource allocator i.e., there are many resources that can be allocated to only one process at time.  Resource types R1, R2,..., Rm CPU cycles, memory space, I/O devices  Each resource type Ri has Wi instances.  Each process or thread utilizes a resource as follows: request use release Computer Operating Systems 3 Deadlock Characterization Deadlock can arise if four conditions hold simultaneously.  Mutual exclusion: only one thread at a time can use a resource  Hold and wait: a thread holding at least one resource is waiting to acquire additional resources held by other threads  No Preemption: a resource can be released only voluntarily by the thread holding it, after that thread has completed its task  Circular wait: there exists a set {T0, T1, …, Tn} of waiting threads such that T0 is waiting for a resource that is held by T1, T1 is waiting for a resource that is held by T2, …, Tn–1 is waiting for a resource that is held by Tn, and Tn is waiting for a resource that is held by T0. Computer Operating Systems 4 Resource-Allocation Graph  Deadlocks can be described more precisely in terms of a directed graph called a system resource-allocation graph. A set of vertices V and a set of edges E.  V is partitioned into two types: T = {T1, T2, …, Tn}, the set consisting of all the threads in the system. R = {R1, R2, …, Rm}, the set consisting of all resource types in the system  request edge – directed edge Ti Rj  assignment edge – directed edge Rj Ti Computer Operating Systems 5 Resource-Allocation Graph  Thread  Resource Type with 4 instances  Ti requests instance of Rj Ti Rj  Ti is holding an instance of Rj Ti Rj Computer Operating Systems 6 Resource Allocation Graph Example  One instance of R1  Two instances of R2  One instance of R3  Three instance of R4  T1 holds one instance of R2 and is waiting for an instance of R1  T2 holds one instance of R1, one instance of R2, and is waiting for an instance of R3  T3 holds one instance of R3 Computer Operating Systems 7 Resource Allocation Graph with a Deadlock Computer Operating Systems 8 Graph with a Cycle But no Deadlock Computer Operating Systems 9 Basic Facts  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 Computer Operating Systems 10 Resource Allocation Graph Example  Determine If there is a deadlock or not R1 R3 T1 T2 T3 R2 Computer Operating Systems 11 Resource Allocation Graph Example  Determine If there is a deadlock or not R1 R3 T1 T2 T3 R2 R4 Computer Operating Systems 12 Resource Allocation Graph Example  Determine If there is a deadlock or not R1 R3 T1 T2 T3 R2 R4 Computer Operating Systems 13 Resource Allocation Graph Example  Determine If there is a deadlock or not R1 R3 T1 T2 T3 R2 R4 Computer Operating Systems 14 Methods for Handling Deadlocks  Ensure that the system will never enter a deadlock state: Deadlock avoidance  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 and Windows. Computer Operating Systems 15 Deadlock Avoidance Requires that the system has some additional a priori information available Simplest and most useful model requires that each thread 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 Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the threads Computer Operating Systems 16 Safe State  When a thread 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 sequence of ALL the threads in the systems such that for each Ti, the resources that Ti can still request can be satisfied by currently available resources + resources held by all the Tj  That is: If Ti resource needs are not immediately available, then Ti can wait until all Tj have finished When Tj is finished, Ti can obtain needed resources, execute, return allocated resources, and terminate When Ti terminates, Ti +1 can obtain its needed resources, and so Computer Operating Systems 17 on Safe, Unsafe, Deadlock State  If a system is in safe state  no deadlocks  If a system is in unsafe state  possibility of deadlock  Avoidance  ensure that a system will never enter an unsafe state. Computer Operating Systems 18 Avoidance Algorithms  Multiple instances of a resource type Use the Banker’s Algorithm Computer Operating Systems 19 Banker’s Algorithm  Multiple instances of resources  Each thread must a priori claim maximum use  When a thread requests a resource, it may have to wait  When a thread gets all its resources it must return them in a finite amount of time Computer Operating Systems 20 Data Structures for the Banker’s Algorithm Let n = number of threads, and m = number of resources types.  Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available  Max: n x m matrix. If Max [i,j] = k, then thread Ti may request at most k instances of resource type Rj  Allocation: n x m matrix. If Allocation[i,j] = k then Ti is currently allocated k instances of Rj  Need: n x m matrix. If Need[i,j] = k, then Ti may need k more instances of Rj to complete its task  Need [i,j] = Max[i,j] – Allocation [i,j] Computer Operating Systems 21 Resource-Request Algorithm for Process Ti Requesti = request vector for process Ti. If Requesti [j] = k then thread Ti wants k instances of resource type Rj 1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim 2. If Requesti ≤ Available, go to step 3. Otherwise Ti must wait, since resources are not available 3. Pretend to allocate requested resources to Ti by modifying the state as follows: Available = Available – Requesti; Allocationi = Allocationi + Requesti; Needi = Needi – Requesti; If safe  the resources are allocated to Ti If unsafe  Ti must wait, and the old resource-allocation state is restored Computer Operating Systems 22 Resource-Request Algorithm (Banker’s Algorithm) for Thread Ti Computer Operating Systems 23 Example of Banker’s Algorithm  5 threads T0 through T4; 3 resource types:  A (10 instances), B (5 instances), and C (7 instances)  Snapshot at time t0: Allocation Max Available ABC ABC ABC T0 010 753 332 T1 200 322 T2 302 902 T3 211 222 T4 002 433  Identify the contents of the Need matrix.  Using the banker’s algorithm, determine whether or not each of the following states is unsafe. If the state is safe, illustrate the order in which the threads may complete. Computer Operating Systems 24 Example of Banker’s Algorithm  The content of the matrix Need is defined to be Max – Allocation Need ABC T0 743 T1 122 T2 600 T3 011 T4 431 Computer Operating Systems 25 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 332 7 4 3 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 26 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 332 7 4 3 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 27 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 332 7 4 3 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 28 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 332 7 4 3 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 29 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 332 7 4 3 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 30 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 332 7 4 3 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 31 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 532 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 32 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 532 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 33 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 34 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 35 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 36 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 37 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 38 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 39 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 745 7 4 3 1 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 3 T4 0 0 2 433 4 3 1 Computer Operating Systems 40 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 7 5 3 10 4 7 7 4 3 1 T1 2 0 0 322 1 2 2 4 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 3 T4 0 0 2 433 4 3 1 Computer Operating Systems 41 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC 5 T0 0 1 0 7 5 3 10 5 7 7 4 3 1 T1 2 0 0 322 1 2 2 4 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 3 T4 0 0 2 433 4 3 1 Computer Operating Systems 42 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC 5 T0 0 1 0 7 5 3 10 5 7 7 4 3 1 T1 2 0 0 322 1 2 2 4 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 3 T4 0 0 2 433 4 3 1  The system is in a safe state since the sequence < T1, T3, T4, T2, T0> satisfies safety criteria Computer Operating Systems 43 Example of Banker’s Algorithm  If a request from thread T1 arrives for (1, 0, 2), can the request be granted immediately? Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 332 7 4 3 T1 2 0 0 322 1 2 2 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 44 Example of Banker’s Algorithm  Check that Request  Need (that is, (1, 0, 2)  (1, 2, 2)  true  Check that Request  Available (that is, (1, 0, 2)  (3, 3, 2)  true Allocation Max Available Need ABC ABC ABC ABC T0 010 753 332 7 4 3 T1 200 322 1 2 2 T2 302 902 6 0 0 T3 211 222 0 1 1 T4 002 433 4 3 1 Computer Operating Systems 45 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 46 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 47 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 48 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 49 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 50 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 51 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 52 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 532 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 53 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 54 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 55 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 56 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 57 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 743 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 Computer Operating Systems 58 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 745 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 3 T4 0 0 2 433 4 3 1 Computer Operating Systems 59 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC 4 T0 0 1 0 753 755 7 4 3 1 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 3 T4 0 0 2 433 4 3 1 Computer Operating Systems 60 Example of Banker’s Algorithm Allocation Max Available Need ABC ABC ABC ABC 4 T0 0 1 0 7 5 3 10 5 7 7 4 3 1 T1 3 0 2 322 0 2 0 5 T2 3 0 2 902 6 0 0 2 T3 2 1 1 222 0 1 1 3 T4 0 0 2 433 4 3 1  Executing Banker’s Algorithm shows that sequence < T1, T3, T4, T0, T2> satisfies safety requirement  Therefore, the request from thread T1 (1, 0, 2), can be granted immediately Computer Operating Systems 61 Example of Banker’s Algorithm  If a request from thread T4 arrives for (3, 3, 0), can the request be granted immediately? Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1 NO you can not granted this request immediately, T4 must wait, since resources are not available. (T4’s Request (3, 3, 0) > Available (2, 3, 0) Computer Operating Systems 62 Example of Banker’s Algorithm  If a request from thread T0 arrives for (0, 2, 0), can the request be granted immediately? Allocation Max Available Need ABC ABC ABC ABC T0 0 1 0 753 230 7 4 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1  Check that Request  Need (that is, (0, 2, 0)  (7, 4, 3)  true  Check that Request  Available (that is, (0, 2, 0)  (2, 3, 0)  true Computer Operating Systems 63 Example of Banker’s Algorithm  Check that Request  Need (that is, (0, 2, 0)  (7, 4, 3)  true  Check that Request  Available (that is, (0, 2, 0)  (2, 3, 0)  true Allocation Max Available Need ABC ABC ABC ABC T0 0 3 0 753 210 7 2 3 T1 3 0 2 322 0 2 0 T2 3 0 2 902 6 0 0 T3 2 1 1 222 0 1 1 T4 0 0 2 433 4 3 1  The request for (0,2,0) by T0 cannot be granted, even though the resources are available, since the resulting state is unsafe, because there is no sequence of threads that satisfies the safety criteria. Computer Operating Systems 64 Example of Banker’s Algorithm  Identify the contents of the Need matrix.  If a request from T3 arrives for (2,1,0,0), can the request be granted immediately? Allocation Max Availabl Need ABCD ABCD e ABCD ABCD T0 2 0 1 1 3 2 1 1 6 4 4 2 T1 1 1 0 0 1 2 0 2 T2 1 0 1 0 3 2 1 0 T3 0 1 0 1 2 1 0 1 Computer Operating Systems 65 Example of Banker’s Algorithm  Identify the contents of the Need matrix.  If a request from T3 arrives for (2,1,0,0), can the request be granted immediately? Allocation Max Availabl Need ABCD ABCD e ABCD ABCD T0 2 0 1 1 3 2 1 1 6 4 4 1 200 2 T1 1 1 0 0 1 2 0 2 0 102 T2 1 0 1 0 3 2 1 0  NO you 2 200 can not granted this request, it raise error condition, since T3 0 has the thread 1 0exceeded 1 2 1 its 0 maximum 1 claim. 2 0 0 0 (T3’s Request (2, 1, 0, 0) > T3’s Need (2, 1, 0, 0) ) Computer Operating Systems 66 Example of Banker’s Algorithm  If a request from T3 arrives for (7,1,0,0), can the request be granted immediately? Allocation Max Availabl Need ABCD ABCD e ABCD ABCD T0 2 0 1 1 3 2 1 1 6 4 4 1 200 2 T1 1 1 0 0 1 2 0 2 0 102 T2 1 0 1 0 3 2 1 0 2 200 T3 0 1 0 1 7 2 0 1 7 100 Computer Operating Systems 67 Example of Banker’s Algorithm  Identify the contents of the Need matrix.  If a request from T3 arrives for (7,1,0,0), can the request be granted immediately? Allocation Max Availabl Need ABCD ABCD e ABCD ABCD T0 2 0 1 1 3 2 1 1 6 4 4 1 200 2 T1 1 1 0 0 1 2 0 2 0 102 T2 1 0 1 0 3 2 1 0 2 200  NO T3 can0 not you 1 0granted 1 7this2 request 0 1 immediately, T73 must 1 0 0wait, since resources are not available. (T3’s Request (7, 1, 0, 0) > Available (6, 4, 4, 2) Computer Operating Systems 68

Use Quizgecko on...
Browser
Browser