Process Synchronization
Organized By: Vinay Arora
V.A.
CSED,TU
Disclaimer
This is NOT A COPYRIGHT MATERIAL
Content has been taken mainly from the following books:
Operating Systems Concepts By Silberschatz & Galvin,
Operating systems By D M Dhamdhere,
System Programming By John J Donovan
etc…
VA.
CSED,TU
Process & Synchronization
Process – Program in Execution.
Synchronization – Coordination.
Independent process cannot affect or be affected by the execution of another
process
Cooperating process can affect or be affected by the execution of another
process
Advantages of process cooperation
Information sharing
Computation speed-up
Modularity
Convenience
VA.
CSED,TU
Buffer
VA.
CSED,TU
Bounded-Buffer – Shared-Memory Solution
Shared Data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer [BUFFER_SIZE];
int in = 0;
int out = 0;
Can only use BUFFER_SIZE-1 elements
VA.
CSED,TU
Producer – Consumer
VA.
CSED,TU
Bounded-Buffer – Producer
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
VA.
CSED,TU
Bounded Buffer – Consumer
while (true) {
while (in == out)
; // do nothing -- nothing to consume
// remove an item from the buffer
item = buffer[out];
out = (out + 1) % BUFFER SIZE;
return item;
}
VA.
CSED,TU
Setting Final value of Counter
VA.
CSED,TU
Buffer Types
Paradigm for cooperating processes, producer process produces information that
is consumed by a consumer process
Unbounded-buffer places no practical limit on the size of the buffer
Bounded-buffer assumes that there is a fixed buffer size
VA.
CSED,TU
RC & CS
Race Condition – Where several processes access and manipulate the same data
concurrently and the outcome of the execution depends on the particular order
in which access takes place.
Critical Section – Segment of code in which Process may be changing common
variables, updating a table, writing a file and so on.
VA.
CSED,TU
Peterson’s Solution
VA.
CSED,TU
Peterson’s Solution
Var flag : array [0…1] of Boolean;
Turn : 0..1;
Begin
Flag[0] = false;
Flag[1] = false;
Parbegin
Repeat Repeat
Flag[0] = true Flag[1] = true
Turn = 1 Turn = 0
While flag[1] && turn==1 While flag[0] && turn==0
Do {nothing}; Do {nothing};
{Critical Section} {Critical Section}
Flag[0] = false; Flag[1] = false;
{Remainder} {Remainder}
Forver; Forver;
Parend
end
VA.
CSED,TU
Peterson’s Solution
VA.
CSED,TU
Synchronization Hardware
Many Systems provide hardware support for critical section code
Uni-Processors – Could disable Interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Modern machines provide special atomic hardware instructions
Atomic :- Uninterruptible
Either Test memory word and Set value
Or Swap contents of two memory words
VA.
CSED,TU
TestAndSet Instruction
Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
VA.
CSED,TU
Solution using TestAndSet
Shared Boolean variable Lock, Initialized to FALSE.
Solution:
do {
while ( TestAndSet (&lock ))
; /* do nothing
// critical section
lock = FALSE;
// remainder section
} while ( TRUE);
VA.
CSED,TU
Swap Instruction
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
VA.
CSED,TU
Solution using Swap
Shared Boolean variable lock initialized to FALSE, Each process has a
local Boolean variable key.
Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
// critical section
lock = FALSE;
// remainder section
} while ( TRUE);
VA.
CSED,TU
Semaphore
Synchronization tool that does not require busy waiting
Semaphore S – Integer Variable
Two standard operations modify S: wait() and signal()
Originally called P() and V()
Less complicated
Can only be accessed via two indivisible (atomic) operations
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
VA.
CSED,TU
Producer Consumer
VA.
CSED,TU
Solution to PC must satisfy 3 conditions
VA.
CSED,TU
Solution to PC with Busy Wait
VA.
CSED,TU
Solution to PC with Signaling
VA.
CSED,TU
Indivisible Operations for PC
VA.
CSED,TU
Reference List
Operating Systems Concepts By Silberschatz & Galvin,
Operating systems By D M Dhamdhere,
System Programming By John J Donovan,
www.os-book.com
www.cs.jhu.edu/~yairamir/cs418/os2/sld001.htm
http://gaia.ecs.csus.edu/~zhangd/oscal/pscheduling.html
http://www.edugrid.ac.in/iiitmk/os/os_module03.htm
http://williamstallings.com/OS/Animations.html
etc…
VA.
CSED,TU
Thnx…
VA.
CSED,TU