0% found this document useful (0 votes)
31 views56 pages

2 - OS Process Synchronization

Uploaded by

manjeshsingh0245
Copyright
© © All Rights Reserved
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)
31 views56 pages

2 - OS Process Synchronization

Uploaded by

manjeshsingh0245
Copyright
© © All Rights Reserved
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/ 56

Chapter : Process Synchronization

Synchronization Hardware
Hardware Solution to C.S.
 Many systems provide hardware support for critical section code

 Uni-processors – could disable interrupts


 Currently running code would execute without preemption

 Modern machines provide special atomic hardware instructions


 Atomic = non-interruptable
 Either test memory word and set value
 Or swap contents of two memory words
Hardware Solution to C.S.
1. Interrupt Disabling

1. Process leaves control of CPU when it is interrupted.


2. Solution is:
1. To have each process disable all interrupts just after
entering to the critical section.
2. Re-enable interrupts after leaving critical section
Hardware Solution to C.S.
 Interrupt Disabling

Repeat

Disable interrupts

C.S

Enable interrupts

Remainder section
Hardware Solution to C.S.
2. Hardware instructions
1. Machines provide instructions that can read, modify and store
memory word
2. Common inst. are:
1. Test and Set
This inst. Provides action of testing a variable and set its
value

It executes atomically
Test and Set instructions operates on single Boolean
variable.
Hardware Solution to C.S.
//initially lock is
FALSE
Swap Instruction

 Definition:

void Swap (boolean *a, boolean *b)


{
boolean temp = *a;
*a = *b;
*b = temp:
}
Hardware Solution to C.S. Using
Swap
Shared data : ‘lock’ initialized to false. Each process has local Boolean
variable ‘key’.

While (true){
Key=TRUE;
While(key==TRUE)
Swap(&lock, &key);

// critical section
lock=FALSE;
//remainder section
}
Semaphore
 Synchronization tool that maintains concurrency using variables
 Semaphore S is a integer variable which can take positive values
including 0. It is accessed from 2 operations only.
Operations On Semaphore:
 wait() and signal()

1. Wait Operation is also known as P() which means to test


2. Signal Operation is also known as V() which means to increment

 Entry to C.S. is controlled by wait()


 Exit from C.S. is signaled by signal()
Semaphore
wait (S) {
while (S <= 0)  Can only be accessed via two
indivisible (atomic) operations and
do skip ; // no- S=1
op}  For critical Section problem
semaphore value is always 1
S- -;  P(S) and V(S):
// C.S wait (S) {
while (S <= 0)
signal (S) { do skip ; // no-op}
S++; } S- -;
// C.S

signal (S) {
S++; }
Semaphore as General Synchronization Tool

Types of Semaphores:
 Counting semaphore – when integer value can be
any non-negative value
 Binary semaphore – integer value can range only
between 0 and 1
 Also known as mutex locks
Semaphore and Busy Waiting
Disadvantage of Semaphore: Busy Waiting

 When a process is in C.S. and any other process that wants to


enter C.S. loops continuously in entry section.

 Wastes CPU cycles

 Semaphore that implements busy waiting is known as: Spin


Lock
Semaphore Implementation with no Busy
waiting
 With each semaphore there is an associated waiting queue.
Each entry in a waiting queue has two data items:

 value (of type integer)


 pointer to next record in the list
Semaphore Implementation with no Busy
waiting
Instead of waiting, a process blocks itself.

 Two operations:
 block – place the process invoking the operation on the
waiting queue.
 wakeup – remove one of processes in the waiting queue and
place it in the ready queue.
Semaphore Implementation with no Busy
waiting
 Implementation of wait:
S=3
wait (S){
value--;
if (value <= 0) {
add process P to waiting
queue
block();
}
// C.S
}
Semaphore Implementation with no Busy
waiting
 Implementation of signal:

Signal (S){
value++; //“value” has -ve value
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
Deadlock and Starvation

 Deadlock – two or more processes are waiting


indefinitely for an event that can be caused by only
one of the waiting processes

 Starvation – indefinite blocking. A process may


never be removed from the semaphore queue in
which it is suspended.
Problems with Semaphores

 Incorrect use of semaphore operations:

 signal (mutex) …. wait (mutex)

 wait (mutex) … wait (mutex)

 Omitting of wait (mutex) or signal (mutex)


(or both)
Monitors
 A way to encapsulate the Critical Section by making class around the critical
section and allowing only one process to be active in that class at one time.
 Only one process may be active within the monitor at a time
 Name of Monitor
 Initialization Code Section
 Procedure to request the Critical Data
 Procedure to release the Critical Data

monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }

procedure Pn (…) {……}
Initialization code ( ….) { … }

}
}
Monitors
Schematic view of a Monitor

Only one process at a time can be in monitor


Condition Variables
Condition Variables
 condition x, y;
 Two operations on a condition variable:
 x.wait () – a process that invokes the operation is suspended until
x.signal()

 x.signal () – resumes one of processes (if any) that invoked x.wait


()
There could be different conditions for which a process could be
waiting
Let a process made the request to C.S. but no critical data
is available, So we would not go back to check all the
conditions again to run the request, As we have
checked some conditions so processes will be lined up
for excess to C.S.
Monitor with Condition Variables
Classical Problems of Synchronization
 Bounded-Buffer Problem
 Readers and Writers Problem
 Dining-Philosophers Problem
Bounded-Buffer Problem

 N buffers, each can hold one item


 Semaphore mutex initialized to the value 1
 Semaphore full initialized to the value 0
 Semaphore empty initialized to the value N.

 mutex= 1
 full= 0
 empty= N
Bounded Buffer Problem (Cont.)
 The structure of the producer process
while (true) {

// produce an item

wait (empty);
wait (mutex);

// add the item to the buffer

signal (mutex);
signal (full);
}
Bounded Buffer Problem (Cont.)

 The structure of the consumer process

while (true) {
wait (full);
wait (mutex); // mutex=1

// remove an item from buffer

signal (mutex); // mutex=0


signal (empty);

// consume the removed item


}
Dining-Philosophers Problem

 Shared data
 Bowl of rice (data set)
 Semaphore chopstick [5] initialized to 1
Dining-Philosophers Problem (Cont.)
 The structure of Philosopher i:

While (true) {
wait ( chopstick[i] );
wait ( chopstick[ (i + 1) % 5] ); // =1

// eat

signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );

// think

}
MCQ Questions
Which process can be affected by other
processes executing in the system?
a) cooperating process
b) child process
c) parent process
d) init process
MCQ Questions
1a
MCQ Questions
When several processes access the same
data concurrently and the outcome of
the execution depends on the particular
order in which the access takes place,
is called
a) dynamic condition
b) race condition
c) essential condition
d) critical condition
MCQ Questions
2b
MCQ Questions
If a process is executing in its critical
section, then no other processes can be
executing in their critical section. This
condition is called
a) mutual exclusion
b) critical exclusion
c) synchronous exclusion
d) asynchronous exclusion
MCQ Questions
3a
MCQ Questions
Which one of the following is a
synchronization tool?
a) thread
b) pipe
c) semaphore
d) socket
MCQ Questions
4c
MCQ Questions
A semaphore is a shared integer variable
a) that can not drop below zero
b) that can not be more than zero
c) that can not drop below one
d) that can not be more than one
MCQ Questions
5a
MCQ Questions
Mutual exclusion can be provided by the
a) mutex locks
b) binary semaphores
c) both mutex locks and binary
semaphores
d) none of the mentioned
MCQ Questions
6c

Explanation: Binary Semaphores are


known as mutex locks.
MCQ Questions
When high priority task is indirectly
preempted by medium priority task
effectively inverting the relative priority
of the two tasks, the scenario is called
a) priority inversion
b) priority removal
c) priority exchange
d) priority modification
MCQ Questions
7a
MCQ Questions
8. Process synchronization can be done
on
a) hardware level
b) software level
c) both hardware and software level
d) none of the mentioned
MCQ Questions
8c
MCQ Questions
9. A monitor is a module that encapsulates
a) shared data structures
b) procedures that operate on shared data
structure
c) synchronization between concurrent
procedure invocation
d) all of the mentioned
MCQ Questions
9d
MCQ Questions
To enable a process to wait within the
monitor,
a) a condition variable must be declared
as condition
b) condition variables must be used as
boolean objects
c) semaphore must be used
d) all of the mentioned
MCQ Questions
10 a
MCQ Questions
semaphore operations are atomic because they are implemented
within the OS.............
a. user mode
b. kernal mode
c. multi-programming
d. multitasking
MCQ Questions
b
MCQ Questions
The counting semaphore was initialized to 10. then 6 P(wait) and
4V(signal) were performed. the resulting value of semaphore
is:
a. 0
b. 8
c. 10
d. 12
MCQ Questions
Consider a non-negative counting semaphore S. The operation
P(S) decrements S, V(S) incements S. During an execution,
20 P(S) operations and 12 V(S) operations are issued in some
order. The largest initial value of S for which at least one P(S)
opeation will remain blocked is.......
MCQ Questions
Sol:

for atleast one blocked process last value of S must be 0


S-19+12=0
S=7
End of Chapter

You might also like