synchronization and semaphores programming language design and implementation (4th edition) by t....

12
Synchronization and semaphores Programming Language Design and Implemen tation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

Upload: richard-jackson

Post on 13-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

Synchronization and semaphores

Programming Language Design and Implementation (4th Edition)

by T. Pratt and M. ZelkowitzPrentice Hall, 2001

Section 11.2.4-11.2.5

Page 2: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

2

Issues in synchronization

Easy to do - Context switching (and statement, fork() function, task creation)

Hardware virtual memory makes it easy for operating system to create independently executing tasks in their own address space

Hard to do - Synchronization. How to pass information reliably among a set of independently executing parallel tasks.

Consider and statement discussed previously:

What is output that is printed?I = 1;I = I+1 and I = I-1 and write(I);write(I)

Both first and second write can be 0, 1 or 2. Why?

Page 3: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

3

Parallel soup

(1) I = I+1 and (2) I = I-1 and (3) write(I);(4) write(I) If execution order is (1)(2)(3)(4), output is <1,1> If execution order is (1)(3)(2)(4), output is <2,1> If execution order is (2)(3)(1)(4), output is <0,1>How to get second write of 0: (1) I=I+1 is not a single operation. It is usually 3

instructions: Load from I, Add 1, store into I. What if context switch between instructions 1 and 2? (2) I=I-1 will set I to 0 then context switch back

to (1) causes original value of I to be incremented to 2, which is printed as <2,2>

If I=I-1 is executed first, we could get <0,0>, etc.

Page 4: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

4

Critical regions

A critical region is a sequence of program statements within a task where the task is operating on some data object shared with other tasks.

Must use some mechanism to access this data: Semaphores Messages Monitors Rendezvous

Page 5: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

5

Mutual exclusion1. Mutual exclusion - Only one process can be executing

a given section of code at one time.block(X) - block semaphore X.wakeup(X) - unblock semaphore X

block(x) - if x is free, grab x, else wait until it is free

wakeup(x) - free x and start any process waiting on x

Example again:I = 1;block(A); I = I+1; wakeup(A)and block(A); I = I-1; wakeup(A)and block (A); write(I); wakeup(A)write(I)

Output: Second write is always I=1, but first write can be 0, 1 or 2, depending upon which and statement executes first.

Page 6: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

6

Semaphores

2. P-V semaphores (Dijkstra). P(X) { If X> 0 continue, else wait until X > 0.

X = X-1 (in unit time)}V(X) { X = X+1 (in unit time)}P acts as a gate to limit access to tasks.In addition, you can use the semaphore as a counter to

control how much access you need.Buffer manager:Initialization:{ counter = number of buffers available};GetBuffer: { P(counter}, return buffer and process dat

a};FreeBuffer: { Put buffer on free list; V{counter}};

In GetBuffer, if no buffers are available, the program will wait until some other process issues a FreeBuffer to unfreeze the blocked process.

Page 7: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

7

Disadvantages of Semaphore

A task can wait for only one semaphore at a time

Dead lock

Difficult to understand, debug and verify

Only work in shared memory

Page 8: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

8

Multiple address spaces

P-V assume all semaphores are addressable by all waiting tasks.When processes execute in different address spaces (e.g., on

different machines), semaphores don't work.Messages can be used:

send(A) - Send a message to pipe A.receive(A) - Receive a message on pipe A. If no pending message, wait until one shows up.

Sample Input-Process-Output loop:P1: do loop P2: do loop P3: do loopGet data; Receive(P2, data) Receive(P3, data);Send(P2,data); Process data; Print dataend Send(P3, data) end end Synchronization can be handled by sending messages in both

directions:Pipe AB sends from A to B.Pipe BA sends from B to A.

Page 9: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

9

Problem: Deadlock

Process A:

Block(X); Block(Y); ... Do something

Wakeup(X); Wakeup(Y);

Process B:

Block(Y); Block(X); ... Do something

Wakeup(X); Wakeup(Y)

If process A does Block(X) at same time process B does Block(Y), then

Both will succeed and then Both will wait for the other resource forever An unlikely occurrence, but it CAN happen.

Page 10: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

10

Synchronization in Ada

Rendezvous in Ada: Sender issues call DataReady Receiver issues accept DataReady. Either waits for other to reach this point.

accept DataReady do -- Entry point for task for sender to do call DataRead

y -- process synchronization taskend;

How to prevent receiver of task to be blocked- Use guarded if (select statement) ...

Page 11: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

11

Ada rendezvous

select

when Device1Status = ON => accept Ready1 do . . .

end;

or when Device2Status = ON => accept Ready2 do . . .

end;

or when Device3Status = connected => accept Ready3 do

. . . end;

else . . . -- No device is ready; do something

end select;

rendezvous - Task waits for either of Device 1, 2, or 3, and if none are ON, does something else

Page 12: Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.2.4-11.2.5

12

Monitors

A monitor is a shared data object together with the set of operations that may manipulate it (i.e., an abstract data type)

A task may manipulate the shared data object only by

using the defined operations (i.e., data is encapsulated)

To enforce mutual exclusion, require that at most one of the operations defined for the data object may be executing at any given time.

We could implement monitors in Java or C++ as classes