Section notes for CS162 Section #6, March 4, 2003 Hakim Weatherspoon ADMINISTRIVIA - Office hours M 12-1, Tu 3:30-4:30 Location 651 Soda (6th floor alcove) - Project 1 code due Thursday, March 6th, 2003, 11:59pm Group evaluations due two days later. Lose points if don't fill out. - Midterm on Thursday, March 13th, 2003, 7:00-9:00pm Location 1 Pimentel - Take picures of all groups today - Project clarification. Using condition variables or semaphores for task 1 (join) is incorrect. QUIZ1 BANKERS ALGORITHM - Important stuff; might be a midterm question! - Source of name: Can be used by banks so they don't allocate their available cache without being able to satisfy the needs of all customers. problem: Dining lawyers with the Banker's Algorithm 4 lawyers (P1, P2, P3, P4) 4 chopsticks - single resource with Available[r] = 4 Each lawyer needs 2 chopsticks: Need[0..p][r] = 2 Lawyer P1 has 2 chopsticks already Are we in a safe state? How do you know? Yes we are in a safe state. Here is how we know: - Lawyer P1 has 2 chopsticks already Alloc Max Avail P1 2 2 2 P2 0 2 P3 0 2 P4 0 2 - P2 asks for a chopstick Go ahead and "allocate" it: Alloc Max Avail P1 2 2 1 P2 1 2 P3 0 2 P4 0 2 Test for safe state: 1) Initialize: Work = 1 Alloc Max Need Finish P1 2 2 0 F P2 1 2 1 F P3 0 2 2 F P4 0 2 2 F 2) Need <= Work for P1, so... Work = 3 Alloc Max Need Finish P1 2 2 0 T P2 1 2 1 F P3 0 2 2 F P4 0 2 2 F 3) Need <= Work for P2, so... Work = 4 Alloc Max Need Finish P1 2 2 0 T P2 1 2 1 T P3 0 2 2 F P4 0 2 2 F 3) Need <= Work for P3, so... Work = 4 (no change!) Alloc Max Need Finish P1 2 2 0 T P2 1 2 1 T P3 0 2 2 T P4 0 2 2 F 4) Need <= Work for P4, so... Work = 4 (no change!) Alloc Max Need Finish P1 2 2 0 T P2 1 2 1 T P3 0 2 2 T P4 0 2 2 T We are in a safe state! What about a different setup? P1, P2, and P3 have grabbed one chopstick and P4 is about to grab one. Are we in a safe state? How do you know? We are not in a safe state Here is how we know: Alloc Max Avail P1 1 2 1 P2 1 2 P3 1 2 P4 0 2 - P4 asks for a chopstick Go ahead and "allocate" it: Alloc Max Avail P1 1 2 0 P2 1 2 P3 1 2 P4 1 2 Test for safe state: 1) Initialize: Work = 0 Alloc Max Need Finish P1 1 2 1 F P2 1 2 1 F P3 1 2 1 F P4 1 2 1 F 2) No proc exists which has Need <= Work! So we are not in a safe state. What if P1 had asked for a second chopstick instead? We are in a safe state. Here is how we know: 1) Initialize: Work = 0 Alloc Max Need Finish P1 2 2 0 F P2 1 2 1 F P3 1 2 1 F P4 0 2 2 F 2) P1's need is <= Work, so Work = 2 (+ 2) Alloc Max Need Finish P1 2 2 0 T P2 1 2 1 F P3 1 2 1 F P4 0 2 2 F Now you can see that any order of P2, P3, or P4 will satisfy the safe state. Bankers Algorithm Definition - Process enters system: Declares maximum resources it may need Available[0..r] : Number of available resources of each type Max[0..p][0..r] : Max demand of process P for resource R Alloc[0..p][0..r] : Amount of R allocated to P Need[0..p][0..r] : Amount of R *might be* needed by P; Need[p,r] = Max[p,r] - Alloc[p,r] - Define notation: Vector X <= Y if X[i] <= Y[i] forall i - Making a resource request: 1) Process P states that it needs resources: Request[p][0..r] 2) If Request[p][0..r] > Need[p][0..r], ERROR Process is requesting more than its stated maximum 3) If Request[p][0..r] > Available[p][0..r], WAIT All resources not available, so must wait 4) Set: Available[0..r] = Available[0..r] - Request[p][0..r] Alloc[p][0..r] = Alloc[p][0..r] + Request[p][0..r] Need[p][0..r] = Need[p][0..r] - Request[p][0..r] Now test if this resource-allocation state is "safe". If safe, proceed, otherwise revert to previous values of vectors and have P wait. - Safety test: Available resources >= Max needed by ANY process Implementation: Work[0..r] = Available[0..r] Finish[0..p] = false 1) Find a process P such that Finish[P] = false && Need[p][0..r] <= Work[0..r] If no such P exists, goto 3 2) Work[0..r] = Work[0..r] + Alloc[p][0..r] Finish[p] = true Goto 1 3) If Finish[p] = true for all procs P, system is safe QUIZ2 SCHEDULING ALGORITHMS Consider the following processes and their associated arrival times and processing time requirements. |------------------------------------------------| |Process | ArrivalTime | ProcessingRequirements | |------------------------------------------------| | A | 0 | 1 | |------------------------------------------------| | B | 2 | 5 | |------------------------------------------------| | C | 3 | 1 | |------------------------------------------------| | D | 6 | 3 | |------------------------------------------------| Complete the following table to indicate which process will be scheduled on the CPU using different scheduling algorithms. Assume that the time slice is 1 unit. For RR, assume that new processes are scheduled immediately. |-------------------------------| | Time | FIFO | RR | SJF | SRTF | |-------------------------------| | 0 | A | A | A | A | |-------------------------------| | 1 | | | | | |-------------------------------| | 2 | B | B | B | B | |-------------------------------| | 3 | B | C | B | C | |-------------------------------| | 4 | B | B | B | B | |-------------------------------| | 5 | B | B | B | B | |-------------------------------| | 6 | B | D | B | B | |-------------------------------| | 7 | C | B | C | B | |-------------------------------| | 8 | D | D | D | D | |-------------------------------| | 9 | D | B | D | D | |-------------------------------| | 10 | D | D | D | D | |-------------------------------| | 11 | | | | | |-------------------------------| | 12 | | | | | |-------------------------------| | 13 | | | | | |-------------------------------| | 14 | | | | | |-------------------------------| | 15 | | | | | |-------------------------------| TURNAROUND TIME Compute the average turnaround time for each algorithm. Which algorithm provides the fastest average resonse time in this example? Does that algorithm always provide the fastest turnaround time? The average turnaround times are as below. FIFO: 1 + 5 + 5 + 5 / 4 = 16 / 4 = 4 RR: 1 + 8 + 1 + 5 / 4 = 15 / 4 = 3.75 SJF: 1 + 5 + 5 + 5 / 4 = 16 / 4 = 4 SRTF: 1 + 9 + 1 + 3 / 4 = 14 / 4 = 3.5 The SRTF algorithm results in the fastest turnaround time in this example. In fact, the SRTF algorithm always provides the fastest turnaround time--provably so. SJF provides the fastest turnaround time among non-preemptive policies. The disadvantage of SRTF and SJF is that they may lead to starvation. QUIZ3 PROTECTION What does the address space abstraction do? prevents multiple programs from interfering with each other. How implemented? Address translation hardware - Each address that a program references is mapped onto a physical memory location - Maintaining different virtual->physical mappings for different processes prevents them from interfering What is Dual-mode operation? - Must give kernel control over manipulating page tables - How? Processor runs in either "user" or "kernel" mode - Certain instructions are privileged -- e.g., access to page tables - Direct access to physical memory also allowed in kernel mode PROTECTION continued - System calls - User invokes special instruction which "traps" into OS - Trap only allows user to invoke certain functions in the kernel - e.g., user places index of system call table in a register, invokes trap - Trap looks at contents of register, jumps to appropriate function in kernel - Must be executing on different stack than user! (Why? To avoid user manipulating return addresses on stack.) - Hardware interrupt - Cause transition from user process to kernel (that is, if user code is currently running) - Software faults - Division by zero - Segmentation violation: Accesing virtual memory address which is not mapped (that is, has no page table entry) - Page fault: Used to provide illusion of "infinite" virtual memory (covered later in course) REVIEW - DEADLOCK - Some words on MESA style monitors (used in Nachos!) - signaler keeps the lock and cpu, the waiter is put on the ready Q ( so use wait() inside a while loop and recheck the condition! ) - waiter releases the lock and goes to sleep atomically, when it wakes it is on the ready Q - Types of resources - preemptable resources can be taken away with little or no problem (eg cpu, memory), and can be scheduled - non-preemtable resources cause a problem if they are taken away (eg disk, semaphores, terminal, printer), and must be allocated - Deadlock is concerned with allocating resources. - Deadlock is a situation in which each thread in a set is waiting for something from some other thread waiting in the set. Since each is waiting, non can proceed. - Conditions for deadlock 1. limited access (mutex, bounded buffer) 2. no preemption allowed 3. multiple independent requests wait while holding a resource 4. circular chain requests - Approaches to deadlock problem - detection, detect deadlock and fix it, database solution - avoidance, OS solution 1. must eliminate one of the four deadlock conditions (hard!) 2. only make allocations that are "safe" (Banker's Algorithm) - A safe state is one in which deadlock is not inevitable (ie there exists a sequence of task completions which lead to all tasks completed). A safe allocation is one that leads to a safe state. - An unsafe state leads to deadlock. - The Banker's Algorithm is used to compute a safe sequence of tasks.