0% found this document useful (0 votes)
49 views

Process Synchn-2

The document discusses process synchronization techniques using hardware instructions and semaphores. It describes: 1) Hardware instructions like test-and-set and swap that can be used to implement mutual exclusion in a critical section problem. These provide an atomic way for processes to access shared memory without interrupts. 2) Semaphores that use wait and signal operations to synchronize processes. A semaphore is an integer variable accessed through these operations. Wait blocks a process if the semaphore is negative, while signal increments the semaphore. 3) An example showing how semaphores can be used to control process synchronization, ensuring a process performs an action only after another process completes its task.

Uploaded by

Sukhchain Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Process Synchn-2

The document discusses process synchronization techniques using hardware instructions and semaphores. It describes: 1) Hardware instructions like test-and-set and swap that can be used to implement mutual exclusion in a critical section problem. These provide an atomic way for processes to access shared memory without interrupts. 2) Semaphores that use wait and signal operations to synchronize processes. A semaphore is an integer variable accessed through these operations. Wait blocks a process if the semaphore is negative, while signal increments the semaphore. 3) An example showing how semaphores can be used to control process synchronization, ensuring a process performs an action only after another process completes its task.

Uploaded by

Sukhchain Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Process Synchronization -2

Synchronization Hardware
• Many systems provide hardware support for critical section
code
• Uniprocessors – could disable interrupts
– Currently running code would execute without
preemption
– Generally too inefficient on multiprocessor systems
• Operating systems using this not broadly scalable
• Modern machines provide special atomic hardware
instructions
• Atomic = non-interruptable
– Either test memory word and set value
– Or swap contents of two memory words
Test-and-Set Instruction

• Definition:

boolean TestAndSet (boolean target)


{
boolean rv = target;
target = TRUE;
return rv:
}
Solution using Test-and-Set
• Shared boolean variable lock, initialized to
false.
• Solution:
while (true) {
while ( TestAndSet (lock ))
do no-op;
Critical section
lock = FALSE;
Remainder section
}
Swap Instruction

• Definition:

void Swap (boolean a, boolean b)


{
boolean temp = a;
a = b;
b = temp:
}
Solution using Swap
• Shared Boolean variable lock initialized to
FALSE.
• Each process has a local Boolean variable key.
• Solution:
while (true) {
key = TRUE;
while ( key == TRUE)
Swap (lock, key );
Critical section
lock = FALSE;
Remainder section
}
Properties of Machine Instruction
Approach
Advantages
– Applicable to n number of processes on a single
processor or multiple processors sharing main
memory
– Simple and easy to verify
– Can support multiple CS; each CS can be defined by
its own variable
Disadvantages
– Busy waiting
– Possible starvation
– Possible Deadlocks
Algorithm satisfying all CS Reqmts
var waiting : array[0..n-1] of boolean
lock: boolean
These data structures are initialized to false
Bounded Waiting Mutual Exclusion
with Test-and-Set
var j:0,..n-1
key :boolean
repeat
waiting[i] = true;
key = true;
while waiting[i] and key do key =Test-and-Set(lock);
waiting[i]=false;
Critical Section
j=i+1 mod n;
while(j<>i) and (not waiting[j]) do j = j+1 mod n;
if j = i then lock = false;
else waiting[j] = false

Remainder Section

until false
Proof
• How ME Reqmt met
Process Pi can enter its CS only if either waiting[i] =
false or key = false.
key can become false only if Test-and-Set is executed.
First process to execute Test-and-Set will find key=false;
all others must wait
waiting[i] can become false only if another process leaves
CS.
Only one waiting[i] is set to false
Proof…
• How Progress Reqmt is met?
A process exiting the CS either sets lock to false
or sets waiting[j] to false
Both allow a process to enter its CS
• How Bounded waiting is met?
A process leaving CS scans the array waiting in
the cyclic ordering (i+1, i+2, …n-1,0,1,2..,i-1)
The first process in this ordering with
waiting[j]=true is designated to enter CS. Any
process waiting to enter CS will do so within n-1
turns.
Semaphores
Fundamental Principle
• Two or more processes can cooperate by means of
simple signals, such that a process can be forced to stop
at a specified place until it has received a specific signal.

• For signaling special variables called semaphores are


used.

• To transmit a signal via semaphore s, a process


executes the primitive signal(s).

• To receive a signal via semaphore s, a process executes


the primitive wait(s) – if the corresponding signal has not
yet been transmitted, the process is suspended until the
txn takes place.
Definition
• A semaphore S is an integer variable that is
accessed only through
– Initialization operations
– Two standard indivisible operations : wait and signal

• Semaphore takes only non negative values

• Definition of wait and signal :


wait(S) : while S<=0 do no-op;
S=S-1;
signal(S) : S=S+1;
Semaphore
• Race conditions on shared data can be avoided if all
operations on shared data are made indivisible.
Semaphores facilitates this.

• When a process performs a wait operation on a


semaphore, the op either blocks the process on the
semaphore or allows it to continue execution after
decrementing the value of the semaphore.

• A signal operation activates a process blocked on the


semaphore by 1.

• Indivisibility of the wait and signal operations is ensured


by the OS/programming language/machine architecture
Semaphore
• The initial value of the semaphore
determines how many processes can get
past the wait operation.

• A process which does not get past a wait


operation is blocked on the semaphore.
This avoids busy waits.
Semaphore
Strong Semaphore
– A semaphore whose definition includes the
policy of removal of a process from the
queue.
Weak Semaphore
– A semaphore that does not specify the order
in which processes are removed from the
queue.
Example of Semaphore Mechanism
• Strong semaphore operation
• Processes A, B and C depend on a result
from process D
Processor
1
A

s=1 C D B
Suspended List Semaphore Ready List

Processor
2
B

s=0 A C D
Suspended List Semaphore Ready List

Processor
3
D

B s=-1 A C
Suspended List Semaphore Ready List
4 Processor
D

s=0 B A C
Suspended List Semaphore Ready List
Processor
5
C

s=0 D B A
Suspended List Semaphore Ready List

Processor
6
D

B A C s= -3
Suspended List Semaphore Ready List
7 Processor
D

B A s=-2 C
Suspended List Semaphore Ready List
Example of Semaphore Mechanism
1. A is running; B, C and D are ready; semaphore count is 1, indicating that one of
D’s result is available. When A executes a wait instruction, it immediately passes
the semaphore and continue to execute; subsequently it rejoins the ready queue.

2. B runs, issues a wait instruction and is suspended.

3. B moves to suspended list, allowing D to run.

4. D completes a new result, issues a signal instruction, allowing B to move to the


ready queue.

5. D rejoins the ready queue and C begins to run.

6. C is suspended when it issues a wait instruction. Similarly A and B are run and
are suspended on the semaphore allowing D to resume execution.

7. When D has a result, it issues a signal, which transfers C to the ready list.

Later cycles of D will release A and B from suspension


Semaphore
Operation wait(S) Operation signal(S)
begin begin
if S>0 if some processes are
then S = S-1; blocked on S;
else block the then activate one blocked
process on S; process;
end else S = S +1;
end
Binary Semaphore
• A semaphore which takes the values 0
and 1 only.
• Binary semaphores are used to implement
mutual exclusion.
n-Process CS Problem
• n processes share a semaphore, mutex
initialized to 1.
• Each process is organized as:
repeat
wait (mutex)
critical section
signal(mutex)
remainder section
until false;
Process Pi about Process Pi completes
to perform wait and Pj executes
wait(mutex) mutex its wait mutex
wait

pi pi
1 0

pj pj
signal
(a) (b)

mutex mutex

pi
pi
1
0
pj
pj

Process Pi performs Process Pj performs (d)


signal op on mutex (c) signal op on mutex
• Properties of Critical Section
– Progress
– Bounded wait does not hold good
– Mutual Exclusion guaranteed
– Dead locks
Control Synchronization
• Processes need to coordinate their
activities with respect to each other such
that a process performs a certain action
only after some other processes have
performed some specific actions
Control Synchronization
Process Pi Process Pj
Wait till Pj performs Perform action aj
action aj

Perform action ak
Signalling using Semaphores

…….. …….
Perform action ai Perform action aj
Wait(sync) Signal(sync)
Perform action ak …………..
………..

Process Pi Process Pj
Signalling using Semaphores
• Before performing action ak, process Pi
should be made to wait till it receives a
signal from Pj, indicating that it has
performed action aj.

• Pj should activate Pi after it performs aj.

• Pi performs wait(sync) before executing


action ak and pj performs a signal(sync)
after executing action aj.
Signalling using Semaphores
• Semaphore sync is initialized to 0 hence if
process Pi finishes action ai before Pj
performs aj, it gets blocked on wait(sync)
till Pj performs signal(sync)

• If Pj performs action aj before Pi performs


wait(sync), then its signal op will increase
the value of sync to 1. Pi will get past the
wait(sync) op.
Classical Problems fo
Synchronization

You might also like