42
C S omputer cience F oundations Copyright c 2002–2018 UMaine School of Computing and Information Science – 1 / 20 COS 140: Foundations of Computer Science Process Synchronization: Semaphores Fall 2018

COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 1 / 20

COS 140: Foundations of Computer Science

Process Synchronization: Semaphores

Fall 2018

Page 2: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Homework, etc.

Process

Synchronization

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 2 / 20

• Reading: Chapter 20

• Homework: Exercises at end of Chapter 20

• Due: Friday, 11/9

• NOTE: Remember that Prelim II is Wednesday, 11/14!

Page 3: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 3 / 20

• Problems ⇐ processes share resource – e.g., memory or device

• Possible: both want to use the resource simultaneously and

incompatibly

Page 4: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens?

Page 5: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first;

Page 6: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first; Jill goes first;

Page 7: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first; Jill goes first; both get stuck!

Page 8: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first; Jill goes first; both get stuck!

• Example: Two tellers, two customers, Sue and Jim, with joint

account

Page 9: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first; Jill goes first; both get stuck!

• Example: Two tellers, two customers, Sue and Jim, with joint

account

◦ Joint account = shared resource

◦ Both want to put $100 in at same time, tellers update

accounts

◦ What can happen?

Page 10: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first; Jill goes first; both get stuck!

• Example: Two tellers, two customers, Sue and Jim, with joint

account

◦ Joint account = shared resource

◦ Both want to put $100 in at same time, tellers update

accounts

◦ What can happen?

• Sue or Jim go first → $200 in account

Page 11: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first; Jill goes first; both get stuck!

• Example: Two tellers, two customers, Sue and Jim, with joint

account

◦ Joint account = shared resource

◦ Both want to put $100 in at same time, tellers update

accounts

◦ What can happen?

• Sue or Jim go first → $200 in account

• Both at same time...?

Page 12: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Process synchronization

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 4 / 20

• Example: Two movers, Bob and Jill, are putting furniture into

house

◦ Both approach doorway (shared resource) simultaneously

with a chair they can’t see around

◦ What happens? Bob goes first; Jill goes first; both get stuck!

• Example: Two tellers, two customers, Sue and Jim, with joint

account

◦ Joint account = shared resource

◦ Both want to put $100 in at same time, tellers update

accounts

◦ What can happen?

• Sue or Jim go first → $200 in account

• Both at same time...? Maybe only $100 in account!

Page 13: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Race conditions

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 5 / 20

• Examples of race conditions: outcome depends on timing/speed

of participants

• Computer: processes are the “racers”

• Resource: any non-simultaneously-sharable thing

Page 14: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example of a race condition

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemory

Page 15: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example of a race condition

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemory

num = NumStds;--INTERRUPT--

Page 16: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example of a race condition

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemory

num = NumStds;--INTERRUPT--

num = NumStds;num = num + 1;Roster[num] = ’Edith’;NumStds = num; --INTERRUPT--

Edith

Page 17: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example of a race condition

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemory

num = NumStds;--INTERRUPT--

num = NumStds;num = num + 1;Roster[num] = ’Edith’;NumStds = num; --INTERRUPT--

Edith

num = num + 1;Roster[num] = ’Francine’;NumStds = num;

5

Page 18: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example of a race condition

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 6 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemory

num = NumStds;--INTERRUPT--

num = NumStds;num = num + 1;Roster[num] = ’Edith’;NumStds = num; --INTERRUPT--

num = num + 1;Roster[num] = ’Francine’;NumStds = num;

Francine

5

Page 19: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Approach: Mutual exclusion

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 7 / 20

• Identify critical regions where shared resource is being accessed

– in teach process

• If we only allow one process into its critical region at a time → no

conflict, no race condition

Page 20: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Solution requirements

Process

Synchronization

• What is it?

• Race conditions

• Example

• Mutual exclusion

Semaphores

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 8 / 20

• Result is predictable

• Solution does not depend on speed of processes

• Solution does not depend on number of CPUs

Page 21: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

One solution: Semaphores

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 9 / 20

• Semaphores:

◦ synchronization primitive

◦ guarantee mutual exclusion when used correctly

• Semaphore: data structure (and associated code) that:

◦ Keeps track of whether or not the resource is in use...

◦ ...or alternatively, keeps track of whether anyone is in their

critical region

◦ Blocks process if it tries to enter its critical region when

someone else is in theirs

• Usually includes queue of processes that are blocked

Page 22: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

One solution: Semaphores

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 9 / 20

• Semaphores:

◦ synchronization primitive

◦ guarantee mutual exclusion when used correctly

• Semaphore: data structure (and associated code) that:

◦ Keeps track of whether or not the resource is in use...

◦ ...or alternatively, keeps track of whether anyone is in their

critical region

◦ Blocks process if it tries to enter its critical region when

someone else is in theirs

• Usually includes queue of processes that are blocked

Page 23: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Using semaphores

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 10 / 20

• A semaphore S has two major procedures associated with it:

◦ P(S), or Down(S)

◦ V(S), or Up(S)

◦ (V for Dutch verhoog (increase) and P for prolaag (try to

decrease) [Dijkstra])

• Think of semaphore signaling “okay” when up, “stop!” when down

Page 24: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Using semaphores

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 10 / 20

• A semaphore S has two major procedures associated with it:

◦ P(S), or Down(S)

◦ V(S), or Up(S)

◦ (V for Dutch verhoog (increase) and P for prolaag (try to

decrease) [Dijkstra])

• Think of semaphore signaling “okay” when up, “stop!” when down

Page 25: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Using semaphores

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 11 / 20

• Initially: semaphore is “up”

Page 26: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Using semaphores

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 11 / 20

• Initially: semaphore is “up”

• Key: wrap calls to Down and Up around each process’ critical

region

Page 27: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Using semaphores

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 11 / 20

• Initially: semaphore is “up”

• Key: wrap calls to Down and Up around each process’ critical

region

◦ Suppose we have semaphore S

◦ Process wants to enter critical region, calls Down(S)

◦ If semaphore “up” then semaphore ⇒ “down”, process

continues

◦ If semaphore “down” then process blocks

◦ When process leaves critical region: call Up(S)

Page 28: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemorySemaphore S

Page 29: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemory

Down(S);num = NumStds;--INTERRUPT--

Semaphore S

Page 30: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 20

Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

4

SharedMemory

Down(S);num = NumStds;--INTERRUPT--

Semaphore S

Down(S);--BLOCK--

Page 31: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 20

5Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

SharedMemory

Down(S);num = NumStds;--INTERRUPT--

Semaphore S

Down(S);--BLOCK--

num = num +1;Roster[num]=’Francine’;NumStds = num;Up(S);

Francine

Page 32: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 20

5Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

SharedMemory

Down(S);num = NumStds;--INTERRUPT--

Semaphore S

Down(S);--BLOCK--

num = num +1;Roster[num]=’Francine’;NumStds = num;Up(S);

Francine

Why is this still down?

Page 33: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 20

6Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

SharedMemory

Down(S);num = NumStds;--INTERRUPT--

num = NumStds;num = num + 1;Roster[num] = ’Edith’;NumStds = num;Up(S);

Semaphore S

Down(S);--BLOCK--

num = num +1;Roster[num]=’Francine’;NumStds = num;Up(S);

Francine

Edith

Page 34: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

• One solution:

Semaphores

• Using semaphores

• Example

Implementation

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 12 / 20

6Ada

Betty

Cecil

Doug

1

2

3

4

5

6

7

ClassRoster

Process 1 Process 2NumStds

SharedMemory

Down(S);num = NumStds;--INTERRUPT--

num = NumStds;num = num + 1;Roster[num] = ’Edith’;NumStds = num;Up(S);

Semaphore S

Down(S);--BLOCK--

num = num +1;Roster[num]=’Francine’;NumStds = num;Up(S);

Francine

Edith

Page 35: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Data structure

Process

Synchronization

Semaphores

Implementation

• Data structure

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 13 / 20

• Semaphores are data structures

• Two parts: count and queue

◦ Count is an integer

◦ Queue contains process IDs

• This type of semaphore = mutual exclusion, or mutex,

semaphore

• Count: set to 1 for mutex semaphores

Page 36: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Procedures

Process

Synchronization

Semaphores

Implementation

• Data structure

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 14 / 20

• Can implement as separate procedures or methods of a

semaphore class

• Down() and Up() have to be atomic

• Only OS can do this!

• Usually have to ask OS for semaphores, then Down() and Up()

are accessed via system calls

Page 37: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Procedures

Process

Synchronization

Semaphores

Implementation

• Data structure

Counting Semaphores

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 15 / 20

• Down(mutex):

◦ Decrement count

◦ If < 0, then block current process

◦ Otherwise, continue (into critical region)

• Up(mutex):

◦ Increment count

◦ If there are blocked processes, allow one to continue

◦ Note: semaphore is still “down” until count is positive

• Example

Page 38: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Counting semaphores

Process

Synchronization

Semaphores

Implementation

Counting Semaphores

• Counting semaphores

• Example

• Producer–Consumer

Problem

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 16 / 20

• Mutex semaphores: just one kind

• Semaphores really count number of free resources

• Mutex: only 1 unit of resource = critical region

• Other cases: may have ≥ 1 ⇒ counting semaphores

Page 39: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Example

Process

Synchronization

Semaphores

Implementation

Counting Semaphores

• Counting semaphores

• Example

• Producer–Consumer

Problem

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 17 / 20

• Producer–consumer problem

• Two processes, shared resource of finite size

• Producer puts things into the resource

• Consumer removes them

• Producer must block when full, consumer must block when empty

Page 40: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Producer–Consumer Problem

Process

Synchronization

Semaphores

Implementation

Counting Semaphores

• Counting semaphores

• Example

• Producer–Consumer

Problem

Discussion

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 18 / 20

Semaphores: mutex, full, empty;

Set count of mutex=1, full=0,

empty=size of resource;

Producer: Consumer:

loop forever: loop forever:

Get thing to put in; Down(full);

Down(empty); Down(mutex);

Down(mutex); Take thing out;

Put thing in resource; Up(mutex);

Up(mutex); Up(empty);

Up(full); Use thing;

end loop. end loop.

Page 41: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Problems with semaphores

Process

Synchronization

Semaphores

Implementation

Counting Semaphores

Discussion

• Problems with

semaphores

• Other approaches

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 19 / 20

• Easy to make mistakes:

◦ Forget to do Up(S)

◦ Too many Down’s

◦ Crossed semaphores

• Cheating

• Too low-level

Page 42: COS 140: Foundations of Computer Sciencemainesail.umcs.maine.edu/COS140/schedule/slides/semaphores.pdf · Both approach doorway (shared resource) simultaneously with a chair they

CSomputer

cience

Foundations

Other approaches

Process

Synchronization

Semaphores

Implementation

Counting Semaphores

Discussion

• Problems with

semaphores

• Other approaches

Copyright c© 2002–2018 UMaine School of Computing and Information Science – 20 / 20

• Event counters

• Monitors

• Message passing

• Equivalence of primitives