Unit 2
Unit 2
Principle of Concurrency
• Concurrency is the execution of multiple instruction sequences at the
same time. It happens in the operating system when there are several
process threads running in parallel. The running process threads
always communicate with each other through shared memory or
message passing. Concurrency results in the sharing of resources
resulting in problems like deadlocks and resource starvation.
• It helps in techniques like coordinating the execution of processes,
memory allocation, and execution scheduling for maximizing
throughput.
Independent Process: Independent Processes are those processes whose task is not
dependent on any other processes.
1. Mutual Exclusion: If process Pi is executing in its critical section then no any other
2. Progress: If no process is executing in its critical section and some process wish to
enter their critical sections then only those process that are not executing in their
remainder section can enter its critical section next.
3. Bounded waiting: There exists a bound on the number of times that other processes
are allowed to enter their critical sections after a process has made a request.
Dekker’s algorithm in Process
Synchronization
turn := j; turn := i;
flag[i] := false; flag[j] := false;
remainder section remainder section
until false; until false;
One advantage of this algorithm is that it doesn't require special test-and-set (atomic read/modify/write)
instructions and is therefore highly portable between languages and machine architectures.
One disadvantage is that it is limited to two processes and makes use of busy waiting instead of
process suspension.
If two processes attempt to enter a critical section at the same time, the algorithm will allow only one
process in, based on whose turn it is. If one process is already in the critical section, the other process
will busy wait for the first process to exit. This is done by the use of two flags, flag[i] and flag[j],
which indicate an intention to enter the critical section on the part of processes 0 and 1, respectively, and
a variable turn that indicates who has priority between the two processes.
Dekker's algorithm requires more complex conditions for
waiting, which can lead to a potential race condition when both
processes attempt to enter the critical section simultaneously.
Peterson's algorithm uses a simpler condition for waiting, which
avoids the race condition and ensures mutual exclusion.
Peterson’s Solution to
Critical Section Problem
Semaphore
• For the solution to the critical section problem one synchronization
tool is used which is known as semaphores. A semaphore ‘S’ is an
integer variable which is accessed through two standard atomic
operations wait and signal.
• Signal(s): S= S+1;
• When a process performs a wait operation on a semaphore, the
operation checks whether the value of the semaphore is >0. If so, it
decrements the value of the semaphore and lets the process continue
its execution; otherwise, it blocks the process on the semaphore.
S1; Wait(sync);
Signal(sync); S2;
Producer Consumer Problem with Semaphore
To solve this problem, we need two counting semaphores – Full and Empty.
• “Full” keeps track of number of items in the buffer at any given time.
• “Empty” keeps track of number of unoccupied slots.
• Initialization of semaphores –
• mutex = 1
• Full = 0 // Initially, all slots are empty. Thus full slots are 0
• Empty = n // All slots are empty initially
Producer Consumer Problem with Semaphore
• When producer produces an item then the value of “empty” is reduced by 1
because one slot will be filled now. The value of mutex is also reduced to prevent
consumer to access the buffer. Now, the producer has placed the item and thus the
value of “full” is increased by 1. The value of mutex is also increased by 1
because the task of producer has been completed and consumer can access the
buffer.
• As the consumer is removing an item from buffer, therefore the value of “full” is
reduced by 1 and the value is mutex is also reduced so that the producer cannot
access the buffer at this moment. Now, the consumer has consumed the item, thus
increasing the value of “empty” by 1. The value of mutex is also increased so that
producer can access the buffer now.
Producer Consumer Problem with Semaphore
Solution for Producer – Solution for Consumer –
do{ do{
//produce an item
wait(empty); wait(full);
wait(mutex); wait(mutex);
//place in buffer // consume item from buffer
signal(mutex); signal(mutex);
signal(full); signal(empty);
}while(true) }while(true)