9-Process Synchronization
9-Process Synchronization
V.A.
CSED,TU
Disclaimer
VA.
CSED,TU
Process & Synchronization
Synchronization – Coordination.
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;
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
VA.
CSED,TU
Setting Final value of Counter
VA.
CSED,TU
Buffer Types
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.
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
VA.
CSED,TU
TestAndSet Instruction
Definition:
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:
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
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
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