DOC PREVIEW
LSU CSC 4103 - Deadlocks II

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 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 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CSC 4103 - Operating SystemsFall 2009Tevfik Ko!arLouisiana State UniversitySeptember 29th, 2009Lecture - XIDeadlocks - II2Roadmap• Classic Problems of Synchronization– Bounded Buffer– Readers-Writers– Dining Philosophers– Sleeping Barber• Deadlock Prevention3Classical Problems of Synchronization• Bounded-Buffer Problem• Readers and Writers Problem• Dining-Philosophers Problem• Sleeping Barber Problem4Bounded-Buffer Problem• Shared buffer with N slots to store at most N items• Producer processes data items and puts into the buffer• Consumer gets the data items from the buffer• Variable empty keeps number of empty slots in the butter• Variable full keeps number of full items in the buffer5Bounded Buffer – 1 Semaphore Soln• The structure of the producer process int empty=N, full=0; do { // produce an item wait (mutex); if (empty> 0){ // add the item to the buffer empty --; full++; } signal (mutex); } while (true);6Bounded Buffer – 1 Semaphore Soln• The structure of the consumer process do { wait (mutex); if (full>0){ // remove an item from buffer full--; empty++; } signal (mutex); // consume the removed item } while (true);consume non-existing item!7Bounded Buffer – 1 Semaphore Soln - II• The structure of the producer process int empty=N, full=0; do { // produce an item while (empty == 0){} wait (mutex); // add the item to the buffer empty --; full++; signal (mutex); } while (true);8Bounded Buffer – 1 Semaphore Soln - II• The structure of the consumer process do { while (full == 0){} wait (mutex); // remove an item from buffer full--; empty++; signal (mutex); // consume the removed item } while (true);* Mutual Exclusion not preserved!9Bounded Buffer – 2 Semaphore Soln• The structure of the producer process do { // produce an item wait (empty); // add the item to the buffer signal (full); } while (true);10Bounded Buffer – 2 Semaphore Soln• The structure of the consumer process do { wait (full); // remove an item from buffer signal (empty); // consume the removed item } while (true);* Mutual Exclusion not preserved!11Bounded Buffer - 3 Semaphore Soln• Semaphore mutex for access to the buffer, initialized to 1• Semaphore full (number of full buffers) initialized to 0• Semaphore empty (number of empty buffers) initialized to N12Bounded Buffer - 3 Semaphore Soln• The structure of the producer process do { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full);13Bounded Buffer - 3 Semaphore Soln• The structure of the consumer process do { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item14Readers-Writers Problem•Multiple Readers and writers concurrently accessing the same database.•Multiple Readers accessing at the same time --> OK•When there is a Writer accessing, there should be no other processes accessing at the same time. 15Readers-Writers Problem• The structure of a writer process do { wait (wrt) ; // writing is performed signal (wrt) ; } while (true)16Readers-Writers Problem (Cont.)• The structure of a reader process do { wait (mutex) ; readercount ++ ; if (readercount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readercount - - ; if readercount == 0) signal (wrt) ; signal (mutex) ; } while (true)17Dining Philosophers Problem• Five philosophers spend their time eating and thinking. • They are sitting in front of a round table with spaghetti served. •There are five plates at the table and five chopsticks set between the plates. • Eating the spaghetti requires the use of two chopsticks which the philosophers pick up one at a time. •Philosophers do not talk to each other.•Semaphore chopstick [5] initialized to 118Dining-Philosophers Problem (Cont.)• The structure of Philosopher i:Do { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think} while (true) ;19To Prevent Deadlock• Ensures mutual exclusion, but does not prevent deadlock• Allow philosopher to pick up her chopsticks only if both chopsticks are available (i.e. in critical section)• Use an asymmetric solution: an odd philosopher picks up first her left chopstick and then her right chopstick; and vice versa20Problems with Semaphores• Wrong use of semaphore operations:– semaphores A and B, initialized to 1 P0 P1wait (A); wait(B)wait (B); wait(A)!Deadlock– signal (mutex) …. wait (mutex)! violation of mutual exclusion– wait (mutex) … wait (mutex)!Deadlock– Omitting of wait (mutex) or signal (mutex) (or both)! violation of mutual exclusion or deadlock21Semaphores• inadequate in dealing with deadlocks• do not protect the programmer from the easy mistakes of taking a semaphore that is already held by the same process, and forgetting to release a semaphore that has been taken • mostly used in low level code, eg. operating systems• the trend in programming language development, though, is towards more structured forms of synchronization, such as monitors and channels 22Monitors• A high-level abstraction that provides a convenient and effective mechanism for process synchronization• Only one process may be active within the monitor at a timemonitor monitor-name{ // shared variable


View Full Document

LSU CSC 4103 - Deadlocks II

Download Deadlocks II
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 Deadlocks II 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 Deadlocks II 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?