Chapter-02-ipc
Chapter-02-ipc
• Three issues
– How one process can pass information to another
– Making sure two or more processes do not get into each
other’s way when engaging in critical activities
– Proper sequencing when dependencies are present
• If process A produces data and process B prints
them, B has to wait until A has produced some data
before starting to print.
1
Race Conditions
• Race conditions
– Situation where two or more processes are reading or writing some shared
data at the same time and the final result depends on who runs precisely
when
2
Critical Regions (1)
• Mutual Exclusion
– If one process is using a shared variable or file, the other
processes will be excluded from doing the same thing.
• Critical Region (Critical Section)
– A part of a program where the shared memory is accessed
• Four conditions of a good solution to critical-section
problem
– No two processes simultaneously in critical region
– No assumptions made about speeds or numbers of CPUs
– No process running outside its critical region may block
other processes
– No process must wait forever to enter its critical region
3
Critical Regions (2)
4
Mutual Exclusion with Busy Waiting (0)
• Disabling Interrupts
– Disable all interrupts just after entering its critical region and re-
enable them just before leaving it
– Unwise to give user processes to power to turn off interrupts
– It’s convenient for the kernel to disable interrupts while it is
updating variables or lists(e.g. list of ready processes) in order to
prevent an inconsistent state.
• Lock variables
– Software solution
– Use of shared(lock) variable
• When a process wants to enter its critical region, it first tests
the lock.
• If the lock is 0, the process sets it to 1 and enters the critical
region.
• If the lock is already 1, the process just waits until it becomes 0.
– The same flaw as the spooler directory
5
Mutual Exclusion with Busy Waiting (1)
• Strict Alternation solution to critical region problem
– Busy waiting
– Continuously testing a variable until some value appears
– Needs to be avoided, since it wastes CPU time
– Used only when the waiting time is expected to be very short.
– Spin lock: a lock that uses busy waiting
– Avoids all races
– Violates condition 3
• A process is blocked by the other not in its critical region
– Taking turns is not good when one of the processes is much slower than
the other.
– Requires that two processes strictly alternate in entering their critical
regions
• E.g. In spooling files, neither process would be permitted to spool
two in a row. → problem
6
Mutual Exclusion with Busy Waiting (2)
• Peterson's solution for achieving mutual exclusion
– Much simpler solution than Dekker’s solution that does not require strict
alteration
7
Mutual Exclusion with Busy Waiting (2)
8
Mutual Exclusion with Busy Waiting (3)
• TSL instruction
– TSL RX, LOCK (Test and Set Lock)
– Reads the contents of the memory word lock into register RX and then
stores a nonzero value at the memory address lock
– The operation is guaranteed to be indivisible.
– Now, we can use a shared variable, lock, to coordinate access to shared
memory with the help of TSL instruction.
9
Sleep and Wakeup
10
Sleep and Wakeup
• Producer-consumer problem
– Also known as the bounded-buffer problem
– Two processes share a common, fixed-size buffer
• producer
– Puts information into the buffer
• consumer
– Takes the information out of the buffer
– If the producer wants to put a new item in the buffer, but the
buffer is already full, it goes to sleep until the consumer
removes an item from the buffer and wakes it up.
– Similarly, if the consumer wants to remove an item from the
buffer and sees that the buffer is empty, it goes to sleep until
the producer puts something in the buffer and wakes it up.
11
Sleep and Wakeup
• Producer-consumer problem
– Race condition occurs when a wakeup set to a process that is not(yet)
sleeping is lost.
12
Semaphores
• Semaphore
– New variable type introduced by E. W. Dijkstra in 1965
• Usage
down(P) operation
critical region
up(V) operation
13
Semaphores
• down(P) operation (P for Dutch proberen(test))
– If a semaphore is greater than 0, it decrements the value
and just continues.
– If the semaphore is 0, the process is put to sleep without
completing the down for the moment.
• up(V) operation (V for verhogen(increment))
– If one or more processes were sleeping on that
semaphore, unable to complete an earlier operation, one
of them is awakened and allowed to complete its down.
– Otherwise, increments the value of the semaphore
addressed.
• Each operation above is indivisible atomic action.
– Normally implemented as system calls where all
interrupts are briefly disabled
14
Semaphores
• Solving the Producer-Consumer Problem using
Semaphores
– Semaphores solve the lost-wakeup problem
– For mutual exclusion
• Guarantees that only one process at a time reads or writes the
buffer and the associated variable
• mutex
– Makes sure the producer and consumer do not access the buffer
at the same time
– binary semaphore : initialized to 1 and used by two or more
processes to ensure that only one of them can enter its critical
region at the same time
– For synchronization
• Guarantees that the producer stops running when the buffer is
full, and the consumer stops running when it is empty
• full
– Counts the number of slots that are full
• empty
– Counts the number of slots that are empty 15
Semaphores
17
Mutexes
19
Monitors
• Problem with semaphores
– Hard to use
• Monitor
– High-level synchronization primitive
– Collection of procedures, variables, and data structures that are all
grouped together in a special kind of module or package
– Processes cannot directly access the monitor’s internal data structures.
• Mutual exclusion
– Only one process can be active in a monitor at any instant.
• Programming language construct
– Since the compiler arranges for mutual exclusion, it is much less likely
that something will go wrong.
20
Monitors
Example of a monitor
21
Monitors
• Condition variables with two operations:
– wait on a condition variable
• Causes the calling process to block on a condition variable
• Allows another process to enter the monitor
– signal on a condition variable
• Wakes up the process sleeping on the condition variable
– Used with mutual exclusion guarantee
– Signals are not accumulated.
• If a condition variable is signaled with no one waiting on it, the
signal is lost forever.
• Problems
– Should be supported by the compiler
– Inapplicable to distributed environment (multiple CPUs, each with its
own priviate memory, connected by a local network)
22
Monitors
• Use of a barrier
– processes approaching a barrier
– all processes but one blocked at barrier
– last process arrives, all are let through
28
Dining Philosophers
• Philosophers eat/think
• Eating needs 2 forks
• Pick one fork at a time
• How to prevent deadlock
29
Dining Philosophers
• A wrong solution to the dining philosophers problem
– causes deadlock
30
Dining Philosophers
• 2nd solution
– After taking the left fork, check to see if the right fork is available. If it
is not, the philosopher puts down the left one, waits for some time, and
then repeats the whole process.
– Could cause starvation
• 3rd solution
– Philosophers wait a random time instead of the same time after failing
to acquire the right-hand fork.
• 4th solution
– Protect the five statements following think by a binary semaphore
– Theoretically, this is adequate.
– But, practically, it has a performance bug: only one philosopher can be
eating at any instant.
31
Dining Philosophers