39
L10: Semaphores I Este texto se distribuye bajo los t´ erminos de la Creative Commons License esar S´ anchez Grado en Ingenier´ ıa Inform´ atica Grado en Matem´ aticas e Inform´ atica Universidad Polit´ ecnica de Madrid Fri, 6-March-2015 Under construction! Do not print

L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

1

L10: Semaphores I

Este texto se distribuye bajo los terminos de la Creative Commons License

Cesar Sanchez

Grado en Ingenierıa InformaticaGrado en Matematicas e Informatica

Universidad Politecnica de Madrid

Fri, 6-March-2015

Underco

nstruct

ion! Donot prin

t

Page 2: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

2

Mapa Conceptual

Terminology:

atomic race conditioninterleaving busy-waitmutual exclusion critical sectiondeadlock livelockliveness semaphores

Concurrency = Simultaneous + Nondeterminism + Interaction

Interaction = Communication | Synchronization

Synchronization = Mutual Exclusion | ConditionalSynchronization

Page 3: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

3

HW4: Garantizar exclusion mutua con semaforos

Homework:HW1: Creacion de threads en JavaHW2: Provocar una condicion de carreraHW3: Garanatizar la exclusion mutua con espera activaHW4: Garantizar la exlusion mutua con semaforos

Fecha de Cierre:Miercoles 11-Marzo-2015 11am

Entrega online:http://lml.ls.fi.upm.es/~entrega

Page 4: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

3

HW4: Garantizar exclusion mutua con semaforos

Page 5: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

4

Problems with Busy-Wait

Busy-wait algorithms (Peterson, Dekker, Bakery) are correct, but

hard to prove correctnesswaste of CPUatomic actions are too fine grain and depend on the architecture

problems

Page 6: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

4

Problems with Busy-Wait

Busy-wait algorithms (Peterson, Dekker, Bakery) are correct, but

hard to prove correctnesswaste of CPUatomic actions are too fine grain and depend on the architecture

solution

problems

Page 7: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

4

Problems with Busy-Wait

Busy-wait algorithms (Peterson, Dekker, Bakery) are correct, but

hard to prove correctnesswaste of CPUatomic actions are too fine grain and depend on the architecture

solution

semaphores

problems

stop threads (like join)based on atomic test and sethigher level, easier to reason about

no busy-wait

Page 8: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

5

Semaphores

stop threads (like join)no busy-waitbased on atomic test and setsupported by most hardwarehigher level, easier to reason aboutassertion based reasoning feasable

Page 9: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

5

Semaphores

stop threads (like join)no busy-waitbased on atomic test and setsupported by most hardwarehigher level, easier to reason aboutassertion based reasoning feasable

Architecture independentPresent in many OSs and languages

Page 10: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

5

Semaphores

stop threads (like join)no busy-waitbased on atomic test and setsupported by most hardwarehigher level, easier to reason aboutassertion based reasoning feasable

Allow to solve easily mutual-exclusion by n processes

Architecture independentPresent in many OSs and languages

We will learn how to use them, and their limitations

Page 11: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

6

What is a semaphore

Resembles (only remotely) traffic lights:if allowed, you passif not allowed, you wait

Page 12: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

6

What is a semaphore

Resembles (only remotely) traffic lights:if allowed, you passif not allowed, you wait

A semaphore is a data-type with two operations:

P()

V()

Page 13: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

6

What is a semaphore

Resembles (only remotely) traffic lights:if allowed, you passif not allowed, you wait

A semaphore is a data-type with two operations:

P()

V()

Internally, a semaphore maintains a counter, which is initialize atcreation.

semaphore s(3);

...

s.P();

...

s.P();

s.count←3

Page 14: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.P():{〈if s.count==0 then block〉〈if s.count!=0 then s.count--〉

Page 15: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.P():{〈if s.count==0 then block〉〈if s.count!=0 then s.count--〉

s.P()s.count==0

Example 1:

Page 16: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.P():{〈if s.count==0 then block〉〈if s.count!=0 then s.count--〉

s.P()s.count==0

Example 1:

Page 17: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.P():{〈if s.count==0 then block〉〈if s.count!=0 then s.count--〉

Example 2:

s.P()s.count==1

Page 18: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.P():{〈if s.count==0 then block〉〈if s.count!=0 then s.count--〉

Example 2:

s.P()s.count==1

s.count==0

Page 19: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.P():{〈if s.count==0 then block〉〈if s.count!=0 then s.count--〉

Example 2:

s.P()s.count==1

s.count==0Atomically!

Page 20: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.V():{〈if no blocked then s.count++〉〈if some blocked then unblock one thread

Page 21: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.V():{〈if no blocked then s.count++〉〈if some blocked then unblock one thread

Example 3:

s.P()s.count==0

s.V()

Page 22: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

7

Semantics of semaphores

Semantics of s.V():{〈if no blocked then s.count++〉〈if some blocked then unblock one thread

Example 3:

s.P()s.count==0

s.V()

s.count==0

s.count==0

Page 23: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

8

Questions

1. Could a semaphore become negative?

Page 24: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

8

Questions

1. Could a semaphore become negative?

2. Could a semaphore (initialized to ≥ 0) become negative?

Page 25: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

8

Questions

1. Could a semaphore become negative?

2. Could a semaphore (initialized to ≥ 0) become negative?

3. Let s be a semaphore be initialized to ≥ 0. Is the following true?:

If a thread is blocked then s.count==0

Page 26: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

8

Questions

1. Could a semaphore become negative?

2. Could a semaphore (initialized to ≥ 0) become negative?

3. Let s be a semaphore be initialized to ≥ 0. Is the following true?:

If a thread is blocked then s.count==0

4. Let s be a semaphore be initialized to ≥ 0. Is the following true?:

If s.count==0 then a thread is blocked

Page 27: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

9

Semaphores in Java

In HW4 you must usees.upm.babel.cclib.Semaphore

Page 28: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

9

Semaphores in Java

In HW4 you must usees.upm.babel.cclib.Semaphore

You must import with:import es.upm.babel.cclib.Semaphore;

Page 29: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

9

Semaphores in Java

In HW4 you must usees.upm.babel.cclib.Semaphore

You must import with:import es.upm.babel.cclib.Semaphore;

Create a semaphore:Semaphore s(1);

Page 30: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

9

Semaphores in Java

In HW4 you must usees.upm.babel.cclib.Semaphore

You must import with:import es.upm.babel.cclib.Semaphore;

Create a semaphore:Semaphore s(1);

Acquire a semaphore:s.await();

Page 31: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

9

Semaphores in Java

In HW4 you must usees.upm.babel.cclib.Semaphore

You must import with:import es.upm.babel.cclib.Semaphore;

Create a semaphore:Semaphore s(1);

Acquire a semaphore:s.await();

Release a semaphore:s.signal();

Page 32: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

10

Operations Names

P()

V()

await()

signal() release()

acquire()

Original cclib java

Page 33: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

10

Operations Names

P()

V()

await()

signal() release()

acquire()

decrement_or_block_if_the_result_is_negative()

wake_a_waiting_process_if_any_or_increment()

Original cclib java

Page 34: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

11

Fairness

Q: Which blocked thread is awaken?

Page 35: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

11

Fairness

Q: Which blocked thread is awaken?

A: non-deterministic

Page 36: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

11

Fairness

Q: Which blocked thread is awaken?

A: non-deterministic

Q: Can a blocked thread always loose against others?

Page 37: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

11

Fairness

Q: Which blocked thread is awaken?

A: non-deterministic

Q: Can a blocked thread always loose against others?

A: It depends

Page 38: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

11

Fairness

Q: Which blocked thread is awaken?

A: non-deterministic

Q: Can a blocked thread always loose against others?

A: It depends

Fairness: a thread cannot be blocked forever(if its semaphore is ready repteadely)

Page 39: L10: Semaphores I · Do Fri, 6-March-2015 not rint. 2 Mapa Conceptual Terminology: atomic race condition interleaving busy-wait mutual exclusioncritical section deadlock livelock

11

Fairness

Q: Which blocked thread is awaken?

A: non-deterministic

Q: Can a blocked thread always loose against others?

A: It depends

Fairness: a thread cannot be blocked forever(if its semaphore is ready repteadely)

Typically, threads are awaken in the order they were blocked.

The Java implementation has a flag to force fairness (by defaultit need not be)

cclib is fair