What is Critacal Section Problem and Semaphore in OS.?
The Critical-Section Problem :
Critical Section:
Each process has a code segment, called a critical section, in which the shared data is
accessed.
Critical Section Problem: We need to ensure that when one process is executing in its
critical section, no other process is allowed to execute in its critical section. It may cause
inconsistency outcomes due to race conditions.
The general structure of process Pi (other processes Pj)
do {
entry section
critical section
exit section
reminder section
} while (1);

Competing Processes may share some common variables to synchronize their actions.
Race condition: The situation where several processes access – and manipulate shared
data concurrently. The final value of the shared data depends upon which process
finishes last.
To prevent race conditions, concurrent processes must be synchronized.
A solution to the critical section problem must satisfy the following three requirements:
1) Mutual Exclusion:
If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
2) Progress:
If no process is executing in its critical section and there exist some processes
that wish to enter their critical section, then the selection of the processes that
will enter the critical section next cannot be postponed indefinitely.
3) Bounded Waiting:
Abound must exist on the number of times that other processes are allowed to
enter their critical sections after a process has made a request to enter its critical
section and before that request is granted.
Semaphore :
To solve the critical section problem we can use a synchronization tool is called
semaphore.
A semaphore is an integer variable whose value can be accessed and altered only
by the operations wait (can be represented as P) and signal (can be represented
as V).
wait() is called when a process wants access to a resource. signal() is called when a
process is done using a resource.
When one process modifies the semaphore value, no other process can
simultaneously modify the same semaphore value.
The classical definition of wait and signal are the following :
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
There are 2 types of semaphores:
1) Binary semaphores :
- Binary semaphores have 2 methods associated with it. (up, down / lock, unlock)
- Binary semaphores can take only 2 values (0/1). They are used to acquire locks.
When a resource is available, the process in charge set the semaphore to 1 else 0.
- Also known as mutex locks
2) Counting semaphores :
- Counting Semaphore may have value to be greater than one, typically used to
allocate resources from a pool of identical resources
Semaphore Implementation:
Must guarantee that no two processes can execute wait () and signal () on the same
Semaphore at the same time.
Thus, implementation becomes the critical section problem where the wait and signal
code is placed in the critical section.
Could now have busy waiting in critical section implementation
Note that applications may spend lots of time in critical sections and therefore this is
not a good solution.
Semaphore Implementation with no busy waiting:
With each semaphore, there is an associated waiting queue. Each entry in a waiting
the queue has two data items:
- value (of type integer)
- pointer to next record in the list
„
wait(S): S: = S − 1;
if S < 0
then block(S)
signal(S): S := S + 1;
if S>= 0
then wakeup(S)
Two operations:
block – place the process invoking the operation on the appropriate waiting queue.
wakeup – remove one of processes in the waiting queue and place it in the ready
queue.
Thanks for regarding the blog if any question ask in the comment section.


Comments
Post a Comment