+ All Categories
Home > Documents > Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan...

Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan...

Date post: 17-Dec-2015
Category:
Upload: horatio-lamb
View: 213 times
Download: 0 times
Share this document with a friend
24
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar
Transcript
Page 1: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Operating SystemsCMPSC 473Mutual Exclusion

Lecture 13: October 12, 2010

Instructor: Bhuvan Urgaonkar

Page 2: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Mid-semester feedback

• On Angel, format similar to SRTEs• Please submit by end of the week

Page 3: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Agenda• Last class

– Condition variables– Semaphores

• Next: More on condition variables and semaphores

Page 4: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Issues/Questions from Last Class

• Improving concurrency of producer/consumer solution using condition variables

• Atomicity of wait() for semaphores• # inteleavings for concurrent code with synchronization constraints

Page 5: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

cond_t not_full, not_empty;mutex_lock m;int count == 0;

Produce() { mutex_lock (m);if (count == N) wait (not_full,m);

… ADD TO BUFFER, count++ …

signal (not_empty);mutex_unlock (m);

}

Consume() {mutex_lock (m);if (count == 0) wait (not_empty,m);

… REMOVE FROM BUFFER, count-- …

signal (not_full);mutex_unlock (m);

}

NOTE: You can improve this code for more concurrency!

Page 6: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

cond_t not_full, not_empty;mutex_lock m;int count == 0;

Produce() { mutex_lock (m);if (count == N) wait (not_full,m);

count++;find pos to add // pos local variable

mutex_unlock (m);

… ADD TO BUFFER at pos …

signal (not_empty);}

Consume() {mutex_lock (m);if (count == 0) wait (not_empty,m);count--;find pos to remove from // pos local variablemutex_unlock (m);

… REMOVE FROM BUFFER at pos …

signal (not_full);}

Page 7: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation:

Atomicity• With busy wait• Entire wait (S) must be atomic– Note: Error in last class slide which said only S-- needed to be atomic!

– How?•Disable interrupts•Or use another mutex solution, e.g., lock

Page 8: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation with no Busy waiting

• With each semaphore there is an associated waiting queue. A waiting queue has two data items:– value (of type integer)– pointer to a list of PCBs

• Introduce a pointer in the PCB structure

• 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.

• FIFO ordering => bounded-waiting Is busy waiting completely gone?

Page 9: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation with no Busy waiting (Cont.)• Implementation of wait:

wait (S){ value--; if (value < 0) {

add this process to waiting queue

block(); } }• Implementation of signal: Signal (S){ value++; if (value <= 0) {

remove a process P from the waiting queue

wakeup(P); } }

Page 10: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation with no Busy waiting: Atomicity• Implementation of wait:

wait (S){ value--; if (value < 0) {

add this process to waiting queue

block(); } }• Implementation of signal: Signal (S){ value++; if (value <= 0) {

remove a process P from the waiting queue

wakeup(P); } }

How to make these atomic?

Page 11: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation with no Busy waiting: Atomicity

• Uniprocessors– Disable interrupts during wait and signal

• Once interrupts are disabled, instructions from different processes cannot be interleaved. Only the currently running process executes until interrupts are re-enabled and the scheduler can regain control.

• Multi-processors– Inhibiting interrupts does not work– Instructions from different processes (running on different processors) may be interleaved in some arbitrary way

– If the hardware does not provide any special instructions, we can employ any of the correct software solutions (e.g., Peterson’s, Bakery) for the critical-section problem, where the critical sections consist of the wait and signal operations (EXTREMELY COOL)

• So, we have not completely eliminated busy waiting with this definition of wait and signal– We have moved busy waiting to the critical sections– Furthermore, we have limited busy waiting to the critical sections of wait and signal

Page 12: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation with no Busy waiting: Atomicity

• Uniprocessors– Disable interrupts during wait and signal

• Once interrupts are disabled, instructions from different processes cannot be interleaved. Only the currently running process executes until interrupts are re-enabled and the scheduler can regain control.

• Multi-processors– Inhibiting interrupts does not work– Instructions from different processes (running on different processors) may be interleaved in some arbitrary way

– If the hardware does not provide any special instructions, we can employ any of the correct software solutions (e.g., Peterson’s, Bakery) for the critical-section problem, where the critical sections consist of the wait and signal operations (EXTREMELY COOL)

• So, we have not completely eliminated busy waiting with this definition of wait and signal– We have moved busy waiting to the critical sections– Furthermore, we have limited busy waiting to the critical sections of wait and signal

Page 13: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation with no Busy waiting: Atomicity

• Uniprocessors– Disable interrupts during wait and signal

• Once interrupts are disabled, instructions from different processes cannot be interleaved. Only the currently running process executes until interrupts are re-enabled and the scheduler can regain control.

• Multi-processors– Inhibiting interrupts does not work– Instructions from different processes (running on different processors) may be interleaved in some arbitrary way

– If the hardware does not provide any special instructions, we can employ any of the correct software solutions (e.g., Peterson’s) for the critical-section problem, where the critical sections consist of the wait and signal operations (EXTREMELY COOL)

• So, we have not completely eliminated busy waiting with this definition of wait and signal– We have moved busy waiting to the critical sections– Furthermore, we have limited busy waiting to the critical sections of wait and signal

Page 14: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Semaphore Implementation with no Busy waiting: Atomicity

• Uniprocessors– Disable interrupts during wait and signal

• Once interrupts are disabled, instructions from different processes cannot be interleaved. Only the currently running process executes until interrupts are re-enabled and the scheduler can regain control.

• Multi-processors– Inhibiting interrupts does not work– Instructions from different processes (running on different processors) may be interleaved in some arbitrary way

– If the hardware does not provide any special instructions, we can employ any of the correct software solutions (e.g., Peterson’s) for the critical-section problem, where the critical sections consist of the wait and signal operations (EXTREMELY COOL)

• So, we have not completely eliminated busy waiting with this definition of wait and signal– We have moved busy waiting to the critical sections– Furthermore, we have limited busy waiting to the critical sections of wait and signal

Page 15: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Sample incorrect use of semaphores• 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 1P0 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.

Page 16: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Compare locks, CVs, semaphores

• Ease of use• Busy wait

– Generally, CVs/semaphores better– Locks good for small critical sections on multi-processors

• Signaling capabilities

Page 17: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Classical Problems of Synchronization

• Bounded-Buffer Problem• Readers and Writers Problem• Dining-Philosophers Problem

Page 18: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Bounded-Buffer Problem

• N buffers, each can hold one item• Semaphore mutex initialized to the value 1

• Semaphore full initialized to the value 0

• Semaphore empty initialized to the value N

Page 19: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Bounded Buffer Problem (Cont.)

• The structure of the producer process while (true) { // produce an item wait (empty); wait (mutex); // add the item to the buffer

signal (mutex); signal (full); }

Page 20: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Bounded Buffer Problem (Cont.)• The structure of the consumer process

while (true) { wait (full); wait (mutex); // remove an item from buffer

signal (mutex); signal (empty); // consume the removed item

}

Page 21: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Readers-Writers Problem

• A data set shared among a number of concurrent processes– Readers – only read the data set; they do not perform any updates

– Writers – can both read and write

• Problem – allow multiple readers to read at the same time. Only one writer may access the shared data at a given time

• Shared Data– Data set– Semaphore mutex initialized to 1– Semaphore wrt initialized to 1– Integer readcount initialized to 0

Page 22: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Readers-Writers Problem (Cont.)

• The structure of a writer process do { wait (wrt) ;

// writing is performed

signal (wrt) ;

} while (TRUE);

mutex = 1wrt = 1readcount = 0

Allow only one writer at a time to write

Page 23: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Readers-Writers Problem (Cont.)

• The structure of a reader processdo {

wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ;

signal (mutex) // reading

wait (mutex) ; readcount - - ; if (readcount == 0)

signal (wrt) ; signal (mutex) ; } while (TRUE);

mutex = 1wrt = 1readcount = 0

Proceed only if no writer is writing; disallowwriters once weproceed

Signal a writeronly when thereare no moreactive readers

Note 1: readcountkeeps track of the number of active readers

Note 2: Understand the role of mutex (consider whathappens without it)

Page 24: Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.

Readers-Writers Problem (Cont.)

• The structure of a reader processdo {

wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ;

signal (mutex) // reading

wait (mutex) ; readcount - - ; if (readcount == 0)

signal (wrt) ; signal (mutex) ; } while (TRUE);

mutex = 1wrt = 1readcount = 0

Proceed only if no writer is writing; disallowwriters once weproceed

Signal a writeronly when thereare no moreactive readers

Deadlock?

Starvation?


Recommended