CY2003Computer Systems
Lecture 04
Interprocess Communication
© JMU, 2004 CY2003-Week 04 2
Overview
• Interprocess synchronisation– race conditions
– critical sections
– deadlocks
• Mutual exclusion with busy waiting– Disabling interrupts
– Lock variables
– Strict alternation
– Peterson’s algorithm
Interprocess Synchronisation
© JMU, 2004 CY2003-Week 04 4
Basic Principles
• In the earlier discussion of processes, we have glossed over the significance and ramifications of the co-existence of processes within a system
• Several possible relationships between processes– fully independent processes
• users working on separate applications within one system– students compiling and running C programs in lab sessions
– independent but related processes• users contributing to one application
– data entry clerks adding orders to sales control system
– concurrent programming applications• applications constructed as a set of co-operating processes
© JMU, 2004 CY2003-Week 04 5
Interprocess Communication
• In a real system processes need to communicate with each other in order to co-operate
• There are several reasons for co-operating– information sharing
• the processes are interested in the same information
– computational speedup• a particular task may run faster if it can be broken down into
multiple subtasks which run in parallel
– convenience• an individual user may choose to work on many tasks at once
© JMU, 2004 CY2003-Week 04 6
Synchronisation Problems
• As soon as we allow multiple processes to share resources and communicate, there are a number of potential process synchronisation problems that may occur– race conditions
• two or more processes competing for a shared resource• the result varies according to the order of process execution
– deadlocks• a situation where two or more processes are each waiting for
resources held by others
– starvation• a process never gets access to a required resource
© JMU, 2004 CY2003-Week 04 7
A Race
fred.doc
bill.doc
jack.doc
4
5
6
7
out = 4in = 7
process A
process B
process A
in = 7
process A
process B
in = 7
B.doc
in = 8
in = 8
process A
process B
A.doc
in = 8
in = 8
The operating systemis now in a consistentstate, but B.doc willnever get printed
This is called a race condition - theresult will depend ona race between thetwo processes
© JMU, 2004 CY2003-Week 04 8
Critical Sections
• How do we avoid race conditions?– the problem is caused by two processes have free
(simultaneous) access to a shared resource• the data item ‘in’ in the example just seen
• We need to find some way to prohibit more than one process from accessing such a resource– problems only occur during shared resource access
• The part of the process which accesses a shared resource is termed a critical section– only one process must be allowed to be in a critical
section at one time: this is mutual exclusion
© JMU, 2004 CY2003-Week 04 9
Example of Critical Sections
criticalregion
Process A
criticalregion
Process B
1. Process A starts to run
Process A
2. Neither process is in a critical section, so A isallowed to enter its
3. Process B starts to run
Process A Process B
4. Process B reaches itscritical section, but is notallowed to enter - the process blocks
5. Process A is reactivatedand leaves critical section
Process BProcess A
6. Process B is reactivatedand is now allowed toenter its critical section
Process A Process B
© JMU, 2004 CY2003-Week 04 10
Flawed Attempt at Mutual Exclusion
• Let’s try an ‘obvious’ solution to achieve mutual exclusion, by setting a ‘critical_region’ flag
while ( critical_region == BUSY )do_nothing;
critical_region= BUSY;<do critical code>critical_region= FREE;
• Unfortunately, this doesn’t solve the problem!– suppose process A is interrupted just after it has
checked that the critical_region flag is FREE, but before it has run the next line to set it to BUSY
– process B then runs and checks the critical_region flag• it finds it FREE, and it too enters the critical code region
© JMU, 2004 CY2003-Week 04 11
Requirements for Solution
• The actual requirements needed for a correct and efficient solution to the mutual exclusion problem are far from obvious– no two processes may be in their critical section at the
same time– no assumptions may be made about speeds or the
number of processors– no process running outside its critical section may
block other processes– no process should have to wait forever to enter its
critical section
© JMU, 2004 CY2003-Week 04 12
Deadlocks
• Even if the mutual exclusion problem is solved, there is another related problem: deadlocks
• In many cases a process needs exclusive access to more than one resource– suppose there are two processes, A and B, and two
shared resources, printer and tape• A allocates the printer• B allocates the tape
– then• A requests the tape and blocks until B releases it
– and then• B requests the printer and blocks until A releases it
Mutual exclusion with busy waiting
© JMU, 2004 CY2003-Week 04 14
Disabling Interrupts – Allow a process to disable interrupts before it enters its
critical section and then enable interrupts after it leaves its critical section
– CPU will be unable to switch processes– Guarantees that the process can use the shared
variable without another process accessing it– But, disabling interrupts, is a major undertaking– At best, the computer will not be able to service
interrupts for, maybe, a long time– At worst, the process may never enable interrupts,
thus (effectively) crashing the computer– The disadvantages far outweigh the advantages
© JMU, 2004 CY2003-Week 04 15
Lock variable
• Another method is to assign a lock variable- For example, set the variable to (say) 1 when a
process is in its critical section and reset to zero when a processes exits its critical section
• But this is flawed as it simply moves the problem from the shared variable to the lock variable
© JMU, 2004 CY2003-Week 04 16
Strict alternation
• An integer variable turn keeps track of whose turn to enter the critical region – If this variable is zero, then process 0 enters the critical
region – process 1 waits for its turn and Continually tests the
variable.
While (TRUE){
while (turn !=n) /*wait */;critical_region();
turn = n+1;noncritical_region();
}
© JMU, 2004 CY2003-Week 04 17
Strict alternation
Process 0While (TRUE) {
while (turn != 0); // waitcritical_section();turn = 1;noncritical_section();
}
Process 1While (TRUE) {
while (turn != 1); // waitcritical_section();turn = 0;noncritical_section();
}
© JMU, 2004 CY2003-Week 04 18
Strict alternation
Process 0While (TRUE) {
while (turn != 0); // waitcritical_section();turn = 1;noncritical_section();
}
Process 1While (TRUE) {
while (turn != 1); // waitcritical_section();turn = 0;noncritical_section();
}
• Assume the variable turn is initially set to zero.• Process 0 runs. And finding turn is zero, it enters its
critical region• If process 1 tries to run, it will find that turn is zero and
will have to wait• When process 0 exits its critical region turn = 1 and
process 1 can continue• Process 0 is now blocked
© JMU, 2004 CY2003-Week 04 19
Strict alternation Process 0While (TRUE) {
while (turn != 0); // waitcritical_section();turn = 1;noncritical_section();
}
Process 1While (TRUE) {
while (turn != 1); // waitcritical_section();turn = 0;noncritical_section();
}– Process 0 runs, enters its critical section and exits; setting turn to
1. Process 0 is now in its non-critical section. Assume this non-critical procedure takes a long time.
– Process 1, which is a much faster process, now runs and once it has left its critical section turn is set to zero.
– Process 1 executes its non-critical section very quickly and returns to the top of the procedure.
– The situation is now that process 0 is in its non-critical section and process 1 is waiting for turn to be set to zero. In fact, there is no reason why process 1 cannot enter its critical region as process 0 is not in its critical region.
© JMU, 2004 CY2003-Week 04 20
Peterson’s algorithm
• Any process that is interested in entering the critical region, sets its interested value to True and sets the turn variable to the process number.
– The process calls for the enter_the_region function.
– When leaving the critical region, the process calls the leave_the_region function and sets interested to False.
© JMU, 2004 CY2003-Week 04 21
Peterson’s algorithm int turn;int interested [N];
void enter_the_region(int process){
int other;other = 1 – process;interested [process] = True;turn = process;while (turn == process && interested [other] = = True);
}
void leave_the_region (int process){
interested [process] = False;}
© JMU, 2004 CY2003-Week 04 22
Summary
• Interprocess synchronisation– race conditions
– critical sections
– deadlocks
• Mutual exclusion with busy waiting – Disabling interrupts
– Lock variable
– Strict variable
– Peterson’s algorithm