Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.

Similar presentations


Presentation on theme: "Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and."— Presentation transcript:

1 Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., To order: or

2 The need for communication Communication is essential among all societies Processes (or threads) form a society in the world inside computers They can communicate by using a variety of techniques, such as shared memory and message passing It is natural to assume dependent processes synchronize their activities Even independent processes synchronize using shared resources in order not to interfere with one another We will discuss methodologies and tools for safe and efficient communication between processes 2

3 Race condition It is difficult to believe correct programs may sometimes produce incorrect results, i.e., race condition occurs Consider two transactions, one wants to withdraw $1000 and the other $2000 from a checking account Transaction 1:Transaction 2: 1-1 Read account 123’s record2-1 Read account 123’s record 1-2 Subtract $1000 from balance2-2 Subtract $2000 from balance 1-3 Write account 123’s record2-3 Write account 123’s record  With round robin scheduler the execution of processes could interleave in numerous ways  The system only guarantees that when a machine instruction starts it will continue to the end 3

4 Lost update What if their execution is interleaved in the following way 1-1 Read account 123’s record //Balance is $ Subtract $1000 from balance //Balance is $9000 but not saved 2-1 Read account 123’s record //Balance is $10, Subtract $2000 from balance //Balance is $8,000 but not saved 2-3 Write account 123’s record //Balance is $8, Write account 123’s record //Balance is $9,000 The result is that the transaction two’s update is lost This couldn’t have happened if transactions are executed one after the other The same situation may happen within the OS, for example, a message may be lost instead of an update 4

5 Critical region Most computer resources are shared in a long time but must be used by a single user exclusively in short times A classroom chair may be used by many students during one day, however at any given time only one person uses it The case is similar for printer, scanner, a record of a database, a queue of waiting processes etc. Such a resource is called a critical resource Critical region or critical section of a program is that part of the program which uses a critical resource 5

6 Mutual Exclusion (ME) In order to safely use a critical resource we must follow the following steps Get permission to enter critical region Enter critical region and use the resource Leave the critical region and free the resource By proper usage of critical resources race condition will not happen, hence correct programs produce correct results By guaranteeing a long-term shared resource to be used solely for short terms such that no more than one process is using the resource in any given time we say, mutual exclusion problem is solved 6

7 Acceptable ME solusions Not all solutions to ME problem are acceptable; an acceptable solution must have the following four properties The number of processes simultaneously using a resource must be, at the most, equal to the maximum number of allowable processes Mutual exclusion must be upheld disrespectful of relative execution speed of processes and the number of processors. A process may only block other processes from using a resource when inside its critical A process does not have to wait forever to get its chance to use a needed critical resource We can now talk about techniques and tools 7

8 Disabling interrupt Method when a process is running, if it disables the whole interrupt system, the process can keep running The steps to insure exclusive usage of a critical resource in a single-processor are: Disable interrupt Use the critical resource Enable interrupt Difficulties: Only the interrupt system of one processor is disabled, doesn’t work in multiprocessor environment The whole system is degraded to single programming Disable interrupt is a privileged instruction 8

9 Strict alternation Method Use a shared variable such as turn It can have only one value at any given time, zero or one Process zero executes the following statement before using the resource, a similar statement is for Process one GetPermission: If (turn != 0) goto GetPermission; Or similarly GetPermission: while (turn !=0); Change turn when finished with using the resource In the following two processes alternate using a resource infinite number of times, initial value of turn determines which process will use it first 9

10 Turn =0; // Process 0 while (true) { GetPermission: if (turn != 0) goto GetPermission; /* Critical section where the resource is used by this process */ turn = 1; //Set turn to process 1 to get permission and use the resource } // Process 1 while (true) { GetPermission: if (turn != 1) goto GetPermission; /* Critical section where the resource is used by this process */ turn = 0; // Set turn to process 0 to get permission and use the resource } 10

11 Difficulties of Strict alternation Method It suffers from busy-waiting If one process is using the resource and the processor switches to the other process, it keeps checking to see when its turn comes up; making processor busy while waiting for an event It is only good for two processes Processes must alternate to use the resource If one accepts its difficulties he is welcome to use it Peterson’s method removes the strict alternation nature of this method Advantage: It is applicable to multiprocessor systems 11

12 Peterson’s method Peterson used the shared variable turn and a two- element array desire that are defined as int turn; int desire [2]; Every process which needs to use the resource will call the GetPermission procedure with its process id which is eithr zero or one void GetPermission (int process) { int other; // Variable Other defines other process’s number other = 1-process; desire [process] = true; //This process wants to use the resource turn = process; while (turn== process && desire [other] == true) ; //Busy-wait } 12

13 Peterson’s method… To leave its critical region and free resource the process will call void FreePermission (int process) { desire [process] = false; } Advantages : removes strict alternation Good for multiprocessor systems Disadvantages: It suffers from busy-waiting It is only good for two processes If one accepts its difficulties he is welcome to use it m 13

14 Summary Uncontrolled attempts by concurrently running processes (or threads) to use a shared resource may cause collision which may cause race processes may produce incorrect results We introduced many methods to guarantee mutual exclusive (hence race-free) resource usage Disabling interrupt method works well for single processor systems; this can only run in a protected mode to run a very short procedure A strict alternation method is for synchronizing two processes to alternate when using a shared resource Peterson and Dekker methods remove the restriction of alternate shared resource usage by processes 14

15 15 Find out The properties of an acceptable ME problem solution An example system running concurrent processes which has race How strict alternation could be extended to three processes An example system for which we can use strict alternation The difference between race and critical region How atomicity could be implemented by software What makes you feel Peterson’s method works well

16 16 Any questions?


Download ppt "Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and."

Similar presentations


Ads by Google