using semaphores

42
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems: A Programmer's Perspective”, 2/E

Upload: eadoin

Post on 23-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Using Semaphores. CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron , “Computer Systems: A Programmer's Perspective”, 2/E. Announcements. MP6 released Today A few midterm problems - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Using Semaphores

1

Using Semaphores

CS 241March 14, 2012University of IllinoisSlides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems: A Programmer's Perspective”, 2/E

Page 2: Using Semaphores

2

Announcements

MP6 releasedToday

• A few midterm problems• Using semaphores: the producer-consumer problem• Using semaphores: the readers-writers problem

Page 3: Using Semaphores

3

Midterm problem discussion

Page 4: Using Semaphores

4

Using Semaphores

Page 5: Using Semaphores

5

Before: Basic use of semaphores

void * worker( void *ptr ){ int i; for (i = 0; i < ITERATIONS_PER_THREAD; i++) { sem_wait(&cnt_mutex); cnt++; sem_post(&cnt_mutex); }}

Page 6: Using Semaphores

6

Today: Advanced use of semaphores

[Monty Python’s Flying Circus]

Page 7: Using Semaphores

7

Using semaphores:The Producer-Consumer

Problem

Page 8: Using Semaphores

8

Producer-consumer problem

Chefs cook items and put them on a conveyer belt

Waiters pick items off the belt

Page 9: Using Semaphores

9

Producer-consumer problem

Now imagine many chefs!

...and many waiters!

Page 10: Using Semaphores

10

Producer-consumer problem

A potential mess!

Page 11: Using Semaphores

11

Producer-consumer problemChef (Producer) Waiter (Consumer)

inserts items removes itemsShared resource:bounded buffer

Efficient implementation:circular fixed-size buffer

Page 12: Using Semaphores

12

Shared bufferChef (Producer) Waiter (Consumer)

Page 13: Using Semaphores

13

Shared buffer

insertPtr

removePtr

What does the chef do with a

new pizza?

Where does the waiter take a pizza from?

Chef (Producer) Waiter (Consumer)

Page 14: Using Semaphores

14

Shared buffer

insertPtr

removePtr

Insert pizza

insertPtr

Chef (Producer) Waiter (Consumer)

Page 15: Using Semaphores

15

Shared buffer

insertPtr

removePtr

Insert pizza

Chef (Producer) Waiter (Consumer)

Page 16: Using Semaphores

16

Shared buffer

insertPtr

removePtr

Insert pizza

Chef (Producer) Waiter (Consumer)

Page 17: Using Semaphores

17

Shared buffer

insertPtr

removePtr

Remove pizza

removePtr

Chef (Producer) Waiter (Consumer)

Page 18: Using Semaphores

18

Shared buffer

insertPtr

removePtr

Insert pizza

Chef (Producer) Waiter (Consumer)

Page 19: Using Semaphores

19

Shared buffer

insertPtr

removePtr

Insert pizza

Chef (Producer) Waiter (Consumer)

Page 20: Using Semaphores

20

Shared buffer

insertPtr

removePtr

BUFFER FULL: Producer must wait!

Insert pizza

Chef (Producer) Waiter (Consumer)

Page 21: Using Semaphores

21

Shared buffer

insertPtrremovePtrRemove pizza

Chef (Producer) Waiter (Consumer)

Page 22: Using Semaphores

22

Shared buffer

insertPtr

removePtr

Remove pizza

Chef (Producer) Waiter (Consumer)

Page 23: Using Semaphores

23

Shared buffer

insertPtr

removePtr

Remove pizza

Chef (Producer) Waiter (Consumer)

Page 24: Using Semaphores

24

Shared buffer

insertPtr

removePtr

Remove pizza

Chef (Producer) Waiter (Consumer)

Page 25: Using Semaphores

25

Shared buffer

insertPtr

removePtr

Remove pizza

Chef (Producer) Waiter (Consumer)

Page 26: Using Semaphores

26

Shared buffer

insertPtr

removePtr

Remove pizza

Chef (Producer) Waiter (Consumer)

Page 27: Using Semaphores

27

Shared buffer

insertPtr

removePtr

Remove pizza

Chef (Producer) Waiter (Consumer)

Page 28: Using Semaphores

28

Shared buffer

insertPtrremovePtr

Buffer empty: Consumer must be blocked!

Remove pizza

Chef (Producer) Waiter (Consumer)

Page 29: Using Semaphores

29

Designing a solutionChef (Producer) Waiter (Consumer)

Wait for empty slotInsert itemSignal item arrival

Wait for item arrivalRemove item

Signal empty slot available

What synchronization do we need?

Page 30: Using Semaphores

30

Designing a solutionChef (Producer) Waiter (Consumer)

Wait for empty slotInsert itemSignal item arrival

Wait for item arrivalRemove item

Signal empty slot available

What synchronization do we need?

Mutex(shared buffer)

Page 31: Using Semaphores

31

Designing a solutionChef (Producer) Waiter (Consumer)

Wait for empty slotInsert itemSignal item arrival

Wait for item arrivalRemove item

Signal empty slot available

What synchronization do we need?

Semaphore(# empty slots)

Page 32: Using Semaphores

32

Designing a solutionChef (Producer) Waiter (Consumer)

Wait for empty slotInsert itemSignal item arrival

Wait for item arrivalRemove item

Signal empty slot available

What synchronization do we need?

Semaphore(# filled slots)

Page 33: Using Semaphores

33

Producer-Consumer Code

buffer[ insertPtr ] = data;

insertPtr = (insertPtr + 1) % N;

result = buffer[removePtr];

removePtr = (removePtr +1) % N;

Critical Section: move insert pointer

Critical Section: move remove pointer

Page 34: Using Semaphores

34

Producer-Consumer Code

sem_wait(&slots);

mutex_lock(&mutex);

buffer[ insertPtr ] = data;

insertPtr = (insertPtr + 1) % N;

mutex_unlock(&mutex);

sem_post(&items);

sem_wait(&items);

mutex_lock(&mutex);

result = buffer[removePtr];

removePtr = (removePtr +1) % N;

mutex_unlock(&mutex);

sem_post(&slots);

Block if there are no free slots

Block if there are no items

to take

Counting semaphore – check and decrement the number of free slots

Counting semaphore – check and decrement the number of available items

Done – increment the number of available items

Done – increment the number of free slots

Page 35: Using Semaphores

35

Consumer Pseudocode: getItem()

sem_wait(&items);

pthread_mutex_lock(&mutex);

result = buffer[removePtr];

removePtr = (removePtr +1) % N;

pthread_mutex_unlock(&mutex);

sem_signal(&slots);

Error checking/EINTR handling not shown

Page 36: Using Semaphores

36

Producer Pseudocode: putItem(data)

sem_wait(&slots);

pthread_mutex_lock(&mutex);

buffer[ insertPtr ] = data;

insertPtr = (insertPtr + 1) % N;

pthread_mutex_unlock(&mutex);

sem_signal(&items);

Error checking/EINTR handling not shown

Page 37: Using Semaphores

37

Readers-Writers Problem

Page 38: Using Semaphores

38

Readers-Writers ProblemGeneralization of the mutual exclusion problemProblem statement:

• Reader threads only read the object• Writer threads modify the object• Writers must have exclusive access to the object• Unlimited number of readers can access the object

Occurs frequently in real systems, e.g.,• Online airline reservation system• Multithreaded caching Web proxy

Page 39: Using Semaphores

39

Variants of Readers-Writers

Favor readers• No reader waits unless a writer is already in critical

section• A reader that arrives after a waiting writer gets priority

over writer

Favor writers• Once a writer is ready to write, it performs its write as

soon as possible • A reader that arrives after a writer must wait, even if the

writer is also waiting

Starvation (thread waits indefinitely) possible in both cases

• Q: How could we fix this?

Page 40: Using Semaphores

40

void writer(void) { while (1) { sem_wait(&w);

/* Critical section */ /* Writing here */

sem_post(&w); }}

Writers:

int readcnt; /* Initially = 0 */sem_t mutex, w; /* Both initially = 1 */

Shared:Solution favoring readers

Page 41: Using Semaphores

41

(full codeonline)

void reader(void) { while (1) { sem_wait(&mutex); readcnt++; if (readcnt == 1) /* First reader in */ sem_wait(&w); /* Lock out writers */ sem_post(&mutex);

/* Main critical section */ /* Reading would happen here */

sem_wait(&mutex); readcnt--; if (readcnt == 0) /* Last out */ sem_post(&w); /* Let in writers */ sem_post(&mutex); }}

Readers:Solution favoring readers

Page 42: Using Semaphores

42

Summary

Synchronization: more than just locking a critical sectionSemaphores useful for counting available resources

• sem_wait(): wait for resource only if none available• sem_post(): signal availability of another resource

Multiple semaphores / mutexes can work together to solve complex problems