# CS 162 Discussion Section Week 4 9/30 - 10/4. Today’s Section ●Project administrivia ●Quiz ●Lecture Review ●Worksheet and Discussion.

## Presentation on theme: "CS 162 Discussion Section Week 4 9/30 - 10/4. Today’s Section ●Project administrivia ●Quiz ●Lecture Review ●Worksheet and Discussion."— Presentation transcript:

CS 162 Discussion Section Week 4 9/30 - 10/4

Today’s Section ●Project administrivia ●Quiz ●Lecture Review ●Worksheet and Discussion

Project 1 ●Autograder is up submit proj1-test ●Due 10/8 (Tuesday) at 11:59 PM submit proj1-code ●Due 10/9 (Wednesday) at 11:59 PM Final design doc & Project 1 Group Evals ●Questions?

Quiz…

1.Name the conditions causing deadlock, given definitions (0.5 points each): a.thread holding at least one resource waits to acquire other resources held by other threads b.only one thread at a time can use a resource c.resource only released voluntarily by the thread holding it after done using the resource d.chain of n threads T(i); T(i) depends on T(i+1); T(n) depends on T(0) (True/False) 2.Starvation implies deadlock 3.Deadlock implies starvation (that a thread waits indefinitely) 4.To find general deadlocks, it is enough to look for loops in the resource allocation graph.

Lecture Review

Lec 7.8 9/25/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 Four requirements for Deadlock Mutual exclusion –Only one thread at a time can use a resource Hold and wait –Thread holding at least one resource is waiting to acquire additional resources held by other threads No preemption –Resources are released only voluntarily by the thread holding the resource, after thread is finished with it Circular wait –There exists a set {T 1, …, T n } of waiting threads »T 1 is waiting for a resource that is held by T 2 »T 2 is waiting for a resource that is held by T 3 »…»… »T n is waiting for a resource that is held by T 1

Lec 7.9 9/25/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 T1T1 T2T2 T3T3 R2R2 R1R1 T4T4 Deadlock Detection Algorithm Only one of each type of resource  look for loops More General Deadlock Detection Algorithm –Let [X] represent an m-ary vector of non-negative integers (quantities of resources of each type): [FreeResources]: Current free resources each type [Request X ]: Current requests from thread X [Alloc X ]: Current resources held by thread X –See if tasks can eventually terminate on their own [Avail] = [FreeResources] Add all nodes to UNFINISHED do { done = true Foreach node in UNFINISHED { if ([Request node ] <= [Avail]) { remove node from UNFINISHED [Avail] = [Avail] + [Alloc node ] done = false } } } until(done) –Nodes left in UNFINISHED  deadlocked

Lec 7.10 9/25/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 Toward right idea: –State maximum resource needs in advance –Allow particular thread to proceed if: (available resources - #requested)  max remaining that might be needed by any thread Banker’s algorithm (less conservative): –Allocate resources dynamically »Evaluate each request and grant if some ordering of threads is still deadlock free afterward »Keeps system in a “SAFE” state, i.e. there exists a sequence {T 1, T 2, … T n } with T 1 requesting all remaining resources, finishing, then T 2 requesting all remaining resources, etc.. –Algorithm allows the sum of maximum resource needs of all current threads to be greater than total resources Banker’s Algorithm for Preventing Deadlock

Lec 7.11 9/25/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 Banker’s Algorithm Technique: pretend each request is granted, then run deadlock detection algorithm, substitute ([Request node ] ≤ [Avail])  ([Max node ]-[Alloc node ] ≤ [Avail]) [FreeResources]: Current free resources each type [Max X ]: Max resources requested by thread X [Alloc X ]: Current resources held by thread X [Avail] = [FreeResources] Add all nodes to UNFINISHED do { done = true Foreach node in UNFINISHED { if ([Max node ]–[Alloc node ] <= [Avail]) { remove node from UNFINISHED [Avail] = [Avail] + [Alloc node ] done = false } } } until(done)

Lec 7.13 9/25/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 Scheduling Metrics Waiting Time: time the job is waiting in the ready queue –Time between job’s arrival in the ready queue and launching the job Service (Execution) Time: time the job is running Response (Completion) Time: –Time between job’s arrival in the ready queue and job’s completion –Response time is what the user sees: »Time to echo a keystroke in editor »Time to compile a program Response Time = Waiting Time + Service Time Throughput: number of jobs completed per unit of time –Throughput related to response time, but not same thing: »Minimizing response time will lead to more context switching than if you only maximized throughput

Lec 8. 9/30/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 Scheduling Policy Goals/Criteria Minimize Response Time –Minimize elapsed time to do an operation (or job) Maximize Throughput –Two parts to maximizing throughput  Minimize overhead (for example, context-switching)  Efficient use of resources (CPU, disk, memory, etc) Fairness –Share CPU among users in some equitable way –Fairness is not minimizing average response time:  Better average response time by making system less fair

Lec 8. 9/30/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 Summary Scheduling: selecting a process from the ready queue and allocating the CPU to it FCFS Scheduling: –Run threads to completion in order of submission –Pros: Simple (+) –Cons: Short jobs get stuck behind long ones (-) Round-Robin Scheduling: –Give each thread a small amount of CPU time when it executes; cycle between all ready threads –Pros: Better for short jobs (+) –Cons: Poor when jobs are same length (-)

Lec 8. 9/30/13 Anthony D. Joseph and John Canny CS162 ©UCB Fall 2013 Summary (cont’d) Shortest Job First (SJF)/Shortest Remaining Time First (SRTF): –Run whatever job has the least amount of computation to do/least remaining amount of computation to do –Pros: Optimal (average response time) –Cons: Hard to predict future, Unfair Multi-Level Feedback Scheduling: –Multiple queues of different priorities –Automatic promotion/demotion of process priority in order to approximate SJF/SRTF Lottery Scheduling: –Give each thread a number of tokens (short tasks  more tokens) –Reserve a minimum number of tokens for every thread to ensure forward progress/fairness

Worksheet …

1.Name the conditions causing deadlock, given definitions (0.5 points each): a.chain of n threads T(i); T(i) depends on T(i+1); T(n) depends on T(0) b.thread holding at least one resource waits to acquire other resources held by other threads c.resource only released voluntarily by the thread holding it after done using the resource d.only one thread at a time can use a resource (True/False) 2.Deadlock implies starvation (that a thread waits indefinitely) 3.Starvation implies deadlock 4.To find general deadlocks, it is enough to look for loops in the resource allocation graph.

Download ppt "CS 162 Discussion Section Week 4 9/30 - 10/4. Today’s Section ●Project administrivia ●Quiz ●Lecture Review ●Worksheet and Discussion."

Similar presentations