Operating Systems Exam 2
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively Initialize: a) Work = Available b) For i = 1,2, ..., n, if Allocation i (Not =) 0, thenFinish[i] = false; otherwise, Finish[i] = true 2. Find an index i such that both: a) Finish[i] == false b) Request i <= Work If no such i exists, go to step 4 3. Work = Work + AllocationiFinish[i] = true go to step 2 4. If Finish[i] == false, for some i, 1 i n, then the system is in a deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked The algorithm requires an order of O(m x n2) operations to detect whether the system is in a deadlocked state
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i = 0, 1, ..., n- 1 2. Find an i such that both: (a) Finish [i] = false (b) Need i <= Work If no such i exists, go to step 4 3. Work = Work + Allocation i Finish[i] = true go to step 2 4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Allocation Graph
A set of vertices V and a set of edges E. ▪ V is partitioned into two types: • P = {P1, P2, ..., Pn}, the set consisting of all the processes in the system • R = {R1, R2, ..., Rm}, the set consisting of all resourcetypes in the system ▪ request edge - directed edge Pi → Rj▪ assignment edge - directed edge Rj → Pi
Deadlock and Starvation
Deadlock - two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes ▪ Let S and Q be two semaphores initialized to 1 P0 P1 wait(S); wait(Q); wait(Q); wait(S); ... ... signal(S); signal(Q); signal(Q); signal(S); ▪ Starvation - indefinite blocking • A process may never be removed from the semaphore queue in which it is suspended ▪ Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process • Solved via priority-inheritance protocol
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously. ▪ 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 {P0, P1, ..., Pn} of waiting processes such that P0 is waiting for a resource that is held byP1, P1 is waiting for a resource that is held by P2, ..., Pn-1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.
Deadlock Prevention
Invalidate one of the four necessary conditions for deadlock: ▪ Mutual Exclusion - not required for sharable resources (e.g., read-only files); must hold for non-sharable resources ▪ Hold and Wait - must guarantee that whenever a process requests a resource, it does not hold any other resources • Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none allocated to it. • Low resource utilization; starvation possible
Thread Cancellation (Cont.)
Invoking thread cancellation requests cancellation, but actual cancellation depends on thread state ▪ If thread has cancellation disabled, cancellation remains pending until thread enables it ▪ Default type is deferred • Cancellation only occurs when thread reaches cancellation point i.e., pthread_testcancel() Then cleanup handler is invoked ▪ On Linux systems, thread cancellation is handled through signals
Data Structures for the Banker's Algorithm
Let n = number of processes, 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 process Pi may request at most k instances of resource type Rj ▪ Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj ▪ Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task Need [i,j] = Max[i,j] - Allocation [i,j]
Resource-Request Algorithm for Process Pi
Request i = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of resource type Rj 1. If Request i <= Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim 2. If Request i <= Available, go to step 3. Otherwise Pi must wait,since resources are not available 3. Pretend to allocate requested resources to Pi by modifying the state as follows: Available = Available - Request i; Allocation i = Allocation i + Request i; Need i = Need i - Request i; • If safe -> the resources are allocated to Pi • If unsafe -> Pi must wait, and the old resource-allocation state is restored
Critical-Section Problem (Cont.)
Requirements for solution to critical-section problem 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the process that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted
Deadlock Avoidance
Requires that the system has some additional a priori information available ▪ Simplest and most useful model requires that each process 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 processes
Example of Banker's Algorithm
▪ 5 processes P0 through P4; 3 resource types: A (10 instances), B (5 instances), and C (7 instances) ▪ Snapshot at time T0: Allocation Max Available A B C A B C A B C P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3
Monitors
▪ A high-level abstraction that provides a convenient and effective mechanism for process synchronization ▪ Abstract data type, internal variables only accessible by code within the procedure ▪ Only one process may be active within the monitor at a time ▪ But not powerful enough to model some synchronization schemes monitor monitor-name { /* shared variable declarations */ function F1 (...) { ... } . . function Fn (...) { ... } initialization_code (...){...} ... } }
Priority Scheduling
▪ A priority number (integer) is associated with each process ▪ The CPU is allocated to the process with the highest priority (smallest integer = highest priority) • Preemptive • Non preemptive ▪ SJF is priority scheduling where priority is the inverse of predicted next CPU burst time ▪ Problem = Starvation - low priority processes may never execute ▪ Solution = Aging - as time progresses increase the priority of the process
Multilevel Feedback Queue
▪ A process can move between the various queues. ▪ Multilevel-feedback-queue scheduler defined by the following parameters: • Number of queues • Scheduling algorithms for each queue • Method used to determine when to upgrade a process • Method used to determine when to demote a process • Method used to determine which queue a process will enter when that process needs service ▪ Aging can be implemented using multilevel feedback queue
Recovery from Deadlock: Process Termination
▪ Abort all deadlocked processes ▪ Abort one process at a time until the deadlock cycle is eliminated ▪ In which order should we choose to abort? 1. Priority of the process 2. How long process has computed, and how much longer to completion 3. Resources the process has used 4. Resources process needs to complete 5. How many processes will need to be terminated 6. Is process interactive or batch?
Deadlock Detection
▪ Allow system to enter deadlock state ▪ Detection algorithm ▪ Recovery scheme
Shortest-Job-First (SJF) Scheduling
▪ Associate with each process the length of its next CPU burst • Use these lengths to schedule the process with the shortest time ▪ SJF is optimal - gives minimum average waiting time for a given set of processes ▪ Preemptive version called shortest-remaining-time-first ▪ How do we determine the length of the next CPU burst? • Could ask the user • Estimate
Shortest-Job-First (SJF) Scheduling
▪ Associate with each process the length of its next CPU burst • Use these lengths to schedule the process with the shortest time ▪ SJF is optimal - gives minimum average waiting time for a given set of processes • The difficulty is knowing the length of the next CPU request • Could ask the user
Several Instances of a Resource Type
▪ Available: A vector of length m indicates the number of available resources of each type ▪ Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process ▪ Request: An n x m matrix indicates the current request of eachprocess. If Request [i][j] = k, then process Pi is requesting k more instances of resource type Rj.
Scheduling Criteria
▪ CPU utilization - keep the CPU as busy as possible▪ Throughput - # of processes that complete their execution per time unit ▪ Turnaround time - amount of time to execute a particular process ▪ Waiting time - amount of time a process has been waiting in the ready queue ▪ Response time - amount of time it takes from when are quest was submitted until the first response is produced.
Example: P1 Request (1,0,2)
▪ Check that Request Available (that is, (1,0,2) (3,3,2) true Allocation Need Available A B C A B C A B C P0 0 1 0 7 4 3 2 3 0 P1 3 0 2 0 2 0 P2 3 0 2 6 0 0 P3 2 1 1 0 1 1 P4 0 0 2 4 3 1 ▪ Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2 > satisfies safety requirement ▪ Can request for (3,3,0) by P4 be granted? ▪ Can request for (0,2,0) by P0 be granted?
Resource-Allocation Graph Scheme
▪ Claim edge Pi → Rj indicated that process Pj may request resource Rj;represented by a dashed line ▪ Claim edge converts to request edge when a process requests are source ▪ Request edge converted to an assignment edge when the resource is allocated to the process ▪ When a resource is released by a process, assignment edge reconverts to a claim edge ▪ Resources must be claimed a priori in the system
Critical Section Problem
▪ Consider system of n processes {p0, p1, ... pn-1} ▪ Each process has critical section segment of code • Process may be changing common variables, updating table, writing file, etc. • When one process in critical section, no other may be in its critical section ▪ Critical section problem is to design protocol to solve this ▪ Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section
Semaphore Usage
▪ Counting semaphore - integer value can range over an unrestricted domain ▪ Binary semaphore - integer value can range only between 0 and 1 • Same as a mutex lock ▪ Can solve various synchronization problems ▪ Consider P1 and P2 that require S1 to happen before S2Create a semaphore "synch" initialized to 0 P1: S1; signal(synch); P2: wait(synch); S2; ▪ Can implement a counting semaphore S as a binary semaphore
Semantics of fork() and exec()
▪ Does fork()duplicate only the calling thread or all threads?• Some UNIXes have two versions of fork ▪ exec()usually works as normal - replace the running process including all threads
Round Robin (RR)
▪ Each process gets a small unit of CPU time (time quantum q), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. ▪ If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)qtime units. ▪ Timer interrupts every quantum to schedule next process▪ Performance • q large -> FIFO • q small -> q must be large with respect to context switch,otherwise overhead is too high
Methods for Handling Deadlocks
▪ Ensure that the system will never enter a deadlock state: • Deadlock prevention • 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.
Example of Detection Algorithm
▪ Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6 instances) ▪ Snapshot at time T0: Allocation Request Available A B C A B C A B C P0 0 1 0 0 0 0 0 0 0 P1 2 0 0 2 0 2 P2 3 0 3 0 0 0 P3 2 1 1 1 0 0 P4 0 0 2 0 0 2 ▪ Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i
Critical Section
▪ General structure of process Pi
Implicit Threading
▪ Growing in popularity as numbers of threads increase, program correctness more difficult with explicit threads ▪ Creation and management of threads done by compilers and run-time libraries rather than programmers ▪ Five methods explored • Thread Pools • Fork-Join • OpenMP • Grand Central Dispatch
Multiple-Processor Scheduling - Load Balancing
▪ If SMP, need to keep all CPUs loaded for efficiency ▪ Load balancing attempts to keep workload evenly distributed ▪ Push migration - periodic task checks load on each processor,and if found pushes task from overloaded CPU to other CPUs ▪ Pull migration - idle processors pulls waiting task from busy processor
Basic Facts
▪ 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.
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
Circular Wait
▪ Invalidating the circular wait condition is most common.▪ Simply assign each resource (i.e., mutex locks) a unique number. ▪ Resources must be acquired in order.
Single Instance of Each Resource Type
▪ Maintain wait-for graph• Nodes are processes • Pi → Pj if Pi is waiting for Pj ▪ Periodically invoke an algorithm that searches for a cycle in the graph.If there is a cycle, there exists a deadlock ▪ An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph
Scheduling Algorithm Optimization Criteria
▪ Max CPU utilization ▪ Max throughput ▪ Min turnaround time ▪ Min waiting time ▪ Min response time
Banker's Algorithm
▪ Multiple instances of resources ▪ Each process must a priori claim maximum use ▪ When a process requests a resource it may have to wait ▪ When a process gets all its resources it must return them in a finite amount of time
Semaphore Implementation
▪ Must guarantee that no two processes can execute the wait()and signal() on the same semaphore at the same time ▪ Thus, the implementation becomes the critical section problem where the wait and signal code are placed in the critical section • Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied ▪ Note that applications may spend lots of time in critical sections and therefore this is not a good solution
Deadlock Prevention (Cont.)
▪ No Preemption: • If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released • Preempted resources are added to the list of resources for which the process is waiting • Process will be restarted only when it can regain its old resources,as well as the new ones that it is requesting ▪ Circular Wait: • Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration
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 iswaiting for an instance of R1 ▪ T2 holds one instance of R1, one instance of R2, and is waiting for an instance of R3 ▪ T3 is holds one instance of R3
Example (Cont.)
▪ P2 requests an additional instance of type C Request A B C P0 0 0 0 P1 2 0 2 P2 0 0 1 P3 1 0 0 P4 0 0 2 ▪ State of system? • Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests • Deadlock exists, consisting of processes P1, P2, P3, and P4
Mutex Locks
▪ Previous solutions are complicated and generally inaccessible to application programmers ▪ OS designers build software tools to solve critical section problem ▪ Simplest is mutex lock ▪ Protect a critical section by first acquire() a lock then release() the lock • Boolean variable indicating if lock is available or not ▪ Calls to acquire() and release() must be atomic • Usually implemented via hardware atomic instructions ▪ But this solution requires busy waiting • This lock therefore called a spinlock
Race Condition
▪ Processes P0 and P1 are creating child processes using the fork()system call ▪ Race condition on kernel variable next_available_pid which represents the next available process identifier (pid) ▪ Unless there is a mechanism to prevent P0 and P1 from accessing the variable next_available_pid the same pid could be assigned to two different processes!
Recovery from Deadlock: Resource Preemption
▪ Selecting a victim - minimize cost ▪ Rollback - return to some safe state, restart process for that state ▪ Starvation - same process may always be picked as victim,include number of rollback in cost factor
Avoidance Algorithms
▪ Single instance of a resource type • Use a resource-allocation graph ▪ Multiple instances of a resource type • Use the Banker's Algorithm
Resource-Allocation Graph Algorithm
▪ Suppose that process Pi requests a resource Rj ▪ The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph
Multiple-Processor Scheduling
▪ Symmetric multiprocessing (SMP) is where each processor is self scheduling. ▪ All threads may be in a common ready queue (a) ▪ Each processor may have its own private queue of threads (b)
Semaphore
▪ Synchronization tool that provides more sophisticated ways (than Mutex locks)for process to synchronize their activities. ▪ Semaphore S - integer variable ▪ Can only be accessed via two indivisible (atomic) operations • wait() and signal() Originally called P() and V() ▪ Definition of the wait() operation wait(S) { while (S <= 0); // busy wait S--; } ▪ Definition of the signal() operation signal(S) { S++; }
Thread Cancellation
▪ Terminating a thread before it has finished ▪ Thread to be canceled is target thread ▪ Two general approaches: • Asynchronous cancellation terminates the target thread immediately • Deferred cancellation allows the target thread to periodically check if it should be cancelled
Example (Cont.)
▪ The content of the matrix Need is defined to be Max - Allocation Need A B C P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 ▪ The system is in a safe state since the sequence < P1, P3, P4, P2, P0 > satisfies safety criteria
Thread-Local Storage
▪ Thread-local storage (TLS) allows each thread to have its own copy of data ▪ Useful when you do not have control over the thread creation process(i.e., when using a thread pool) ▪ Different from local variables • Local variables visible only during single function invocation • TLS visible across function invocations ▪ Similar to static data• TLS is unique to each thread
Example of Multilevel Feedback Queue
▪ Three queues: • Q0 - RR with time quantum 8 milliseconds • Q1 - RR time quantum 16 milliseconds • Q2 - FCFS ▪ Scheduling • A new process enters queue Q0 which is served in RR When it gains CPU, the process receives 8 milliseconds If it does not finish in 8 milliseconds, the process is moved to queue Q1 • At Q1 job is again served in RR and receives 16 additional milliseconds If it still does not complete, it is preempted and moved to queue Q2
Safe State
▪ When a process 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 <P1, P2, ..., Pn > of ALL the processes in the systems such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i ▪ That is: • If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished • When Pj is finished, Pi can obtain needed resources, execute,return allocated resources, and terminate • When Pi terminates, Pi +1 can obtain its needed resources, and so on
Multiple-Processor Scheduling - Processor Affinity
▪ When a thread has been running on one processor, the cache contents of that processor stores the memory accesses by that thread. ▪ We refer to this as a thread having affinity for a processor (i.e.,"processor affinity") ▪ Load balancing may affect processor affinity as a thread may be moved from one processor to another to balance loads, yet that thread loses the contents of what it had in the cache of the processor it was moved off of. ▪ Soft affinity - the operating system attempts to keep a thread running on the same processor, but no guarantees. ▪ Hard affinity - allows a process to specify a set of processors it may run on.
Semaphore Implementation with no Busy waiting
▪ With each semaphore there is an associated waiting queue ▪ Each entry in a waiting queue has two data items: • value (of type integer) • pointer to next record in the list ▪ Two operations: • block - place the process invoking the operation on the appropriate waiting queue • wakeup - remove one of processes in the waiting queue and place it in the ready queue ▪ typedef struct{ int value; struct process *list; } semaphore;
Condition Variables
▪ condition x, y; ▪ Two operations are allowed on a condition variable: • x.wait() - a process that invokes the operation is suspended until x.signal() • x.signal() - resumes one of the processes (if any) that invoked x.wait() If no x.wait() on the variable, then it has no effect on the variable