Beruflich Dokumente
Kultur Dokumente
based on: William Stallings Operating SystemsInternals and Design Principles, 6th ed. Pearson Education International Peter Sturm, UoTrier Operating Systems course
1. Principles of Concurrency 2. Mutual Exclusion: Hardware Support 3. Semaphores 4. Monitors 5. Message Passing 6. Readers/Writers Problem
1. Principles of Concurrency
Principles of Concurrency
Informatik CAU Kiel
Principles of Concurrency
Informatik CAU Kiel
You remember
time P1 P2 P3 Multiprogramming, one processor: interleaving P1 P2 P3 Multiprocessing, two processors: interleaving and overlapping blocked running
Principles of Concurrency
Informatik CAU Kiel
Stallings, p. 207: "At first glance, it may seem that interleaving and overlapping represent fundamentally different modes of execution and present different problems. In fact, [] both present the same problems."
Why?
Some of those problems
Sharing of global resources Optimal resource allocation Locating programming errors
7
Principles of Concurrency
Informatik CAU Kiel
A simple example
void echo() { chin = getchar(); chout = chin; putchar(chout); }
One character is read from the keyboard one keystroke at a time, stored in variable chin, and sent out to the screen for display.
Principles of Concurrency
Informatik CAU Kiel
P1's input is lost; for both P1 and P2 the same character is sent to the screen!
9
Principles of Concurrency
Informatik CAU Kiel
P1's input is lost; for both P1 and P2 the same character is sent to the screen!
10
Principles of Concurrency
Informatik CAU Kiel
Execute the instances of the echo routine such that one instance runs after the other. No parallelism and no error, but also no gain in performance
Atomicity
o
Mutual exclusion required for so-called critical sections At most one thread inside critical section at any time
11
Principles of Concurrency
Informatik CAU Kiel
12
Principles of Concurrency
Informatik CAU Kiel
Key terms
atomic operation A sequence of one or more statements that appears to be indivisible; that is, no other process can see an intermediate state or interrupt the operation. critical section A section of code within a process that requires access to shared resources and that may not be executed while another process is in a corresponding section of code. A situation in which two or more processes are unable to proceed because each is waiting for one of the others to do something. A situation in which two or more processes continuously change their state in response to changes in the other process(es) without doing any useful work.
deadlock Iivelock
mutual exclusion The requirement that, when one process is in a critical section that accesses shared resources, no other process may be in a critical section that accesses any of those shared resources.
13
Principles of Concurrency
Informatik CAU Kiel
Key terms
race condition
(cont'd.) A situation in which multiple threads or processes read and write a shared data item and the final result depends on the relative timing of their execution. A situation in which a runnable process is overlooked indefinitely by the scheduler; although it is able to proceed, it is never chosen.
starvation
14
Principles of Concurrency
Informatik CAU Kiel
Cooperation by Results of one process sharing may depend on information obtained from others Cooperation by Results of one process communication may depend on information obtained from others
Principles of Concurrency
Informatik CAU Kiel
OS must provide means to protect critical sections, e.g. enterCritical, exitCritical functions
Assume two processes P1, P2, both requesting two resources R1, R2: If P1 holds R1, and P2 holds R2 neither can proceed OS must detect and resolve deadlocks
Assume three processes P1, P2, P3, all three needing R1: If P1 and P2 use R1 in turn, P3 will suffer from starvation
16
Disabling interrupts guarantees mutual exclusion Processor is limited in its ability to interleave programs Will not work in multiprocessor architecture
19
Sequence of compare and swap cannot be interrupted by other processes. Value at *word is readable for all processes having access to shared memory
20
Compare&Swap: example
/* program mutualexclusion */ const int n = /* number of processes */; int bolt; void P(int i) { while (true) { while (compare_and_swap(bolt, 0, 1) == 1) /* do nothing */; /* critical section */; bolt = 0; /* remainder */; } void main() { bolt = 0; parbegin (P(1), P(2), ... ,P(n)); }
21
Instruction exchanges the content of a processor register with that of a memory location Exchange instruction can be found in Intel's IA-32 and IA-64 architectures
22
Exchange: example
keyi is local variable Following expression holds: bolt + keyi = n
/* program mutualexclusion */ const int n = /* number of processes */; int bolt; void P(int i) { int keyi = 1; while (true) { do exchange (keyi, bolt) while (keyi != 0); /* critical section */; bolt = 0; /* remainder */; } } void main() { bolt = 0; parbegin (P(1), P(2), ... ,P(n)); }
23
Disadvantages
Busy-waiting consumes processor time. Starvation is possible when a process leaves a critical section and more than one process is waiting. Deadlock is possible.
24
3. Semaphores
Semaphores
Informatik CAU Kiel
26
Semaphores
Informatik CAU Kiel
Mailboxes/Messages Spinlocks
27
Semaphores
Informatik CAU Kiel
Semaphores are special variables used for signaling: A process waiting for a signal is suspended until that signal is sent. Properties of semaphores
Semaphores have an integer counter value and a queue for blocked processes/threads. Counter may be initialized to a nonnegative number. Wait operation decrements the semaphore count. Signal operation increments semaphore count.
28
Semaphores
Informatik CAU Kiel
Edsger W. Dijkstra
(1930 2002)
Semaphores
Informatik CAU Kiel
Semaphore Primitives s
value queue PCB PCB PCB
31
Semaphores
Informatik CAU Kiel
semWait(s);
semWait();
Critical Section
semSignal(s);
Critical Section
semSignal();
32
Semaphores
Informatik CAU Kiel
P4
P4 P5
P5
time
Invariant
There are never more than init non-blocking semWait calls than finished semSignal calls: non-blocking #semWait calls #semSignal calls + init
33
Semaphores
Informatik CAU Kiel
34
Semaphores
Informatik CAU Kiel
Semaphores
Informatik CAU Kiel
Example
36
Semaphores
Informatik CAU Kiel
Example (cont'd.)
37
Semaphores
Informatik CAU Kiel
38
Semaphores
Informatik CAU Kiel
Producer/Consumer Problem
One or more producers are generating data and placing these in a buffer A single consumer is taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time Producer cant add data into full buffer and consumer cant remove data from empty buffer
39
Semaphores
Informatik CAU Kiel
Solution outline
producer while (true) { /* produce item v */ b[in] = v; in++; } consumer while (true) { while (in <= out) /*do nothing */; w = b[out]; out++; /* consume item w */ }
40
Semaphores
Informatik CAU Kiel
41
Semaphores
Informatik CAU Kiel
Binary semaphore solution #1 for the producer/ consumer problem with infinite buffer: incorrect!
42
Producer 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 semWaitB(s) n++ if (n==1) (semSignaIB(delay)) semSignaIB(s) semWaitB(s) n++ if(n==1) (semSignaIB(delay)) semSignaIB(s)
Consumer
s 1 0 0 0 1
n 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1
delay 0 0 0 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0
1 0 0 1 0 0 0 1
1 0 0 1 1 0 0 1
Semaphores
Informatik CAU Kiel
Binary semaphore solution #2 for the producer/ consumer problem with infinite buffer: : correct!
44
Semaphores
Informatik CAU Kiel
General semaphore solution for the producer/ consumer problem with infinite buffer
45
Semaphores
Informatik CAU Kiel
46
Semaphores
Informatik CAU Kiel
47
Semaphores
Informatik CAU Kiel
General semaphore solution for the producer/consumer problem with circular buffer
48
Semaphores
Informatik CAU Kiel
s.Wait()
s.Wait()
s.Wait()
s.Wait()
x.Wait()
y.Wait()
s.Signal() s.Signal()
s.Signal() s.Signal()
y.Signal() x.Signal()
Critical section is no critical section anymore Deadlocks Inconsistent counter values in general semaphores
Deadlocks
49
3. Monitors
Monitors
Informatik CAU Kiel
Monitor M { Shared Data; Entry f1 (Parameter) : Result { Statements; } ... Entry fn (Parameter) : Result { Statements; } }
51
C.A.R. Hoare
(*1934)
Monitors
Informatik CAU Kiel
Structure of a monitor
54
Monitors
Informatik CAU Kiel
Monitor M { Shared Data; Semaphore MonSem(1); Entry f1 (Parameter) : Result { MonSem.Wait(); Statements; MonSem.Signal(); } ... Entry fn (Parameter) : Result { MonSem.Wait(); Statements; MonSem.Signal(); } 55 }
Monitors
Informatik CAU Kiel
But
What happens, if a process/thread is blocked inside a monitor? Nobody else to enter the monitor? Deadlock? It follows a three-part story on a monitor-based solution for the producer/consumer problem.
56
Monitors
Informatik CAU Kiel
Monitor Buffer { Element b[n]; Semaphore BufferNotFull(n); Semaphore BufferNotEmpty(0); int Free := Used := 0; Entry Produce (Element e) { BufferNotFull.Wait(); b[Free] := e; Free := (Free+1) mod n; BufferNotEmpty.Signal(); } Entry Consume () : Element { Element e; BufferNotEmpty.Wait(); e = b[Used]; Used := (used+1) mod n; BufferNotFull.Signal(); return e; } } Consumer
57
Monitors
Informatik CAU Kiel
Monitor Buffer b; Element e; Bool done; Forever { Produce e; repeat done := b.Produce(e); Yield(); until done; }
Producer/Consumer Part 2:
Busy Waiting to avoid blocking inside monitor?
Entry Produce (Element e) : Bool { if (count = n) return False; else { b[Free] := e; Free := (Free+1) mod n; count++; return True; } } 58
Monitors
Informatik CAU Kiel
Monitors
Informatik CAU Kiel
CV.Wait()
o o
CV.Signal()
o o o
Some other thread k blocked at CV will become ready k must compete for monitor entrance again Different variants of Signal()
60
Monitors
Informatik CAU Kiel
cvSignal() either releases a blocked thread, if there is one, or the signal is lost as if it never happens. If cvSignal() releases a blocked thread, the caller yields the monitor or continues (see below). Only one of the caller or the released thread can continue, but not both. 61
Monitors
Informatik CAU Kiel EQ
CQ1
Condition Queues (CQ) For each condition variable a queue for all the waiting threads
SQ
Signalling Queue (SQ) Threads that are possibly blocked after issuing a signal
Signaled Queue (WQ) Threads awaiting monitor re-entrance after receiving a signal
62
Monitors
Informatik CAU Kiel
63
Monitors
Informatik CAU Kiel EQ
Whos next?
Any new thread outside who wants to enter? (EQ) Some thread that has been waiting in a Condition Queue (CQi)?
SQ
CQ1
Thread may leave CQi when some other thread emits a signal.
SQ is a special case Some versions of signal block the signaling thread and favor the signaled thread again.
64
Monitors
Informatik CAU Kiel
Priority means, if threads are waiting on all these queues, threads in queues with higher priority are favorized.
P.A. Buhr, M. Fortier, M.H. Coffin Monitor Classification ACM Computing Survey, Vol. 27, No. 1, pp. 63-107, 1995
65
Monitors
Informatik CAU Kiel
66
Monitors
Informatik CAU Kiel
67
Monitors
Informatik CAU Kiel Consumer 1 Producer
Monitor
Consumer 2
Sample problem avoided by insertion of the while loop: Some Consumer C2 might enter the monitor, before a notified Consumer C1 gets chance to take element.
68
Monitors
Informatik CAU Kiel
69
Monitors
Informatik CAU Kiel
70
Monitors
Informatik CAU Kiel EQ
CQ1
SQ
WQ Exit
71
Monitors
Informatik CAU Kiel
Semaphore c.s per cond. var. c Counter c.c counts the number of threads blocked on c
Entry fk (...) { semWait(MonSem); ... if (w.c > 0) semSignal(w.s); else semSignal(MonSem); }
72
Monitors
Informatik CAU Kiel EQ
CQ1
SQ
73
4. Message Passing
Message Passing
Informatik CAU Kiel
75
Message Passing
Informatik CAU Kiel
Synchronization
Sender and receiver may or may not be blocking (waiting for message) Blocking send
o
Blocking receive
o
Rendezvous
o
76
Message Passing
Informatik CAU Kiel
Synchronization
Non-blocking send, blocking receive
o o
Sender continues after sending irrespective of receive Receiver is blocked until the requested message arrives
77
Message Passing
Informatik CAU Kiel
Direct addressing
Send includes a specific identifier of the destination process Receive could require that the receiver explicitely designate a sender, i.e. receiver knows ahead of time from which process a message is expected Receive could use source parameter to return to the calling process when the receive operation has been performed.
78
Addressing
Informatik CAU Kiel
Indirect addressing
Messages are sent to a shared data structure consisting of queues Queues are sometimes called mailboxes One process sends a message to the mailbox and the other process picks up the message from the mailbox
79
80
81
82
Producer/Consumer Messages
Informatik CAU Kiel
83
5. Readers/Writers Problem
Readers/Writers Problem
Informatik CAU Kiel
Any number of readers may simultaneously read the file Only one writer at a time may write to the file If a writer is writing to the file, no reader may read it Note:
Readers don't write, writers don't read Producer not a writer, but a reader+writer Consumer is not a reader, but a reader+writer
85
86
87
88
Readers/Writers Problem
Informatik CAU Kiel
89
Readers/Writers Problem
Informatik CAU Kiel
90
Well understood easy material Mostly understood material is ok Hardly understood difficult material
91