1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion...

30
1 Interprocess Communication (IPC) - Outline • Problem: Race condition • Solution: Mutual exclusion Disabling interrupts; Lock variables; Strict alternation (busy waiting); Peterson’s solution; TSL instruction (TSL reg, LOCK and MOVE LOCK, 0) Sleep & Wakeup Producer-Consumer Problem (bounded buffer)

Transcript of 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion...

Page 1: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

1

Interprocess Communication (IPC) - Outline

• Problem: Race condition

• Solution: Mutual exclusion– Disabling interrupts;– Lock variables;– Strict alternation (busy waiting);

• Peterson’s solution;

• TSL instruction (TSL reg, LOCK and MOVE LOCK, 0)

– Sleep & Wakeup• Producer-Consumer Problem (bounded buffer)

Page 2: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

2

ME Solutions: Today

• Memory-sharing– All previous primitives/mechanisms are memory-sharing

– Semaphores as mutex and as synchronization primitive

– Monitors

• Message-passing solutions• Barriers• Classical ME / concurrency problems

– Access problems (Readers/Writers Problem)

– Synchronization problems (Dining Philosophers Problem)

– Scheduling (Sleeping Barber Problem)

Page 3: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

3

Semaphores?

• Do SW mechanisms exist for synchronization than HW TSL?• Semaphores (S): Integer Variables [System Calls]

– Accessible only through 2 standard atomic operations (i.e., operation must execute indivisibly) of:

• wait()• signal()

Wait(S) { ; sleep while S 0 ; // no-op S--; } Signal(S) { ; wakeup S++; }

S can be an integer resource counter;If S is binary, it is called a “mutex”

wait(1) progress & decrementwait(0) blocksignal(0) increment and unblock

Page 4: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

4

Binary Semaphores with TSL: Mutexes

Page 5: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

5

ME using Semaphores

do {

wait (mutex); mutex initialized to 1

// critical section

signal (mutex);

// non critical section

} while (TRUE);

Page 6: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

6

Ordering (Sync.) with Semaphores

• Consider 2 concurrent processes P1/P2 with statements S1/S2

• Would like S2 to be executed only after S1 completed

• Let P1 and P2 share a common semaphore “sync” set to 0

– [in P1] S1; Statement S1 executes in Process P1

signal(sync);

– [in P2] wait(sync);

S2; Statement S2 executes in Process P2

[sync = 0 P2 executes S2 only after P1 has invoked signal(sync); which is only after S1 has been executed]

Page 7: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

7

Semaphores

The producer-consumer problem using semaphores

mutual exclusion synchronization |

CS |

CS |

Page 8: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

8

Semaphore Constructs in Java

• Implemented by java.util.concurrent.Semaphore class• The class uses special language constructs that use the wait() and signal()

system calls

• public Semaphore available = new Semaphore(100);• • available.acquire(); //available-- ; uses wait() syscall;• available.release(); //available++; uses signal() syscall;

…and other available methods, as acquire(int n); release (int n);acquireUninterrupted() etc.

• So are higher level sync abstractions useful?

Page 9: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

9

Semaphore Problems?• Semaphore effective SW level synchronization• System calls (simple!)• But, timing errors are still possible through misuse of

Wait/Signal – process interchanges order of wait() and signal() ops on the

semaphore• wait(mutex) CS signal(mutex) signal(mutex) … CS …

wait(mutex)• several processes may end up executing their CS concurrently

– Suppose a user replaces signal(mutex) with wait(mutex)• wait(mutex) … CS … wait(mutex)• deadlock!!!

– Suppose the process omits wait(mutex) or signal(mutex) or both

• ME violated or deadlock

Page 10: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

10

Monitors: Language Constructs not System Calls

// shared variable declarations

Page 11: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

11

Monitors

Outline of producer-consumer problem with monitors- only one monitor procedure active at one time- buffer has N slots

Page 12: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

12

Monitors in Java

Solution to producer-consumer problem in Java

Page 13: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

13

Monitors in Java

Solution to producer-consumer problem in Java

Page 14: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

14

Problems?

• TSL: lowest level (HW), but busy-waits, priority inversion problem

• Let’s block instead of busy-waiting…• Semaphores: low-level (kernel), depend too much on

programmer’s skills• Monitors: need language support (C/Pascal?)• All: memory sharing solutions, work only on the same

machine but not if the processes sit in different machines (LAN etc.)

• Let’s look at message passing solutions (send/receive)

Page 15: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

15

Producer-Consumer with Message Passing

Ques: what happens if the producer (or the consumer) is muchfaster at processing messages than the consumer (or producer)?

Page 16: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

16

Barriers (primitives) for Synchronization

Use of a barrier (~ AND operation)a) processes approaching a barrierb) all processes blocked at barrier, waiting for Cc) last process (C) arrives, all are let through

Page 17: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

17

Synchronization Implementations

• Solaris– adaptive mutex, semaphores, RW locks, threads blocked by locks etc

• Windows XP– interrupt masking, busy-waiting spin locks (for short code

segments), mutex, semaphores, monitors, msg. passing

• Linux– pre v2.6 (non-preemptible); post v2.6 pre-emptible: interrupts

– semaphores, spin-locks (for short CS’s in kernel only)

Page 18: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

18

Classical ME/Concurrency Problems

• Access problems (Readers/Writers Problem)

• Synch. problems (Dining Philosophers Problem)

• Scheduling (Sleeping Barber Problem)

Page 19: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

19

Readers-Writers Problem

• A data set is shared among a number of concurrent processes– Readers – only read the database; they do not perform any updates– Writers – can both read and write.

• Problem – allow multiple readers to queue to read at the same time. Only one single writer can access the shared data at a time.

• Shared Data– Database– Integer readcount initialized to 0 (# of processes currently reading object)– Semaphore mutex initialized to 1; controls the access to readcount– Semaphore db initialized to 1; controls access to database;

Page 20: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

20

Writer/Reader Processes: Structure

while (true) {

wait (db) ;

…writing performed…

signal (db) ;

}

while (true) { wait (mutex) ; //ME for readcount

readcount ++ ; if (readcount == 1) wait (db) ; signal (mutex);

…reading performed…

wait (mutex) ; readcount - - ; if (readcount == 0) signal

(db) ; signal (mutex) ;

}initialize: db = 1, mutex = 1, readcount = 0

1 reader queued on “db”; N-1 readers queued on “mutex”

Page 21: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

21

The Readers and Writers Problem

Page 22: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

22

Dining Philosophers

• Philosophers eat/think• Eating needs 2 chopsticks (forks)• Pick one instrument at a time

• Solutions (with semaphores?)…

Page 23: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

23

Dining Philosophers – Obvious Solution

wait

signal

Deadlocks? – all pick the left fork at the same time add a check if the fork is available Livelock!

wait

signal

Page 24: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

24

Dining-Philosophers Problem

• Philosopher i:

while (true) {

…think…

wait ( chopstick[i] );wait ( chopstick[ (i + 1) % N] )

signal (…) ? random backoff?

…eat…

signal (chopstick[i] ); signal (chopstick[ (i + 1) % N] );}

Needs:• No deadlock• No starvation for anyone• Maximum parallelism• Deterministic!

Page 25: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

25

Dining Philosophers – No deadlocks -Max Parallelism Solution

Page 26: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

26

Continuation…

Deadlock free + max. parallelism (2 eat) !!!!

// acquire forks

Page 27: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

27

Bounded-Buffer Problem

• N buffers, each can hold one item

• Semaphore mutex initialized to the value 1

• Semaphore full initialized to the value 0

• Semaphore empty initialized to the value N.

Page 28: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

28

Producer/Consumer Process

while (true) {

…produce an item…

wait (empty);wait (mutex);

…add the item to the buffer…

signal (mutex);signal (full);

}

while (true) {

wait (full);

wait (mutex);

…remove an item from buffer…

signal (mutex);

signal (empty);

…consume the removed item…

}

initialize: mutex = 1, full = 0, empty = N

Page 29: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

29

The Sleeping Barber Problem

• 1 Barber• 1 Barber Chair• N Customer Chairs

Page 30: 1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

30

The Sleeping Barber Problem