synchronisation sorin manolache [email protected]
Post on 19-Dec-2015
226 views
TRANSCRIPT
2S. Manolache, Process programming and operating systems, Synchronisation
Last on TTIT61
Processes are executing programsKernel manages them, their state, associated data (open
files, address translation tables, register values, etc.)Threads are lightweight processes, i.e. they share data
segments, are cheaper to spawn and terminateScheduler selects next process to run among the ready
onesVarious scheduling algorithms and criteria to evaluate them
3S. Manolache, Process programming and operating systems, Synchronisation
Lecture Plan
1. What is an operating system? What are its functions? Basics of computer architectures. (Part I of the textbook)
2. Processes, threads, schedulers (Part II , chap. IV-VI)
3. Synchronisation (Part II, chap. VII)
4. Primary memory management. (Part III, chap. IX, X)
5. File systems and secondary memory management (Part III, chap. XI, XII, Part IV)
6. Security (Part VI)
4S. Manolache, Process programming and operating systems, Synchronisation
Outline
Need for synchronisationSynchronisation mechanisms
System calls: pause, select, waitpidMessage passing: blocking read/writes
Race conditions, critical regions, atomic operations, busy-waiting, hardware support for their implementation
Synchronisation mechanismsSemaphoresMonitors
Re-entrant codeDeadlockClassical problems of synchronisation
Readers/writers, bounded buffer, dining philosophers
5S. Manolache, Process programming and operating systems, Synchronisation
Need for Synchronisation
Processes that do not interact with other processes (from their functionality point of view) are independent
Processes that do interact are co-operating
E.g.:Web server and browser (client) (and all applications in
the client-server paradigm)Mail composer and spell checkersort –nr +1 -2 < grades.txt | cut –f2 | uniq | head -10
Takes a files where the grades are on the second column, sorts it in descending order of the grades, takes the second column, eliminates duplicates, and prints the highest ten grades
6S. Manolache, Process programming and operating systems, Synchronisation
Need for Synchronisation
The co-operating processes synchronise in some waySynchronous: happening at the same time (Chronos:
Greek god of time)
E.g.:Web server pumps bytes down the connection after
getting a request. Browser reads served document after the client starts sending it.
The spell checking is triggered by the appearance of new text in the composer.
7S. Manolache, Process programming and operating systems, Synchronisation
Simple Solutions: Pause
The pause system callThe processes that invokes pause blocks until it gets a
signal. The spell checker could call pause, and the mail composer could call kill –USR1 pid_of_spell_checker.
Disadvantage: The composer must know to whom it sends the signal (its pid). Also, it must have the right to signal the spell checker.
8S. Manolache, Process programming and operating systems, Synchronisation
Simple Solutions: Select
The select system callThe caller of select(fd, …) blocks until a specified
operation (read or write) is possible on the file descriptor fd without blocking.
The mail composer could write everything the user types to a pipe descriptor and the spell checker could call select on the pipe descriptor.
Disadvantage: There must be a pipe, or socket, or file shared between the caller of select and the processes that makes the unblocking read or write possible.
9S. Manolache, Process programming and operating systems, Synchronisation
Blocking Reads/Writes
The previous problems could be solved with the synchronisation mechanism of blocking reads or writes
Blocking read:The invoking process attempts to read something from a
descriptor (socket, pipe, file, message queue). If no data is available for reading, the invoking process blocks until data is available.
Blocking write:The invoking process attempts to write a data item to a
descriptor. It blocks until a process consumes the data item.
10S. Manolache, Process programming and operating systems, Synchronisation
Nachos, Lab 1, Bounded Buffer
Read Write
Blocking
Not blocking X
X
How many elements would the buffer containif the writing was blocking?
11S. Manolache, Process programming and operating systems, Synchronisation
Outline
Need for synchronisationSynchronisation mechanisms
System calls: pause, select, waitpidMessage passing: blocking read/writes
Race conditions, critical regions, atomic operations, busy-waiting, hardware support for their implementation
Synchronisation mechanismsSemaphoresMonitors
Re-entrant codeDeadlockClassical problems of synchronisation
Readers/writers, bounded buffer, dining philosophers
12S. Manolache, Process programming and operating systems, Synchronisation
Race Condition
We say that we have a race condition when, given that several processes access the same data, the execution outcome depends on the order in which the concurrent processes accessed the data.
E.g.:
buffer[index] = processID;
++index;
13S. Manolache, Process programming and operating systems, Synchronisation
Critical Sections
A critical section is a section of code that no process may execute it while another process executes it.
The execution of critical sections by processes if mutually exclusive in time
The critical section problem is to design a protocol to ensure the mutual exclusive execution
Requirements for the solution of the critical section problem:Mutual exclusionProgressBounded waiting
14S. Manolache, Process programming and operating systems, Synchronisation
Two-Process Solution (1)
do {
while (turn != 0) no-op;
critical section
turn = 1
remainder
} while (true);
Progress?
15S. Manolache, Process programming and operating systems, Synchronisation
Two-Process Solution (2)
do {
flag[0] = true;
while (flag[1]) no-op;
critical section
flag[0] = false;
remainder
} while (true);
Progress? Deadlock!
16S. Manolache, Process programming and operating systems, Synchronisation
Two-Process Solution (2’)
do {
while (flag[1]) no-op;
flag[0] = true;
critical section
flag[0] = false;
remainder
} while (true);
Mutual exclusion?
17S. Manolache, Process programming and operating systems, Synchronisation
Two-Process Solution (3)
do {
flag[0] = true;
turn = 1;
while (flag[1] && turn == 1) no-op;
critical section
flag[0] = false;
remainder
} while (true);
Works?
18S. Manolache, Process programming and operating systems, Synchronisation
Multiple-Process Solution
do {choosing[i] = true;number[i] = max(number[0], …, number[n – 1]) + 1;choosing[i] = false;for (j = 0; j < n; j++) {
while (choosing[j]) no-op;while (number[j] != 0 && (number[j], j) < (number[i], i))
no-op;}critical sectionnumber[i] = 0;remainder
} while (true);
19S. Manolache, Process programming and operating systems, Synchronisation
Busy Waiting
All algorithms on previous slides contained
while (condition) do nothing;
The process is marked as ready, and whenever running, it just wastes CPU time (and money, where one buys CPU time)
The process is said to be busy waiting
20S. Manolache, Process programming and operating systems, Synchronisation
Busy Waiting
It would be better if the waiting is supported by the OS. The process marks itself as waiting, is not anymore ready, and waits for the OS to notify it when it may wake up and become ready again.
No CPU cycles wasted, additional complexity in the OS, additional context switches
If we know that a process would wait for a very short time, busy waiting loops could be preferred to OS support.
21S. Manolache, Process programming and operating systems, Synchronisation
Hardware Support: Interrupts
Disabling interrupts when entering the critical section.Not efficient for multi-processor systems If the critical section is long, the system is not
responsive and/or it may lose events
22S. Manolache, Process programming and operating systems, Synchronisation
Hardware Support: Test and Set
boolean TestAndSet(boolean *target) {boolean rv = *target;*target = true;return rv;
}
It runs atomically, i.e. as an uninterruptible unit.do {
while (TestAndSet(lock)) no-op;critical sectionlock = false;remainder
} while (true);
23S. Manolache, Process programming and operating systems, Synchronisation
Hardware Support: Swap
do {key[i] = true;while (key[i] == true)
swap(lock, key[i]);critical sectionlock = falseremainder
} while (true);
Are these two solutions, TestAndSet-based and Swap-based, two-process solutions or multiple-process solutions?
24S. Manolache, Process programming and operating systems, Synchronisation
Outline
Need for synchronisationSynchronisation mechanisms
System calls: pause, select, waitpidMessage passing: blocking read/writes
Race conditions, critical regions, atomic operations, busy-waiting, hardware support for their implementation
Synchronisation mechanismsSemaphoresMonitors
Re-entrant codeDeadlockClassical problems of synchronisation
Readers/writers, bounded buffer, dining philosophers
25S. Manolache, Process programming and operating systems, Synchronisation
Message Passing
Synchronisation by message passing is a rather high-level mechanism
We would need something at a lower level, more general, that can be used to implement several higher-level synchronisation mechanisms
26S. Manolache, Process programming and operating systems, Synchronisation
Semaphores
Semaphores:Synchronisation tool introduced by Dijkstra with the
following two operations: P (Dutch: probeeren = to test)
– while (S0) no-op; --S; V (Dutch: verhogen = to increment)
– ++S;No modification of S is allowed to be performed by a
process while another process operates on S.Testing and decrementing of S in P has to be done
without interruption by another process.
27S. Manolache, Process programming and operating systems, Synchronisation
Non-Blocking Write, Blocking Read with Sem.
Writer:
write(fd, data item);
sem.V();
Reader:
sem.P();
data item = read(fd);
We assume an unbounded buffer associated to the descriptor fd
28S. Manolache, Process programming and operating systems, Synchronisation
Critical Sections with Semaphores
do {
sem.P();
critical section
sem.V();
remainder
} while (true)
Multi-process or two-process solution?What is the initial value of the semaphore in an n-process
solution?
29S. Manolache, Process programming and operating systems, Synchronisation
Monitors
Is a high-level synchronisation constructContains a set of programmer-defined operatorsRepresented by a set of variables (defining the state) and a
set of procedures
monitor name {shared variables declaration // the array of the BB
procedure P1(…) { // put}
procedure Pn(…) { // get}{ initialisation code} // array construction
}
30S. Manolache, Process programming and operating systems, Synchronisation
Monitors
Access on the shared data must be exclusive
Shared data
Initialisation code
operations
Conditions Entry queue
31S. Manolache, Process programming and operating systems, Synchronisation
Conditions
Conditions:Wait:
Invoking process appends itself to the queue corresponding to the condition
It releases the monitor It blocks After it is woken up, it re-acquires the monitor
Signal A blocked process from the queue of processes that
wait on the condition is woken up
Order?
32S. Manolache, Process programming and operating systems, Synchronisation
Outline
Need for synchronisationSynchronisation mechanisms
System calls: pause, select, waitpidMessage passing: blocking read/writes
Race conditions, critical regions, atomic operations, busy-waiting, hardware support for their implementation
Synchronisation mechanismsSemaphoresMonitors
Re-entrant codeDeadlockClassical problems of synchronisation
Readers/writers, bounded buffer, dining philosophers
33S. Manolache, Process programming and operating systems, Synchronisation
Re-Entrant Code
A section of code that may be executed by a process before another process leaves it
E.g.:char kernel_buffer[BUF_MAX];void user_to_kernel(int virtualAddr, char kernel_buf[]) {
int physicalAddr, i = 0;do {
physicalAddr = translate(virtualAddr);kernel_buf [i++] = c = memory[physicalAddr];
} while (c != ‘\0’);}
user_to_kernel(1000, kernel_buffer) || user_to_kernel(2000, kernel_buffer)
34S. Manolache, Process programming and operating systems, Synchronisation
Re-Entrant Code
The problem with the previous code is that both processes share the kernel_buffer array.
Do not use global variables when writing re-entrant code If you have to use them, then protect them with locks. The
protected accesses will not be re-entrant any more.
35S. Manolache, Process programming and operating systems, Synchronisation
Outline
Need for synchronisationSynchronisation mechanisms
System calls: pause, select, waitpidMessage passing: blocking read/writes
Race conditions, critical regions, atomic operations, busy-waiting, hardware support for their implementation
Synchronisation mechanismsSemaphoresMonitors
Re-entrant codeDeadlockClassical problems of synchronisation
Readers/writers, bounded buffer, dining philosophers
36S. Manolache, Process programming and operating systems, Synchronisation
Deadlock
Process A blocks if resource R is unavailableResource R may never become available again because
process B holds it and never releases itProcess B may never release it because in its turn, it waits
for a different resource, R’, that could be held by process AProcesses A and B deadlock
37S. Manolache, Process programming and operating systems, Synchronisation
Conditions for Deadlock
Mutual exclusion: At least one resource cannot be sharedHold and wait: A process must be holding at least one
resource and waiting to acquire additional resources, held by other processes
No preemption: Resources are released only voluntarilyCircular wait: P0 must wait on a resource held by P1, which
in turn waits on a resource held by P2, … held by Pn-1, which in turn waits on a resource held by P0
38S. Manolache, Process programming and operating systems, Synchronisation
Resource Allocation Graph
Processresource edge indicates resource request
Resourceprocess edge indicates that the resource is held by the process
Existence of cycles is a necessary condition for the existence of deadlocks
Also sufficient if there exists only one instance of each resource
R1
R2
R3
R4
P1 P2 P3
39S. Manolache, Process programming and operating systems, Synchronisation
Resource Allocation Graph
We have a cycle, but no deadlock
When P4 releases an instance of R2, P3 may take it
R1
R2
P1
P2
P3
P4
40S. Manolache, Process programming and operating systems, Synchronisation
Handling Deadlocks
Deadlock avoidanceDeadlock detection and recovery
41S. Manolache, Process programming and operating systems, Synchronisation
Outline
Need for synchronisationSynchronisation mechanisms
System calls: pause, select, waitpidMessage passing: blocking read/writes
Race conditions, critical regions, atomic operations, busy-waiting, hardware support for their implementation
Synchronisation mechanismsSemaphoresMonitors
Re-entrant codeDeadlockClassical problems of synchronisation
Readers/writers, bounded buffer, dining philosophers
42S. Manolache, Process programming and operating systems, Synchronisation
Classical Problems: Dining Philosophers
Dining philosophers:They may eat only when they have both forksThey cannot get both forks in one move
Implement a resource (fork) access protocol for philosophers such that they do not starve
43S. Manolache, Process programming and operating systems, Synchronisation
Readers/Writers Problem
There exists a shared resourceProcesses that do not change it upon an access are readersAll the other are writers
First readers/writers problem:No reader will be kept waiting unless a writer has already
obtained writing permission (Read accesses are not blocked if a writer manifested interest in writing)
Second readers/writers problem:Once a writer is ready, that writer performs its write as
soon as possible (Read accesses are blocked if a writer manifested interest in writing)