Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chien-Chung Shen CIS/UD

Similar presentations


Presentation on theme: "Chien-Chung Shen CIS/UD"— Presentation transcript:

1 Chien-Chung Shen CIS/UD cshen@udel.edu
Chapter 31 Semaphores Chien-Chung Shen CIS/UD

2 Basic Ideas of Semaphores
Invented by Edsger Dijkstra as a synchronization primitive, which can be used as both locks and condition variables Use of semaphores and implementation of semaphores BTW, do you know who Dijkstra is?

3 Definition An object with one integer value and two operations, sem_wait() and sem_post() Initial value of a semaphore determines its behavior sem_t s; sem_init(&s, 0, X); // 0: shared between threads in the same process // X: initial value int sem_wait(sem_t *s) { // P decrement the value of s by 1 wait if value of s is negative } int sem_post(sem_t *s) { // V increment the value of s by 1 if there are one or more threads waiting, wake one

4 Features sem_wait() will either return right away (because value of semaphore was 1 or higher when sem_wait() is called), or it will cause the caller to suspend execution waiting for a subsequent post. Multiple calling threads may call into sem_wait(), and thus all be queued waiting to be woken sem_post() does not wait. Rather, it simply increments the value of semaphore and then, if there is any thread waiting, wakes one of them up Value of semaphore, when negative? equal to the number of waiting threads

5 Semaphore as Lock X = ? termed binary semaphore (X = 1) sem_t m;
sem_init(&m, 0, X); // initialize semaphore to X; sem_wait(&m); // critical section here sem_post(&m); X = ? termed binary semaphore (X = 1)

6 Binary Semaphore as Lock

7 Semaphore as Condition Variable
Used when a thread waits for something to happen, and a different thread makes that something happen and then signaling that it has happened, thus waking the waiting thread. sem_t s; // parent waits for child void child(void *arg) { printf("child\n"); sem_post(&s); // signal here: child is done return NULL; return NULL; } int main(int argc, char *argv[]) { sem_init(&s, 0, X); // what should X be? printf("parent: begin\n");  pthread_t c; Pthread_create(c, NULL, child, NULL); sem_wait(&s); // wait here for child printf("parent: end\n"); return 0; X = ?

8 Semaphore as Condition Variable
Parent waits for child Child runs to the end first X = 1

9 Producer/Consumer (Bounded Buffer)
single producer and single consumer buffer of 1 slot – producer waits for a buffer to become empty in order to put data into it [who can empty the buffer?], and consumer similarly waits for a buffer to become filled before taking data out [who can fill the buffer] Can I put/take data into/out of buffer? If I did, I will let the other party know P cares empty and C cares full C P

10 Bounded Buffer P/C I What about MAX > 1 and multiple producers and consumers?

11 Bounded Buffer Multiple P/C
So far so good? Deadlock! e.g., consumer runs first…….

12 Bounded Buffer Multiple P/C
Reduce scope of lock

13 Dining Philosophers by Dijkstra
Philosopher(int p) { while (1) { think(); getforks(p); eat(); putforks(p); } void getforks(int p) { sem_wait(forks[left(p)]); sem_wait(forks[right(p)]); } void putforks(int p) { sem_post(forks[left(p)]); sem_post(forks[right(p)]); sem_t forks[5]; int left(int p) { return p; } int right(int p) { return (p+1)%5; Problem? deadlock void getforks(int p) { if (p == 4) { sem_wait(forks[right(p)]); sem_wait(forks[left(p)]); } else { } change how forks are acquired by at least one of the philosophers

14 Semaphore Usage: Summary
Mutual exclusion: binary semaphore as mutex lock Controlled access to a given resource consisting of a finite number of instances: counting semaphore Semaphore is initialized to the number of instances available Synchronization: two concurrent running threads T1 and T2 with statements S1 and S2, respectively Require S2 be executed only after S1 has completed (on one CPU) Semaphore s = 0; T1: S1; signal(s); T2: wait(s); S2; T1 S1 S2 T2

15 Implementation of Semaphore

16 Implementation of Semaphore
via disabling/enabling interrupts


Download ppt "Chien-Chung Shen CIS/UD"

Similar presentations


Ads by Google