Lecture 06
Lecture 06
Cooperating Processes
• Once we have multiple processes or threads, it is
likely that two or more of them will want to
communicate with each other
• Process cooperation (i.e., interprocess
communication) deals with three main issues
– Passing information between processes/threads
– Making sure that processes/threads do not interfere with
each other
– Ensuring proper sequencing of dependent operations
• These issues apply to both processes and theads
– Initially we concentrate on shared memory mechanisms
1
Cooperating Processes
• An independent process cannot affect or be
affected by the execution of another process.
• A cooperating process can affect or be affected by
the execution of another process
• Advantages of process cooperation
– Information sharing
– Computation speed-up
– Modularity
– Convenience
2
Cooperating Processes Approach
• Any approach to process cooperation requires that
– No two processes may be simultaneously inside their critical
regions
– No assumptions may be made about speeds or the number of
CPUs
– No process running outside of its critical region may block
other processes
– No process should have to wait forever to enter its critical
region
Cooperating Processes
Consider a shared, bounded-buffer
public class Buffer {
private volatile int count = 0;
private volatile int in = 0, out = 0;
private Object[] buffer = new Object[10];
3
Cooperating Processes
The remove() operation on the bounded-buffer
// consumer calls this method
public Object remove() {
Object item;
while (count == 0)
; // do nothing
// remove an item from the buffer
--count;
item = buffer[out];
out = (out + 1) % buffer.length;
return item;
}
Cooperating Processes
The enter() operation on the bounded-buffer
4
Mutual Exclusion with Busy Waiting
Mutual exclusion via busy waiting / spin locks
5
Mutual Exclusion with Busy Waiting
Mutual exclusion and busy waiting with hardware support
6
Mutual Exclusions with Semaphores
• Mutual exclusion using semaphores
– Busy waiting versus blocking
• Semaphores can use busy waiting, but generally they are
implemented using blocking
• With blocking, a process/thread that fails to acquire the
semaphore does not loop continuously, instead it is blocked
and another thread is scheduled
• This is more efficient since it does not waste CPU cycles and
allows other processes/threads to make progress
7
Deadlock with Semaphores
• Deadlock is when two or more processes are waiting
indefinitely for an event that can be caused by only one of
the waiting processes
• Let S and Q be two semaphores initialized to 1
P0 P1
P(S); P(Q);
P(Q); P(S);
M M
V(S); V(Q);
V(Q) V(S);
• Starvation is indefinite blocking when a process is never
removed from the semaphore queue in which it was
suspended
8
Mutual Exclusion with Monitors
Example of a monitor
9
Process Cooperation with Messages
• IPC using message passing
• Barrier
– Processes perform computation and approach a barrier
– Processes block when they reach the barrier after
finishing their computation
– Once all processes arrives, then all are unblocked
10
Cooperation Process Problems
• Classic synchronization problems
– Bounded-buffer
– Readers-writers
– Dining philosophers
– Sleeping barber
11
Linux Kernel Control Paths
• In the simplest case, the CPU executes a kernel control
path sequentially from the first instruction to the last
• The CPU interleaves kernel control paths when
– A context switch occurs
– An interrupt occurs
• Interleaving kernel control paths is necessary to
implement preemptive multitasking, but it also
improves throughput
• Care must be taken when modifying data structures in
kernel control paths to avoid corruption
12
Linux Synchronization Techniques
• Nonpreemptability of processes in Kernel Mode
– The following assertions always hold in Linux
• No process running in kernel mode may be replaced by
another process, except when it voluntarily releases the CPU
• Interrupt or exception handlers can interrupt a process in
kernel mode, but the CPU must return to the same kernel
control path
• A kernel control path performing interrupt handling can only
be interrupted by another interrupt handler
– This simplifies kernel control paths dealing with
nonblocking systems calls, they are atomic
– Data structures not modified by interrupt handlers can be
safely accessed
13
Linux Synchronization Techniques
• Atomic operations
– Linux provides special functions that are implemented as
atomic assembly language instructions, such as
• atomic_read(v)
• atomic_set(v, i)
• atomic_add(i, v)
• atomic_sub(i, v)
• atomic_inc(v)
• atomic_dec(v)
• atomic_dec_and_test(v)
• ...
14
Linux Synchronization Techniques
• Locking
– Before entering a critical region, the kernel control path
acquires a lock for that region
– There are two locking mechanisms
• Kernel semaphores
– Suspends corresponding process if busy
• Spin locks
– Kernel control path busy waits
– Only useful in multiprocessor systems
15
Linux Synchronization Techniques
• Avoid deadlock on semaphores
– Deadlocks can only occur when a kernel control path
requires multiple semaphores to enter a critical region
– It is rare that a kernel control path needs multiple
semaphores
– In the cases that a kernel control path does need multiple
semaphores, they are always acquired in the order of their
addresses: lowest address is acquired first
16