DOC PREVIEW
Berkeley COMPSCI 162 - Lecture 7 Deadlock, CPU Scheduling

This preview shows page 1-2-3-21-22-23-42-43-44 out of 44 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS162!Operating Systems and!Systems Programming!Lecture 7!Deadlock, CPU Scheduling"February 9, 2011!Ion Stoica!http://inst.eecs.berkeley.edu/~cs162!Lec 7.2!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Read/Writer Revisited"Reader() { // check into system lock.Acquire(); while ((AW + WW) > 0) { WR++; okToRead.wait(&lock); WR--; } AR++; lock.release(); // read-only access AccessDbase(ReadOnly); // check out of system lock.Acquire(); AR--; if (AR == 0 && WW > 0) okToWrite.signal(); lock.Release(); }!Writer() { // check into system lock.Acquire(); while ((AW + AR) > 0) { WW++; okToWrite.wait(&lock); WW--; } AW++; lock.release(); // read/write access AccessDbase(ReadWrite); // check out of system lock.Acquire(); AW--; if (WW > 0){ okToWrite.signal(); } else if (WR > 0) { okToRead.broadcast(); } lock.Release(); }!What if we remove this line?!Lec 7.3!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Read/Writer Revisited"Reader() { // check into system lock.Acquire(); while ((AW + WW) > 0) { WR++; okToRead.wait(&lock); WR--; } AR++; lock.release(); // read-only access AccessDbase(ReadOnly); // check out of system lock.Acquire(); AR--; if (AR == 0 && WW > 0) okToWrite.broadcast(); lock.Release(); }!Writer() { // check into system lock.Acquire(); while ((AW + AR) > 0) { WW++; okToWrite.wait(&lock); WW--; } AW++; lock.release(); // read/write access AccessDbase(ReadWrite); // check out of system lock.Acquire(); AW--; if (WW > 0){ okToWrite.signal(); } else if (WR > 0) { okToRead.broadcast(); } lock.Release(); }!What if we turn signal to broadcast?!Lec 7.4!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Read/Writer Revisited"Reader() { // check into system lock.Acquire(); while ((AW + WW) > 0) { WR++; okContinue.wait(&lock); WR--; } AR++; lock.release(); // read-only access AccessDbase(ReadOnly); // check out of system lock.Acquire(); AR--; if (AR == 0 && WW > 0) okContinue.signal(); lock.Release(); }!Writer() { // check into system lock.Acquire(); while ((AW + AR) > 0) { WW++; okContinue.wait(&lock); WW--; } AW++; lock.release(); // read/write access AccessDbase(ReadWrite); // check out of system lock.Acquire(); AW--; if (WW > 0){ okToWrite.signal(); } else if (WR > 0) { okContinue.broadcast(); } lock.Release(); }!What if we turn okToWrite and okToRead into okContinue?!Lec 7.5!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Read/Writer Revisited"Reader() { // check into system lock.Acquire(); while ((AW + WW) > 0) { WR++; okContinue.wait(&lock); WR--; } AR++; lock.release(); // read-only access AccessDbase(ReadOnly); // check out of system lock.Acquire(); AR--; if (AR == 0 && WW > 0) okContinue.signal(); lock.Release(); }!Writer() { // check into system lock.Acquire(); while ((AW + AR) > 0) { WW++; okContinue.wait(&lock); WW--; } AW++; lock.release(); // read/write access AccessDbase(ReadWrite); // check out of system lock.Acquire(); AW--; if (WW > 0){ okToWrite.signal(); } else if (WR > 0) { okContinue.broadcast(); } lock.Release(); }!• R1 arrives !• W1, R2 arrive while R1 reads!• R1 signals R2!Lec 7.6!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Read/Writer Revisited"Reader() { // check into system lock.Acquire(); while ((AW + WW) > 0) { WR++; okContinue.wait(&lock); WR--; } AR++; lock.release(); // read-only access AccessDbase(ReadOnly); // check out of system lock.Acquire(); AR--; if (AR == 0 && WW > 0) okContinue.broadcast(); lock.Release(); }!Writer() { // check into system lock.Acquire(); while ((AW + AR) > 0) { WW++; okContinue.wait(&lock); WW--; } AW++; lock.release(); // read/write access AccessDbase(ReadWrite); // check out of system lock.Acquire(); AW--; if (WW > 0){ okToWrite.signal(); } else if (WR > 0) { okContinue.broadcast(); } lock.Release(); }!Need to change to broadcast!!Why?!Lec 7.7!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!• Resources – passive entities needed by threads to do their work!– CPU time, disk space, memory!• Two types of resources:!– Preemptable – can take it away!» CPU, Embedded security chip!– Non-preemptable – must leave it with the thread!» Disk space, printer, chunk of virtual address space!» Critical section !• Resources may require exclusive access or may be sharable!– Read-only files are typically sharable!– Printers are not sharable during time of printing!• One of the major tasks of an operating system is to manage resources!Resources"Lec 7.8!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Starvation vs Deadlock"• Starvation vs. Deadlock!– Starvation: thread waits indefinitely!» Example, low-priority thread waiting for resources constantly in use by high-priority threads!– Deadlock: circular waiting for resources!» Thread A owns Res 1 and is waiting for Res 2#Thread B owns Res 2 and is waiting for Res 1!– Deadlock ⇒ Starvation but not vice versa!» Starvation can end (but doesnʼt have to)!» Deadlock canʼt end without external intervention!Res 2 Res 1 Thread B Thread A Wait For Wait For Owned By Owned ByLec 7.9!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Conditions for Deadlock"• Deadlock not always deterministic – Example 2 mutexes:!! !Thread A Thread B x.P(); y.P(); y.P(); x.P(); y.V(); x.V(); x.V(); y.V(); – Deadlock wonʼt always happen with this code!» Have to have exactly the right timing (“wrong” timing?)!» So you release a piece of software, and you tested it, and there it is, controlling a nuclear power plant…!• Deadlocks occur with multiple resources!– Means you canʼt decompose the problem!– Canʼt solve deadlock for each resource independently!• Example: System with 2 disk drives and two threads!– Each thread needs 2 disk drives to function!– Each thread gets one disk and waits for another one!A: x.P(); B: y.P(); A: y.p(); B: x.P(); ... Deadlock!Lec 7.10!2/9/11! Ion Stoica CS162 ©UCB Spring 2011!Bridge Crossing Example"• Each segment of road can be viewed as a resource!– Car must own the segment


View Full Document

Berkeley COMPSCI 162 - Lecture 7 Deadlock, CPU Scheduling

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Download Lecture 7 Deadlock, CPU Scheduling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7 Deadlock, CPU Scheduling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 Deadlock, CPU Scheduling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?