Sie sind auf Seite 1von 10

Module 3

Inter Process Communications [2.3 Tan 4th Ed]

Problems:
1. How can one process pass information to another.
2. Making sure two or more processes do not get in each other’s way, eg, two
processes in an airline reservation system each trying to grab the last seat on a
plane for a different customer.
3. 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.
Race Conditions
Situations, where two or more processes are reading or writing some shared data and
the final result depends on who runs precisely when, are called race conditions

In (write ptr)and Out (read ptr) – 2 shared


variables accessed by Proc A and Proc B
Proc A and B: adds file names to spool dir, update
write ptr (In)
Print D : reads spool dir and prints file, updates
read ptr (Out)
A and B try to write 2 file name at the ~same
time. Say A goes first, reads value of In = 7 – but is
put to sleep by CPU (clk Intr) before it can
increment In and write the file name , B runs
next, also read In=7. B writes file name in loc 7.
When A wakes up later, looks at In (7) and
overwrites loc 7. The file requested by B will
never get printed.
• Race conditions are extremely hard to debug
• increasing parallelism due to increasing numbers of cores -- race condition are
becoming more common
• To avoid races - find some way to prohibit more than one process from reading and
writing the shared data at the same time – mutual exclusion
• The part of the program where the shared memory is accessed is called the critical
region or critical section.
• To avoid races – ensure that no two processes will be in their critical regions at the
same time
For parallel processes to cooperate correctly and efficiently using shared data:
1. No two processes may be simultaneously inside their critical regions.
2. No assumptions may be made about speeds or the number of CPUs.
3. No process running outside its critical region may block any process.
4. No process should have to wait forever to enter its critical region.
Disabling Interrupts
• simplest solution is to have each process disable all interrupts just after entering
its critical region and re-enable them just before leaving it. With interrupts
disabled, no clock interrupts can occur.
• unwise to give user processes the power to turn off interrupts. Errors eg, What
they forget to turn them on again?
• In a multiprocessor system, disabling interrupts within a process would only do it
for that CPU. The other CPUs programs may still enter its critical section
• not appropriate as a general mutual exclusion mechanism for user processes.
Lock Variables
• Software solution. A single, shared (lock) variable, initially 0.
• 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.
• A 0 means that no process is in its critical region, and a 1 means that some process is
in its critical region

Flaw: Same as in the spooler case


• Suppose that one process reads the lock and sees that it is 0. Before it can set the lock
to 1, another process is scheduled, runs, and sets the lock to 1.
• When the first process runs again, it will also set the lock to 1, and two processes will
be in their critical regions at the same time.
Strict Alternation;

The integer variable turn, initially 0, keeps track of whose turn it is to enter critical region and
access shared memory.
Process 0 inspects turn, finds it to be 0, and enters its critical region.
Process 1 also finds it to be 0 and sits in a tight loop continually testing turn to check is it 1.
Continuously testing a variable until some value appears is called busy waiting. A lock that
uses busy waiting is called a spin lock.
busy waiting - should be avoided - wastes CPU time. Use only if short wait times expected
This algorithm avoid all races but violates condition 3. -- process 0 is being blocked by a
process not in its critical region:
When one of the processes (say Proc 1) is much slower than the other -Process 0 finishes its
noncritical region and goes back to the top of its loop. Unfortunately, it is not permitted to
enter its critical region now, because turn is 1 and process 1 is busy with its noncritical region
Hardware Support for Locks : TSL Instruction (Test and Set Lock)
TSL RX,LOCK :
• Reads the contents of the memory word lock into register RX and then stores a nonzero
value at the memory address lock.
• The operations of reading the word and storing into it are guaranteed to be indivisible—
no other processor can access the memory word until the instruction is finished.
• The CPU executing the TSL instruction locks the memory bus to prohibit other CPUs from
accessing memory until it is done.
Assignment – 1: Due on March 6th

Write a brief note in your own words on the following classical problems in
synchronization. Be prepared to discuss these in class.

1. The Producer-Consumer Problem


2. The Dining Philosophers Problem
3. The Readers and Writers Problem

Das könnte Ihnen auch gefallen