operating system concepts and techniques lecture 13
DESCRIPTION
Operating System Concepts and Techniques Lecture 13. Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed., iUniverse Inc., 2011. To order: www.iUniverse.com , www.barnesandnoble.com , or www.amazon.com. - PowerPoint PPT PresentationTRANSCRIPT
Operating System Concepts and
Techniques Lecture 13
Interprocess communication-2
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
Dekker’s solutionDekker has a similar solution to Peterson’s,
turn and desire are initialized firstint desire[2]= {0,0}; int turn =0;
void GetPermission( int process){ desire[process] = true;
while ( desire[1-process] == true) { if ( turn != process) desire[process] = false;
while (turn != process); desire [process] =true;
}}
void FreePermission(int process){ turn =1-process;
desire [process] = false;}
2
Test and set lock (tsl) Method
In this method we use some help from hardware A machine instruction which does two actions in one instruction, test the content of a memory location and
set it equal to one
Its scientific name is test and set lock
3
load register, x store #1, x // Store integer 1 into xtsl register, x
Test and set lock (tsl) Method…The procedure-based
implementation of these two operations will look like:
void GetPermission (void) {
ASM; top: tsl register, x
cmp register, #0 jnz top
ENDASM;}
void FreePermission (void) {
x = 0;}
4
An actual tsl-like implementationSome IBM Machines use TS
GetPermission TS x
BNZ *-4 //Go back 4 bytes, if not zero
FreePermission MVI x,’00’
Pentium processors use BTS (bit test and set)
Motorola processors have TAS (test and set operand)
Test and set lock method is not restricted to two processes
not restricted to single processor but it also suffers from busy-wait
5
Swap (or exchange) methodA shared variabel, say S, shows whether the resource
is available, i.e., S=0, or taken, i.e., S=1An atomic operation called exchange will exchange
the content of two memory locations uninterruptablyEach process will use the following procedures,
correctly and in proper places to use the resource
6
void GetPermission (void) { int p = 1; while (p != false) exchange (p, S);}void FreePermission(void){ S = 0;}
•The exchange method is applicable in the single-processor or multiprocessor environment•It is applicable for two or more processes.
Busy-wait sideeffect Busy-wait-based methods may
sometimes cause priority inversion When there is no high priority process a low
priority starts Enters a critical region to use a resource A higher priority process arrives and the
system preempts the low priority and starts the high priority
This process requests the same resource Cannot get it because lower priority is not
finished with it, therefore no progress on either processes
7
Semaphore-Based IPC method
Think of a semaphore as an abstract data type It includes data objects and operations (methods) on these
data objects The whole entity is encapsulated to prevent manipulation of
the data objects by any operation except the methods
It has one protected integer data and two methods Down (or wait): checks the content of the semaphore
variable; if it is grater than zero decrements it by one and continues Otherwise, puts the process in the waiting queue
Up (or signal): increments the content of semaphore variable by one; if there is any process waiting for this
semaphore wakes one up to continue its execution; the process that has just wakened up must do its down
operation again
8
Semaphore typesBinary: takes any of the values zero and one; it
is usually initialized to one It is used for the cases where only one process can
use the resource at any given time For example, there can be one professor teaching in
the class
Integer: takes any non-negative integer to a maximum; it is usually initialized tot to its
maximum It is used for the cases where one or more processes
can simultaneously use the resource For example, there can be many students
simultaneously participating in a university classroom
9
Producer-consumer problem
Let’s see semaphore in action in a race-free solution to the producer-consumer problem
Producer process(es) produce entities and put them in the shared storage
Consumer(s) take entities from the shared storage and use them
For example, all simultaneous processes in a system could be producers of output files to be printed
Al printers could be consumers The queue of file names to be printed is the common
storage
The size of buffer queue is limited to say BC slots
10
Producer-consumer problem..
We start the solution by listing all constraints and assigning one semaphore to each constraint Enforce mutual exclusion in using the entity-buffer
Make sure not to put new entities in a full buffer Make sure not to remove articles from an empty buffer
We now assign one semaphore to each constraint mutex , for mutual exclusion enforcement
occupied, to show how many slots of the buffer are occupied
available, to show how many slots are available
Recall that the operation available=BC-accupied is not allowed as we are only allowed to use down
and up operations on semaphores
11
Producer-consumer problem..
Semaphores have to be initialized #define true 1
#define BC 1000 // Buffer capacity typedef int semaphore; //Defines the keyword
semaphore semaphore available = BC; //All buffer slots are
available semaphore occupied = 0; //Buffer is empty
semaphore mutex =1; //Shared Resource is
12
Producer procedurevoid producer (void)
{ struct buffer_slot entity;
while (true) { produce_an_entity(&entity);
wait (&available); //Wait for a free slot wait (&mutex); //Get permission
insert_the_entity(&entity); //Put entity in queue signal (&mutex); //Free resource
signal (&occupied); //One slot is filled }
}
13
Consumer procedurevoid consumer (void)
{ struct buf_slot entity;
while (true) { wait (&occupied); //Wait until there is an
entity wait (&mutex);
remove_the_entity(&entity); signal (&mutex);
signal (&available); consume_the_entity(&entity);
}}
14
SummaryThe test-and-set-lock method solves the mutual exclusion
problem with the help of a machine instruction provided only for this purpose
Another similar method is the swap method. This tries to obtain the permission to use a shared resource by swapping
the value of a local variable with the value of a global variableSome processors have a machine instruction that can swap
the contents of two memory locations in one machine instruction, otherwise we have to implement it by software These methods (except the disable interrupt) all suffer from
busy-wait consumption of CPU time, however the waste may not be high and OS implementers may decide to use them
A semaphore-based method is a busy-wait free form of guaranteeing mutual exclusion
It is general and efficient
15
16
Find outA machine instruction in your computer’s
processor which works like tslA machine instruction in your computer’s
processor which works like swapThe percent of CPU time which is wasted due to busy-wait in a system running three concurrent
processes sharing one critical resourceAn operating system problem scenario which
resembles producer-consumer problemWhy busy-wait causes priority inversion
Which of the ME problem solutions you like the best and why
17
Any questions?