CS345 05 - Mutual Exclusion

CS345 05 - Mutual Exclusion - Chapter 5 Mutual Exclusion...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 5 Mutual Exclusion
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BYU CS 345 Mutual Exclusion 2 Topics to Cover… Semaphores Producer/Consumer Bounded Buffer Message Passing Monitor Synchronization Objectives
Background image of page 2
BYU CS 345 Mutual Exclusion 3 Review… The OS must keep track of active processes. The OS must allocate and deallocate resources. Processor time Memory Files I/O devices The OS must protect the data and physical resources. The results of a process must be independent of the speed of execution relative to the speed of other concurrent processes. Mutual Exclusion
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BYU CS 345 Mutual Exclusion 4 Resource Allocation Mutual Exclusion Critical resource – a single nonsharable resource. Critical section – portion of the program that accesses a critical resource. Deadlock Each process owns a resource that the other is waiting for. Two processes are waiting for communication from the other. Starvation A process is denied access to a resource, even though there is no deadlock situation. Mutual Exclusion
Background image of page 4
BYU CS 345 Mutual Exclusion 5 Semaphores SEM_SIGNAL Producer SEM_WAIT Consumer SEM_TRYLOCK Conditional consumer Semaphores used for: Synchronization Resource Mutual Exclusion Semaphores
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BYU CS 345 Mutual Exclusion 6 Consider… P0: wait(S); wait(Q); . . . signal(S); signal(Q); P1: wait(Q); wait(S); . . . signal(Q); signal(S); Is there anything wrong here? Semaphores
Background image of page 6
BYU CS 345 Mutual Exclusion 7 The Producer-Consumer Problem repeat produce an item in nextp while (counter == n); buffer[in] = nextp in = (in + 1) mod n counter = counter + 1 until false Producer repeat while (counter == 0); nextc = buffer[out] out = (out + 1) mod n counter = counter - 1 consume the item in nextc until false Consumer Producer/Consumer Is there anything wrong here?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BYU CS 345 Mutual Exclusion 8 Autonomy Critical Semaphore operations must be atomic Uniprocessor simply inhibit interrupts (normal user can’t) Use TestAndSet to create a mutex in the calls Multiprocessor hardware must provide special support, or use software solutions semWait(Semaphore s) { while(TestAndSet(&lock)); s.value--; if (s.value < 0) { add process to s.queue * lock = FALSE; block; } else *lock = FALSE; } Semaphores
Background image of page 8
BYU CS 345 Mutual Exclusion 9 Semaphores Binary Semaphore 2 states 0 = nothing produced, maybe tasks in queue 1 = something produced, no tasks in queue Counting Semaphore Resource counter 0 = nothing produced, nothing in queue -n = nothing produced, n tasks queued +n = n items produced, no tasks in queue Semaphores
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BYU CS 345 Mutual Exclusion 10 SEM_SIGNAL - Producer Semaphores void semSignalBinary(Semaphore* semaphore) // signal binary semaphore { semaphore->state = 1; // produce (signal) binary semaphore if (tid = deQ(semaphore->queue) < 0) return; // dequeue blocked task (if any) semaphore->state = 0; // consume (clear) semaphore
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2012 for the course C S 345 taught by Professor Ericmercer during the Winter '12 term at BYU.

Page1 / 34

CS345 05 - Mutual Exclusion - Chapter 5 Mutual Exclusion...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online