CRITICAL SECTION
1
ONLY ONE PROCESS ACCESS A SHARED RESOURCE AT A TIME
The process enter the criticial section when it starts to execute the first command and leave the
critical section when it starts to execute the last command. 2
CRITICAL SECTION
• When concurrent processes interact through a shared variable,the
integrity of the variable may be violated if access to the variable is
not co ordinated.
Example of integrity violations
(1) Variable does not record all changes
(2) a process may read inconsistent values
(3) The final value of the variable may be consistent
3
The process must be synchronized such that only one process can access the variable at any
one time.
This is known as mutual exclusion.
A critical section is a code segment in a process in
which a shared resource is accessed.
4
Solution to the problem of mutual exclusion must satisfy
the following requirement
Only one processor can execute its critical section at any one time.
When no process is executing in its critical section,any process that
requests entry to its critical section must be permitted to enter without
delay.
When two or more process compete to enter their respective critical
section,the selection cannot be postponed indefinitely.
No process can prevent any other process from entering its critical
section indefinitely; that is every process should be given a fair chance
to access the shared resource.
5
Here, are four essential elements of the critical section
•Entry Section: It is part of the process which decides the entry of a
particular process.
•Critical Section: This part allows one process to enter and modify the
shared variable.
•Exit Section: checks that a process that finished its execution should
be removed through this Section.
•Remainder Section: All other parts of the Code, which is not in Critical,
Entry, and Exit Section, are known as the Remainder Section.
6
7
Solutions for critical section
1.Early mechanisms for mutual exclusion
a.Busy waiting
-process that cannot enter its critical section continuously tests the
value of a status variable to find if the shared resource is free.
-the status variable checks whether the status of the shared resource.
-The main problem with this approach are the wastage of CPU cycles
and the memory access bandwidth
8
b.Disabling interrupts
-is a mechanism where process disables interrupts before entering the
critical section and enables the interrupts immediately after exiting the
critical section
-a process is not interrupted during the execution of its critical section
and thus excludes all the process from entering the critical section.
9
c.test and set Instruction
• Used to achieve mutual exclusion.
• The instruction completes a single indivisible operation on a
designated/specific memory location.
• When this instruction is executed a specific memory location is
checked for a particular value,if they match the memory locations
contents are altered.
• Can be used as a building block for busy waiting or can be
incorporated into schemes that relinquish the cpu when the
instruction fails
10
2.Semaphore
• It is a high level construct used to synchronize concurrent processes.
• A semaphore S is an integer variable on which processes can perform
two indivisible operations.
• Each semaphore has a queue associated with it,where processes that
are blocked on that semaphore wait
11
The P (wait) and S(signal) operation
is defined as follows
P:wait(s) S:signal(s)
{
while(s<=0) ; { s=s+1;
s=s-1;
} }
12
Let P1,P2,P3,P4..n tries to enter
into critical section
do let p1 enters
{ wait(s) s=1,s=0.
suppose if p2 enters
//critical section
Signal(s)
//remainder section
}
While(T)
13
Semaphore classified into two types
• Binary semaphore-initial value is 1.
• Resource counting semaphore-initial value is normally more than 1.
14
Solution to mutual exclusion using
semaphore
Shared var
mutex:semaphore(=1);
Process i(i=1,n);
begin
……
P(mutex);
execute CS;
V(mutex);
……
end
15
Consider if process P1 and P2 is there Process P1 enters into the critical
section,performs its work in critical section and leaves the critical
section by incrementing the value by one by performing the signal
operation.
Then Process P2 can enter the critical section
16
We can use a binary semaphore to achieve mutual exclusion.
If any process has performed a P (mutex) operation without performing
the corresponding V (mutex) operation(ie the process is still inside the
critical section),then all the other process trying to enter the critical
section will wait on the P(mutex) operation until this process performs
the V(mutex) operation(ie exits the critical section).
Therefore mutual exclusion is achieved.
17
For any semaphore
Number of P operations-Number of V operations <=initial value
Eg:)A counting semaphore S is initialized to 10. Then, 6 P operations and 4 V operations are
performed on S. What is the final value of S?
10-6+4=8
18
Drawbacks of Semaphore
• A process that uses a semaphore has to know which other processes
use the semaphore.It may also have to know those processes are
using the semaphore to ensure co ordination
• Semaphore operations must be carefully installed in a process.The
omission of a P or V operation may result in inconsistencies or
deadlocks.
• Programs using semaphore are extremely hard to verify for
correctness.
19
Other synchronization Problems
(CLASSIC SYNCHRONIZATION
PROBLEMS)
Here also concurrent access to shared resource is essential
The Dining Philosopher’s Problem
The Producer-Consumer Problem
The Reader’s Writer’s Problem
20
The Dining Philosopher’s Problem
2
4
21
The dining philosophers problem states that there are 5 philosophers sharing
a circular table and they eat and think alternatively.
There is a bowl of rice for each of the philosophers and 5 chopsticks.
A philosopher needs both their right and left chopstick to eat.
A hungry philosopher may only eat if there are both chopsticks available.
Otherwise a philosopher puts down their chopstick and begin thinking again.
22
Note : 1.No two neighbouring philosopher’s can eat
simultaneously.
2.The act of picking up a fork should be done in
critical section.
3.Devising a deadlock free solution is not easy.
23
The Producer Consumer Problem
• Here a set of producer process supplies messages to a set of
consumer process.
• These processes share a common buffer pool where messages are
deposited by producers and removed by consumers
• All the processes are asynchronous
• No consumer process can remove when the buffer pool is empty and
no producer can deposit a message when the buffer pool is full.
• Integrity problems may arise if multiple consumers try to remove
messages in the buffer pool simultaneously.so we use critical section
here.
24
The Reader’s Writer’s Problem
• A shared resource is a file that is accessed by both the reader and the
writer.
• Reader process simply read the information in the file without
changing its contents.
• Writer process may change the information in the file.
• The basic synchronization constraint is that any number of readers
should be able to concurrently access the file,but only one writer can
access the file at a given time.
25
There are several versions of the
problem whether readers or writers
are given priority
Reader’s Priority:
• Arriving readers receive priority over waiting writers.
• A waiting or arriving writer gains access to the file only when there
are no readers in the system.
• When the writer is done with the files,all the waiting readers have
priority over the waiting writers.
26
Writer’s Priority:
• Arriving writers receive priority over waiting readers.
• A waiting or arriving reader gains access to the file only when there
are no writers in the system.
• When the reader is done with the files,all the waiting writers have
priority over the waiting readers.
27
• The writers may starve in the reader’s priority case and vice versa.
• To overcome this we use a weak reader’s priority case or a weak
writer’s priority case.
• In a weak reader’s priority case the arriving reader still has priority
over waiting writers.
• However when a writer departs,both waiting readers and waiting
writers have equal priority.
28
A semaphore solution to the
reader’s priority problem
29
• A reader calls a reader procedure and a writer calls a writer procedure
to read and write the files respectively.
• If the reader is reading then in the reader procedure nreaders!=0 and
consequently all arriving readers get to immediately read the file..
• If there are some writers waiting to write,one of them is blocked on
the semaphore ‘wmutex’ and the rest of them are blocked on the
semaphore ‘srmutex’ in the writer procedure.
30
• When the last reader finishes reading the file,it unblocks a writer
waiting on the ‘wmutex’ semaphore.
• If some readers arrive while the writer is writing,the first reader will
be blocked on the semaphore ‘wmutex’ and all the subsequent
readers will be blocked on the semaphore ‘mutex’.
• When the writer departs,its V(mutex) operation will unblock a
waiting reader(if there is one) which subsequently causes all the
waiting readers to unblock,one by one.
31
• The departing writer performs a V(srmutex) operation that unblocks a
writer (if there is one) waiting on P(srmutex)
• Clearly this writer will be blocked at P(wmutex) if nreaders is greater
than 0 this time.
32
Monitors
• Monitors are abstract data types for defining shared
objects (or resources) and for scheduling access to
these objects in multiprogramming environment.
• A monitor consists of procedures, the shared
object(resources) and administrative data.
• Procedures are gateways to the shared resource and
are called by the process needing to access.
33
Structure of a Monitor
34
The execution of the monitor obeys
the following constraints
• Only one process can be active to ensure mutual exclusion.
• Procedures of a monitor can only access data local to the monitor
they cannot access an outside variable.
• The variables or data local to a monitor cannot be directly accessed
from outside the monitor.
35
• Since the main function of a monitor is to control access to a
shared resource it should be able to delay and resume the
execution of the processes calling monitor’s procedures.
• The synchronization processes is accomplished via two special
operations,wait and signal,which are executed within the
monitor’s procedures.
• Executing a wait operation suspends the caller process and the
caller process thus relinquishes control of the monitor.
36
• Executing a signal operation causes exactly one waiting
process to immediately regain control of the
monitor.The signalling process is suspended on an
urgent queue.
• The processes in the urgent queue have a higher
priority for regaining control of the monitor than the
process trying to enter the monitor when a process
relinquishes it.
• When a waiting process is signalled,it starts execution
from the very next statement following the wait
statement.If there are no waiting process,the signal has
no effect. 37
CONDITION VARIABLE
• A conditional variable associated with wait and signal operations
helps to distinguish the processes to be blocked or unblocked for
different reasons.
• It is associated with a queue of processes that are currently waiting
on that condition
• The operation <condition variable> queue returns true if the queue
associated with the condition variable is not empty.
• Otherwise it returns false
38
The syntax of wait and signal operations associated with a condition is:
<condition variable> .wait;
<condition variable>.signal;
39
Advantages
• Flexibility that they allow in scheduling the process in the waiting
queues.
• FIFO discipline is generally used with queues,but priority queues can
be implemented by enhancing the wait operation with a parameter.
• The parameter specifies the priority of the process to be delayed.
• The smaller the value of the parameter,the higher its priority
• When a queue is signalled the process with the highest priority in
that queue is activated.The syntax for the priority wait is:
<condition variable>.wait(<parameter>)
40
Drawback of Monitors
• Absence of concurrency since one process can be active at a time.
• For proper synchronization,procedures of the monitors must be
invoked before and after accessing the shared resource.This
arrangement however results in the improper accessing of resource
without accessing the shared resources.
• Possibility of deadlock in the case of nested monitor calls
41