Semaphores and Bounded Buffer - PowerPoint PPT Presentation

About This Presentation
Title:

Semaphores and Bounded Buffer

Description:

Semaphore has an initial value of 1 P() is called before a critical section V() is called after the critical section semaphore litter_box = 1; P(litter_box); ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 65
Provided by: csFsuEdu
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Semaphores and Bounded Buffer


1
Semaphores and Bounded Buffer
2
Semaphores
  • Semaphore is a type of generalized lock
  • Defined by Dijkstra in the last 60s
  • Main synchronization primitives used in UNIX
  • Consist of a positive integer value
  • Two operations
  • P() an atomic operation that waits for
    semaphore to become positive, then decrement it
    by 1
  • V() an atomic operation that increments
    semaphore by 1 and wakes up a waiting thread at
    P(), if any.

3
Semaphores vs. Integers
  • No negative values
  • Only operations are P() and V()
  • Cannot read or write semaphore values
  • Except at the initialization times
  • Operations are atomic
  • Two P() calls cannot decrement the value below
    zero
  • A sleeping thread at P() cannot miss a wakeup
    from V()

4
Binary Semaphores
  • A binary semaphore is initialized to 1
  • P() waits until the value is 1
  • Then set it to 0
  • V() sets the value to 1
  • Wakes up a thread waiting at P(), if any

5
Two Uses of Semaphores
  • 1. Mutual exclusion
  • Lock was designed to do this
  • lock-gtacquire()
  • // critical section
  • lock-gtrelease()

6
Two Uses of Semaphores
  • Mutual exclusion
  • The lock function can be realized with a binary
    semaphore semaphore subsumes lock.
  • Semaphore has an initial value of 1
  • P() is called before a critical section
  • V() is called after the critical section
  • semaphore litter_box 1
  • P(litter_box)
  • // critical section
  • V(litter_box)

7
Two Uses of Semaphores
  • 1. Mutual exclusion
  • Semaphore has an initial value of 1
  • P() is called before a critical section
  • V() is called after the critical section
  • semaphore litter_box 1
  • P(litter_box)
  • // critical section
  • V(litter_box)

litter_box 1
8
Two Uses of Semaphores
  • 1. Mutual exclusion
  • Semaphore has an initial value of 1
  • P() is called before a critical section
  • V() is called after the critical section
  • semaphore litter_box 1
  • P(litter_box) // purrr
  • // critical section
  • V(litter_box)

litter_box 1 ? 0
9
Two Uses of Semaphores
  • 1. Mutual exclusion
  • Semaphore has an initial value of 1
  • P() is called before a critical section
  • V() is called after the critical section
  • semaphore litter_box 1
  • P(litter_box)
  • // critical section
  • V(litter_box)

litter_box 0
10
Two Uses of Semaphores
  • 1. Mutual exclusion
  • Semaphore has an initial value of 1
  • P() is called before a critical section
  • V() is called after the critical section
  • semaphore litter_box 1
  • P(litter_box) // meow
  • // critical section
  • V(litter_box)

litter_box 0
11
Two Uses of Semaphores
  • 1. Mutual exclusion
  • Semaphore has an initial value of 1
  • P() is called before a critical section
  • V() is called after the critical section
  • semaphore litter_box 1
  • P(litter_box)
  • // critical section
  • V(litter_box)

litter_box 0 ?1
12
Two Uses of Semaphores
  • 2. Synchronization Enforcing some order between
    threads
  • T1 T2
  • do X
  • wait for X
  • do Y
  • wait for Y
  • do Z
  • wait for Z

13
Two Uses of Semaphores
  • 2. Synchronization
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

14
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
15
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
16
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
wait
17
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
18
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 ?1 wait_right 0
19
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 1 ? 0 wait_right 0
20
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
21
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
wait
22
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
23
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0 ?1
24
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 1 ? 0
25
Two Uses of Semaphores
  • 2. Scheduling
  • Semaphore usually has an initial value of 0
  • semaphore wait_left 0
  • semaphore wait_right 0
  • Left_Paw() Right_Paw()
  • slide_left() P(wait_left)
  • V(wait_left) slide_left()
  • P(wait_right) slide_right()
  • slide_right() V(wait_right)

wait_left 0 wait_right 0
26
Two Uses of Semaphores
  • 2. Synchronization
  • Semaphore usually has an initial value of 0
  • semaphore s1 0
  • semaphore s2 0
  • A() B()
  • write(x) P(s1)
  • V(s1) read(x)
  • P(s2) write(y)
  • read(y) V(s2)

27
Producer-Consumer with a Bounded Buffer
  • A classic problem
  • A producer put things into a shared buffer
  • A consumer takes them out

28
Problem Constraints
  • The solution involves both synchronization and
    mutual exclusion
  • Constraints
  • The consumer must wait if buffers are empty
    (synchronization constraint)
  • The producer must wait if buffers are full
    (synchronization constraint)
  • Only one thread can manipulate the buffer at a
    time (mutual exclusion)

29
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers N
  • semaphore nLoadedBuffers 0

30
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers N
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • Consumer()
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)

31
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers N
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)

32
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers N
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

33
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 2 nLoadedBuffers 0
34
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 2 nLoadedBuffers 0
35
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 2 nLoadedBuffers 0
36
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 2 ? 1 nLoadedBuffers 0
37
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 ? 0 nFreeBuffers 1 nLoadedBuffers 0
38
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 1 nLoadedBuffers 0
39
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 1 nLoadedBuffers 0
40
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 1 ? 0 nLoadedBuffers 0
41
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
42
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 ? 1 nFreeBuffers 0 nLoadedBuffers 0
43
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 ? 0 nFreeBuffers 0 nLoadedBuffers 0
44
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
45
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0 ? 1
46
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 1 ? 0
47
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
48
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 ? 1 nFreeBuffers 0 nLoadedBuffers 0
49
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 ? 0 nFreeBuffers 0 nLoadedBuffers 0
50
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
51
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
52
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
53
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 ? 1 nFreeBuffers 0 nLoadedBuffers 0
54
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 0 nLoadedBuffers 0
55
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 0 nLoadedBuffers 0
56
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 0 ? 1 nLoadedBuffers 0
57
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 nFreeBuffers 1 ? 0 nLoadedBuffers 0
58
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 1 ? 0 nFreeBuffers 0 nLoadedBuffers 0
59
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
60
Developing the Solution
  • Each constraint needs a semaphore
  • semaphore mutex 1
  • semaphore nFreeBuffers 2
  • semaphore nLoadedBuffers 0
  • Producer()
  • P(nFreeBuffers)
  • P(mutex)
  • // put 1 item in the buffer
  • V(mutex)
  • V(nLoadedBuffers)
  • Consumer()
  • P(nLoadedBuffers)
  • P(mutex)
  • // take 1 item from the
  • // buffer
  • V(mutex)
  • V(nFreeBuffers)

mutex 0 nFreeBuffers 0 nLoadedBuffers 0
61
Implementing Semaphore
  • How to implement semaphore?
  • Almost exactly like lock.
  • Using spinlock or queue
  • What hardware support is needed?
  • Interrupt disable
  • Test-and-set

62
Implementing Semaphore
class semaphore int value semaphoresemaph
ore(int i) value i semaphorep() //
disable interrupts while (value 0) //
enable interrupts // disable interrupts valu
e -- // enable interrupts
semaphorev() // disable interrupts value
// enable interrupts
63
Implementing Semaphore with test and set
class semaphore int value semaphoresemaph
ore(int i) value i semaphorep()
while (test_and_set(guard)) while (value
0) // queue the thread // guard 0 and
sleep value -- guard 0
semaphorev() while (test_and_set(guard))
if (anyone waiting) // wake up one thread
// put in on ready queue else
value guard 0
64
Semaphore in UNIX
  • Managing concurrent access to shared memory.
  • Semaphore system calls
  • Creation semget( )
  • Incr/Decr/set semop()
  • Deletion semctl(semid, 0, IPC_RMID, 0)
  • See examples seminit.c, sema.c semb.c
Write a Comment
User Comments (0)
About PowerShow.com