Presentation is loading. Please wait.

Presentation is loading. Please wait.

CY2003 Computer Systems Lecture 04 Interprocess Communication.

Similar presentations


Presentation on theme: "CY2003 Computer Systems Lecture 04 Interprocess Communication."— Presentation transcript:

1 CY2003 Computer Systems Lecture 04 Interprocess Communication

2 © JMU, 2004CY2003-Week 042 Overview Interprocess synchronisation –race conditions –critical sections –deadlocks Mutual exclusion with busy waiting –Disabling interrupts –Lock variables –Strict alternation –Peterson’s algorithm

3 Interprocess Synchronisation

4 © JMU, 2004CY2003-Week 044 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

5 © JMU, 2004CY2003-Week 045 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

6 © JMU, 2004CY2003-Week 046 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

7 © JMU, 2004CY2003-Week 047 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 process A process B A.doc in = 8 The operating system is now in a consistent state, but B.doc will never get printed This is called a race condition - the result will depend on a race between the two processes

8 © JMU, 2004CY2003-Week 048 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

9 © JMU, 2004CY2003-Week 049 Example of Critical Sections critical region Process A critical region Process B 1. Process A starts to run Process A 2. Neither process is in a critical section, so A is allowed to enter its 3. Process B starts to run Process AProcess B 4. Process B reaches its critical section, but is not allowed to enter - the process blocks 5. Process A is reactivated and leaves critical section Process BProcess A 6. Process B is reactivated and is now allowed to enter its critical section Process AProcess B

10 © JMU, 2004CY2003-Week 0410 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; 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

11 © JMU, 2004CY2003-Week 0411 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

12 © JMU, 2004CY2003-Week 0412 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

13 Mutual exclusion with busy waiting

14 © JMU, 2004CY2003-Week 0414 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

15 © JMU, 2004CY2003-Week 0415 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

16 © JMU, 2004CY2003-Week 0416 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(); }

17 © JMU, 2004CY2003-Week 0417 Strict alternation Process 0 While (TRUE) { while (turn != 0); // wait critical_section(); turn = 1; noncritical_section(); } Process 1 While (TRUE) { while (turn != 1); // wait critical_section(); turn = 0; noncritical_section(); }

18 © JMU, 2004CY2003-Week 0418 Strict alternation Process 0 While (TRUE) { while (turn != 0); // wait critical_section(); turn = 1; noncritical_section(); } Process 1 While (TRUE) { while (turn != 1); // wait critical_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

19 © JMU, 2004CY2003-Week 0419 Strict alternation Process 0 While (TRUE) { while (turn != 0); // wait critical_section(); turn = 1; noncritical_section(); } Process 1 While (TRUE) { while (turn != 1); // wait critical_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.

20 © JMU, 2004CY2003-Week 0420 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.

21 © JMU, 2004CY2003-Week 0421 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; }

22 © JMU, 2004CY2003-Week 0422 Summary Interprocess synchronisation –race conditions –critical sections –deadlocks Mutual exclusion with busy waiting –Disabling interrupts –Lock variable –Strict variable –Peterson’s algorithm


Download ppt "CY2003 Computer Systems Lecture 04 Interprocess Communication."

Similar presentations


Ads by Google