View
221
Download
0
Category
Tags:
Preview:
Citation preview
Operating System Concepts and
Techniques Lecture 12
Interprocess communication-1
M. Naghibzadeh
ReferenceM. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011.
To order: www.iUniverse.com, www.barnesandnoble.com, or www.amazon.com
The need for communicationCommunication 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
passingIt is natural to assume dependent processes
synchronize their activitiesEven 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
Race conditionIt 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
accountTransaction 1: Transaction 2:
1-1 Read account 123’s record 2-1 Read account 123’s record1-2 Subtract $1000 from balance 2-2 Subtract $2000 from balance
1-3 Write account 123’s record 2-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
Lost updateWhat if their execution is interleaved in the following
way1-1 Read account 123’s record //Balance is $10000
1-2 Subtract $1000 from balance //Balance is $9000 but not saved2-1 Read account 123’s record //Balance is $10,000
2-2 Subtract $2000 from balance //Balance is $8,000 but not saved2-3 Write account 123’s record //Balance is $8,0001-3 Write account 123’s record //Balance is $9,000
The result is that the transaction two’s update is lostThis 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
Critical regionMost 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 resourceCritical region or critical section of a program is
that part of the program which uses a critical resource
5
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 resultsBy 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
Acceptable ME solusionsNot 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
Disabling interrupt Method
when a process is running, if it disables the whole interrupt system, the process can keep
runningThe 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
Strict alternation MethodUse 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 resourceIn the following two processes alternate using a resource infinite number of times, initial value of turn determines
which process will use it first
9
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 1while (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
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 processesProcesses must alternate to use the resource
If one accepts its difficulties he is welcome to use itPeterson’s method removes the strict alternation nature of
this methodAdvantage: It is applicable to multiprocessor systems
11
Peterson’s methodPeterson 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
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
SummaryUncontrolled attempts by concurrently running
processes (or threads) to use a shared resource may cause collision which may cause race processes may
produce incorrect resultsWe introduced many methods to guarantee mutual
exclusive (hence race-free) resource usageDisabling 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 resourcePeterson and Dekker methods remove the restriction
of alternate shared resource usage by processes
14
15
Find outThe properties of an acceptable ME problem
solutionAn example system running concurrent processes
which has raceHow strict alternation could be extended to three
processesAn example system for which we can use strict
alternationThe difference between race and critical region
How atomicity could be implemented by softwareWhat makes you feel Peterson’s method works
well
Recommended