l10-semaphore-monitor - W4118: semaphore and monitor...

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

View Full Document Right Arrow Icon
W4118: semaphore and monitor Instructor: Junfeng Yang
Background image of page 1

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

View Full DocumentRight Arrow Icon
Outline Semaphores Producer-consumer problem Monitors and condition variables 2
Background image of page 2
3 Semaphore motivation Problem with lock : ensures mutual exclusion, but no execution order Producer-consumer problem : need to enforce execution order Producer : create resources Consumer : use resources bounded buffer between them Execution order: producer waits if buffer full, consumer waits if buffer empty E.g., $ cat 1.txt | sort | uniq | wc
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Semaphore definition A synchronization variable that contains an integer value Can’t access this integer value directly Must initialize to some value • sem_init (sem_t *s, int pshared, unsigned int value) Has two operations to manipulate this integer • sem_wait (or down(), P() ) • sem_post (or up(), V() ) int sem_post(sem_t *s) { increment the value of semaphore s by 1 if there are 1 or more threads waiting, wake 1 } int sem_wait(sem_t *s) { wait until value of semaphore s is greater than 0 decrement the value of semaphore s by 1 }
Background image of page 4
5 Semaphore uses: mutual exclusion Mutual exclusion Semaphore as mutex Binary semaphore: X=1 Mutual exclusion with more than one resources Counting semaphore : X>1 Initialize to be the number of available resources // initialize to X sem_init(s, 0, X) sem_wait(s); // critical section sem_post(s);
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Semaphore uses: execution order Execution order One thread waits for another What should initial value be? //thread 0 … // 1 st half of computation sem_post(s); // thread 1 sem_wait(s); … //2 nd half of computation
Background image of page 6
How to implement semaphores? Exercise
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/18/2012 for the course INFORMATIK 2011 taught by Professor Phanthuongcang during the Winter '11 term at Cornell University (Engineering School).

Page1 / 22

l10-semaphore-monitor - W4118: semaphore and monitor...

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

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