SPOS_Unit 5
SPOS_Unit 5
Concurrency Control
By: Mohammed Asad
Concurrency: Introduction
• 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: Introduction
• Non-atomic:
• Operations that are non-atomic but interruptible by multiple processes can
cause problems.
• Race conditions:
•A race condition occurs of the outcome depends on which of several
processes gets to a point first.
Issues of Concurrency
• Blocking:
• Processescan block waiting for resources. A process could be blocked for
long period of time waiting for input from a terminal.
• If
the process is required to periodically update some data, this would be very
undesirable.
• Deadlock: It occurs when two processes are blocked and hence neither can
proceed to execute.
Synchronization
• On the basis of synchronization, processes are categorized as one of the
following two types:
• Critical Section:
• The regions of a program that try to access shared resources and may cause
race conditions are called critical section.
• Toavoid race condition among the processes, we need to assure that only one
process at a time can execute within the critical section.
Critical Section Problem
• Critical Section is the part of a program which tries to access shared resources.
• That resource may be any resource in a computer like a memory location, Data
structure, CPU or any IO device.
• The critical section cannot be executed by more than one process at the same
time; operating system faces the difficulties in allowing and disallowing the
processes from entering the critical section.
• The critical section problem is used to design a set of protocols which can
ensure that the Race condition among the processes will never arise.
Critical Section Problem
• Critical section is a code segment that can be accessed by only one process at
a time.
• Critical
section contains shared variables which need to be synchronized to
maintain consistency of data variables.
Critical Section Problem
• In the entry section, the process requests for entry in the Critical Section.
• Any solution to the critical section problem must satisfy three requirements:
1. Mutual exclusion
2. Progress
3. Bounded waiting
Requirements of Synchronization mechanisms
Primary
• Mutual Exclusion
• Our solution must provide mutual exclusion. By Mutual Exclusion, we mean
that if one process is executing inside critical section then the other process
must not enter in the critical section.
• Progress
• Progress means that if one process doesn't need to execute into critical section
then it should not stop other processes to get into the critical section.
Mutual Exclusion (Mutex)
Requirements of Synchronization mechanisms
Secondary
• Bounded Waiting
• We should be able to predict the waiting time for every process to get into the
critical section.
• The process must not be endlessly waiting for getting into the critical section.
• Architectural Neutrality
• Our mechanism must be architectural natural. It means that if our solution is
working fine on one architecture then it should also run on the other ones as
well.
Interprocess Communication (IPC)
• Incomputer science, Interprocess Communication (IPC) allows
communicating processes to exchange the data and information.
2. Signal():
• The signal operation increments the semaphore value. If the value is greater
than or equal to zero, then the process blocked by wait() operation is removed
from the waiting queue and sent to ready queue.
Monitor
•A monitor is a synchronization construct that allows threads to have both
mutual exclusion and the ability to wait (block) for a certain condition to
become true.
• Monitors also have a mechanism for signaling other threads that their
condition has been met.
•A monitor consists of a mutex (lock) object and condition variables. A
condition variable is basically a container of threads that are waiting for a
certain condition.
• Monitors provide a mechanism for threads to temporarily give up exclusive
access in order to wait for some condition to be met, before regaining
exclusive access and resuming their task.
Classical Problems of Synchronization
Producer Consumer Problem
• Incomputing, the producer–consumer problem (also known as the
bounded-buffer problem) is a classic example of a multiprocess
synchronization problem.
• The problem describes two processes, the producer and the consumer, who
share a common, fixed-size buffer used as a queue.
• The producer's job is to generate data, put it into the buffer, and start again.
• Atthe same time, the consumer is consuming the data (i.e., removing it from
the buffer), one piece at a time.
Producer Consumer Problem
• The problem is to make sure that the producer won't try to add data into the
buffer if it's full and that the consumer won't try to remove data from an empty
buffer.
• The solution for the producer is to either go to sleep or discard data if the buffer
is full.
• Thenext time the consumer removes an item from the buffer, it notifies the
producer, who starts to fill the buffer again.
• In the same way, the consumer can go to sleep if it finds the buffer to be empty.
Producer Consumer Problem
• Thenext time the producer puts data into the buffer, it wakes up the sleeping
consumer.
• A philosopher may eat if he can pickup the two chopsticks adjacent to him.
• One chopstick may be picked up by any one of its adjacent followers but not
both.
Deadlock
•A deadlock is a situation in which two computer programs sharing the same
resource are effectively preventing each other from accessing the resource,
resulting in both programs ceasing to function.
Deadlock Condition
• Mutual Exclusion: One or more than one resource
are non-sharable (Only one process can use at a time).
3. Ignore the problem all together: If deadlock is very rare, then let it
happen and reboot the system. This is the approach that both windows and
Unix take.
Deadlock Prevention
• We can prevent Deadlock by eliminating any of the above four condition.
• Eliminate Mutual Exclusion:
• It
is not possible to dis-satisfy the mutual exclusion because some resources,
such as the tap drive and printer, are inherently non-shareable.
• Processwill make new request for resources after releasing the current set of
resources.
• Consider there are n account holders in a bank and the sum of the money in
all of their accounts is S.
• Everytime a loan has to be granted by the bank, it subtracts the loan amount
from the total money the bank has.
Deadlock Avoidance
• Then it checks if that difference is greater than S.
• It
is done because, only then, the bank would have enough money even if all
the n account holders draw all their money at once.
• Afterkilling each process check for deadlock again and keep repeating the
process till the system recovers from deadlock.
• Killingall the processes one by one helps a system to break circular wait
conditions.
Deadlock Recovery
• Resource Preemption:
• Resources are preempted from the processes involved in the deadlock, and
preempted resources are allocated to other processes so that there is a
possibility of recovering the system from the deadlock.
• In this case, the system goes into starvation.
• Concurrency Control:
• Concurrency control mechanisms are used to prevent data inconsistencies in
systems with multiple concurrent processes.
• These mechanisms ensure that concurrent processes do not access the same
data at the same time, which can lead to inconsistencies and errors.
Deadlock Recovery
• Deadlockscan occur in concurrent systems when two or more processes are
blocked, waiting for each other to release the resources they need.
• This can result in a system-wide stall, where no process can make progress.