CS 134: Operating Systems More Synchronization 1 / 21 Overview - - PowerPoint PPT Presentation

cs 134 operating systems
SMART_READER_LITE
LIVE PREVIEW

CS 134: Operating Systems More Synchronization 1 / 21 Overview - - PowerPoint PPT Presentation

CS34 2013-05-19 CS 134: Operating Systems More Synchronization CS 134: Operating Systems More Synchronization 1 / 21 Overview CS34 Overview 2013-05-19 More Synchronization Overview Monitors Simpler Mechanisms More Synchronization


slide-1
SLIDE 1

CS 134: Operating Systems

More Synchronization

1 / 21

CS 134: Operating Systems

More Synchronization

2013-05-19

CS34

slide-2
SLIDE 2

Overview

More Synchronization Monitors Simpler Mechanisms

2 / 21

Overview

More Synchronization Monitors Simpler Mechanisms

2013-05-19

CS34 Overview

slide-3
SLIDE 3

More Synchronization

The Story So Far. . .

Mutual Exclusion

◮ Basic idea?

Semaphores

◮ Basic idea?

3 / 21

The Story So Far. . .

Mutual Exclusion

◮ Basic idea?

Semaphores

◮ Basic idea?

2013-05-19

CS34 More Synchronization The Story So Far. . .

slide-4
SLIDE 4

More Synchronization

Fairness

Just how fair do we need to be. . . ?

Our Take. . .

4 / 21

Fairness

Just how fair do we need to be. . . ?

Our Take. . .

2013-05-19

CS34 More Synchronization Fairness

slide-5
SLIDE 5

More Synchronization

Fairness

Just how fair do we need to be. . . ?

Our Take. . .

No one likes semaphores!

◮ Too low-level ◮ Too much freedom (& too strange) ◮ Too hard to get right

Need an alternative. . .

4 / 21

Fairness

Just how fair do we need to be. . . ?

Our Take. . .

No one likes semaphores!

◮ Too low-level ◮ Too much freedom (& too strange) ◮ Too hard to get right

Need an alternative. . .

2013-05-19

CS34 More Synchronization Fairness

slide-6
SLIDE 6

More Synchronization Monitors

Monitors

Monitors were devised as an alternative to semaphores

◮ High-level synchronization construct, based on classes ◮ Only one task can be running “inside” the class at a time

Declare classes like this: monitor class MyClass { public: /* method declarations only private: /* private data and private methods */ };

5 / 21

Monitors

Monitors were devised as an alternative to semaphores

◮ High-level synchronization construct, based on classes ◮ Only one task can be running “inside” the class at a time

Declare classes like this: monitor class MyClass { public: /* method declarations only private: /* private data and private methods */ };

2013-05-19

CS34 More Synchronization Monitors Monitors

slide-7
SLIDE 7

More Synchronization Monitors

Monitors

Basic idea:

◮ Only one process can be

in the monitor at a time But what about waiting?

Entrance queue of entering processes Exit local data method 1 method k initialization code

  • MONI TOR

6 / 21

Monitors

Basic idea:

◮ Only one process can be

in the monitor at a time But what about waiting?

Entrance queue of entering processes Exit local data method 1 method k initialization code
  • MONI TOR

2013-05-19

CS34 More Synchronization Monitors Monitors

slide-8
SLIDE 8

More Synchronization Monitors

Monitors

Basic idea

◮ Only one process can

be in the monitor at a time

◮ cwait(beer) waits for beer ◮ csignal(beer) signals beer

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)

  • local data

condition variables method 1 method k initialization code

  • monitor waiting area

MONI TOR

7 / 21

Monitors

Basic idea

◮ Only one process can

be in the monitor at a time

◮ cwait(beer) waits for beer ◮ csignal(beer) signals beer

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)
  • local data
condition variables method 1 method k initialization code
  • monitor waiting area
MONI TOR

2013-05-19

CS34 More Synchronization Monitors Monitors

slide-9
SLIDE 9

More Synchronization Monitors

Monitors

Basic idea

◮ Only one process can

be in the monitor at a time

◮ cwait(beer) waits for beer ◮ csignal(beer) signals beer

Entrance queue of entering processes Exit condition c1 cwait(c1) urgent queue csignal condition cn cwait(cn)

  • local data

condition variables method 1 method k initialization code

  • monitor waiting area

MONI TOR

8 / 21

Monitors

Basic idea

◮ Only one process can

be in the monitor at a time

◮ cwait(beer) waits for beer ◮ csignal(beer) signals beer

Entrance queue of entering processes Exit condition c1 cwait(c1) urgent queue csignal condition cn cwait(cn)
  • local data
condition variables method 1 method k initialization code
  • monitor waiting area
MONI TOR

2013-05-19

CS34 More Synchronization Monitors Monitors

slide-10
SLIDE 10

More Synchronization Monitors

Equivalence Claims

How could we show that

◮ Semaphores aren’t “more powerful” than monitors? ◮ Monitors aren’t “more powerful” than semaphores?

9 / 21

Equivalence Claims

How could we show that

◮ Semaphores aren’t “more powerful” than monitors? ◮ Monitors aren’t “more powerful” than semaphores?

2013-05-19

CS34 More Synchronization Monitors Equivalence Claims

slide-11
SLIDE 11

More Synchronization Simpler Mechanisms

  • Minimalism. . .

In NP-completeness, you learn SAT, and then the simpler 3-SAT, which is equivalent. Can we imagine something “less” than semaphores?

10 / 21

  • Minimalism. . .

In NP-completeness, you learn SAT, and then the simpler 3-SAT, which is equivalent. Can we imagine something “less” than semaphores?

2013-05-19

CS34 More Synchronization Simpler Mechanisms

  • Minimalism. . .
slide-12
SLIDE 12

More Synchronization Simpler Mechanisms

Binary Semaphores

Basic idea?

11 / 21

Binary Semaphores

Basic idea?

2013-05-19

CS34 More Synchronization Simpler Mechanisms Binary Semaphores

A binary semaphore is similar to test-and-set. If it’s nonzero, one one process can set it to zero and continue past bsem_dec. If it’s zero, bsem_inc sets it nonzero and wakes at least one process waiting on

  • it. Multiple calls to bsem_inc with no intervening bsem_dec will have

no effect. However, it is illegal to do that: you can’t call bsem_inc unless the semaphore value is currently zero.

slide-13
SLIDE 13

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...?

12 / 21

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...?

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores

slide-14
SLIDE 14

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...?

struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here...

12 / 21

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here...

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores

slide-15
SLIDE 15

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...?

struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count

12 / 21

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores

slide-16
SLIDE 16

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...?

struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count volatile int waiting; // How many waiting

12 / 21

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count volatile int waiting; // How many waiting

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores

slide-17
SLIDE 17

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...?

struct sem { volatile int count; // Semaphore count // +val = sem count, -val = wait count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count

12 / 21

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count // +val = sem count, -val = wait count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores

slide-18
SLIDE 18

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...?

struct sem { volatile int count; // Semaphore count // +val = sem count, -val = wait count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count };

12 / 21

Semaphores from Binary Semaphores

Assume the following binary semaphore operations: struct bsem* bsem_create (int count); void bsem_dec (struct bsem* s); void bsem_inc (struct bsem* s); Data to implement semaphores...? struct sem { volatile int count; // Semaphore count // +val = sem count, -val = wait count struct bsem* wait; // Wait here... struct bsem* mutex; // Protects count };

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores

slide-19
SLIDE 19

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores (cont.)

Initialization:

struct sem* sem_init(int count) { struct sem* s = malloc(sizeof(struct sem)); assert(s != NULL && count >= 0); s->count = count; s->mutex = bsem_create(1); // Ordinary mutex s->wait = bsem_create(0); // Mostly locked, // briefly unlocked return s; }

13 / 21

Semaphores from Binary Semaphores (cont.)

Initialization: struct sem* sem_init(int count) { struct sem* s = malloc(sizeof(struct sem)); assert(s != NULL && count >= 0); s->count = count; s->mutex = bsem_create(1); // Ordinary mutex s->wait = bsem_create(0); // Mostly locked, // briefly unlocked return s; }

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores (cont.)

slide-20
SLIDE 20

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores (cont.)

Is this code okay?

void sem_dec(struct sem* s) { bsem_dec(s->mutex);

  • -(s->count);

if (s->count < 0) { bsem_inc(s->mutex); bsem_dec(s->wait); } else { bsem_inc(s->mutex); } } void sem_inc(struct sem* s) { bsem_dec(s->mutex); ++(s->count); if (s->count <= 0) { bsem_inc(s->wait); } bsem_inc(s->mutex); }

14 / 21

Semaphores from Binary Semaphores (cont.)

Is this code okay? void sem_dec(struct sem* s) { bsem_dec(s->mutex);

  • -(s->count);

if (s->count < 0) { bsem_inc(s->mutex); bsem_dec(s->wait); } else { bsem_inc(s->mutex); } } void sem_inc(struct sem* s) { bsem_dec(s->mutex); ++(s->count); if (s->count <= 0) { bsem_inc(s->wait); } bsem_inc(s->mutex); }

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores (cont.)

There is a race after sem_dec calls bsem_inc on the mutex; we could sleep on s->wait even though s->count has become

  • nonzero. We need to ensure that there is exactly one bsem_inc per
  • wait. For example:
  • 1. Process 1 decs and stops after mutex release
  • 2. Process 2 decs and stops after mutex release
  • 3. Process 3 incs and bumps wait
  • 4. Process 4 incs and re-bumps wait (illegally)
  • 5. Process 1 continues and passes through wait
  • 6. Process 2 continues and waits forever
slide-21
SLIDE 21

More Synchronization Simpler Mechanisms

Semaphores from Binary Semaphores (cont.)

Does this version fix the problem?

void sem_dec(struct sem* s) { bsem_dec(s->mutex);

  • -(s->count);

if (s->count < 0) { bsem_inc(s->mutex); bsem_dec(s->wait); } bsem_inc(s->mutex); } void sem_inc(struct sem* s) { bsem_dec(s->mutex); ++(s->count); if (s->count <= 0) { bsem_inc(s->wait); } else { bsem_inc(s->mutex); } }

15 / 21

Semaphores from Binary Semaphores (cont.)

Does this version fix the problem? void sem_dec(struct sem* s) { bsem_dec(s->mutex);

  • -(s->count);

if (s->count < 0) { bsem_inc(s->mutex); bsem_dec(s->wait); } bsem_inc(s->mutex); } void sem_inc(struct sem* s) { bsem_dec(s->mutex); ++(s->count); if (s->count <= 0) { bsem_inc(s->wait); } else { bsem_inc(s->mutex); } }

2013-05-19

CS34 More Synchronization Simpler Mechanisms Semaphores from Binary Semaphores (cont.)

The assumption here is that if sem_dec waits, sem_inc would grab the mutex on its behalf and bump wait. So even if somebody else gets in between the release of the mutex and the wait, they will necessarily allow us to pass through. In our previous scenario:

  • 1. Process 1 decs and stops after mutex release
  • 2. Process 2 decs and stops after mutex release
  • 3. Process 3 incs and bumps wait
  • 4. Because the mutex is still held, process 4 can’t proceed. Instead,
  • ne of process 1 & 2 will continue and pass through the wait.
  • 5. Process 1 continues and releases mutex.
  • 6. Process 4 incs and bumps wait
  • 7. Process 2 can now continue and pass through wait.
slide-22
SLIDE 22

More Synchronization Simpler Mechanisms

Monitors Revisited

Basic idea

◮ Only one process can

be in the monitor at a time

◮ cwait(beer) waits for beer ◮ csignal(beer) signals beer

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)

  • local data

condition variables method 1 method k initialization code

  • monitor waiting area

MONI TOR

16 / 21

Monitors Revisited

Basic idea

◮ Only one process can

be in the monitor at a time

◮ cwait(beer) waits for beer ◮ csignal(beer) signals beer

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)
  • local data
condition variables method 1 method k initialization code
  • monitor waiting area
MONI TOR

2013-05-19

CS34 More Synchronization Simpler Mechanisms Monitors Revisited

slide-23
SLIDE 23

More Synchronization Simpler Mechanisms

Monitors without Condition Variables

Basic idea

◮ Only one process can

be in the monitor at a time Remind you of anything?

Entrance queue of entering processes Exit local data method 1 method k initialization code

  • MONI TOR

17 / 21

Monitors without Condition Variables

Basic idea

◮ Only one process can

be in the monitor at a time Remind you of anything?

Entrance queue of entering processes Exit local data method 1 method k initialization code
  • MONI TOR

2013-05-19

CS34 More Synchronization Simpler Mechanisms Monitors without Condition Variables

slide-24
SLIDE 24

More Synchronization Simpler Mechanisms

Mutexes / Locks

Like binary semaphores, but with ownership rules:

◮ You “acquire” the lock ◮ You “hold” the lock ◮ You “release” the lock

Someone else can’t release it for you.

18 / 21

Mutexes / Locks

Like binary semaphores, but with ownership rules:

◮ You “acquire” the lock ◮ You “hold” the lock ◮ You “release” the lock

Someone else can’t release it for you.

2013-05-19

CS34 More Synchronization Simpler Mechanisms Mutexes / Locks

slide-25
SLIDE 25

More Synchronization Simpler Mechanisms

Mutexes / Locks

void task(const int i) { for ( ; ; ) { lock_acquire(ourlock); critical_section_actions(i); lock_release(ourlock);

  • ther_actions(i);

} }

Class Exercise

Is bool lock_tryacquire(lock) useful?

19 / 21

Mutexes / Locks

void task(const int i) { for ( ; ; ) { lock_acquire(ourlock); critical_section_actions(i); lock_release(ourlock);

  • ther_actions(i);

} }

Class Exercise

Is bool lock_tryacquire(lock) useful?

2013-05-19

CS34 More Synchronization Simpler Mechanisms Mutexes / Locks

slide-26
SLIDE 26

More Synchronization Simpler Mechanisms

Class Exercise

Can you implement semaphores using mutexes? Can you implement mutexes using semaphores? What do mutexes remind you of?

20 / 21

Class Exercise

Can you implement semaphores using mutexes? Can you implement mutexes using semaphores? What do mutexes remind you of?

2013-05-19

CS34 More Synchronization Simpler Mechanisms Class Exercise

slide-27
SLIDE 27

More Synchronization Simpler Mechanisms

Class Exercise

Can you implement semaphores using mutexes? Can you implement mutexes using semaphores? What do mutexes remind you of? But what’s missing?

Entrance queue of entering processes Exit local data method 1 method k initialization code

  • MONI TOR

20 / 21

Class Exercise

Can you implement semaphores using mutexes? Can you implement mutexes using semaphores? What do mutexes remind you of? But what’s missing?

Entrance queue of entering processes Exit local data method 1 method k initialization code
  • MONI TOR

2013-05-19

CS34 More Synchronization Simpler Mechanisms Class Exercise

slide-28
SLIDE 28

More Synchronization Simpler Mechanisms

Condition Variables (for Mutexes)

What are the operations? What are the arguments?

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)

  • local data

condition variables method 1 method k initialization code

  • monitor waiting area

MONI TOR

21 / 21

Condition Variables (for Mutexes)

What are the operations? What are the arguments?

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)
  • local data
condition variables method 1 method k initialization code
  • monitor waiting area
MONI TOR

2013-05-19

CS34 More Synchronization Simpler Mechanisms Condition Variables (for Mutexes)

slide-29
SLIDE 29

More Synchronization Simpler Mechanisms

Condition Variables (for Mutexes)

What are the operations? What are the arguments? Do you need to hold the lock when you cond_signal or cond_broadcast?

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)

  • local data

condition variables method 1 method k initialization code

  • monitor waiting area

MONI TOR

21 / 21

Condition Variables (for Mutexes)

What are the operations? What are the arguments? Do you need to hold the lock when you cond_signal or cond_broadcast?

Entrance queue of entering processes Exit condition c1 cwait(c1) condition cn cwait(cn)
  • local data
condition variables method 1 method k initialization code
  • monitor waiting area
MONI TOR

2013-05-19

CS34 More Synchronization Simpler Mechanisms Condition Variables (for Mutexes)