Process Synchronization Reviewer 065012
Process Synchronization Reviewer 065012
Cooperating Process:
o Processes that can affect or be affected by other processes executing in the system.
o Cooperating process can either:
1) Directly share a logical address space (both code and data)
2) Be allowed to share data only through files or messages
o Concurrent access to shared data may result in data inconsistency, that’s why we need various
mechanisms to ensure the orderly execution of cooperating processes that share a logical
address space.
Race Condition
o A situation where several processes access and manipulate the same data concurrently and the
outcome of the execution depends on the particular order in which the access takes place.
o To avoid such conditions that most likely will lead to inaccurate answers, we need to ensure
that only one process at a time can change or modify the data.
o Thus, we require processes to be synchronized in some way.
Critical Section
o A segment of code in which the process may be changing common variables, updating a table,
writing a file, etc.
o When one process is executing in its critical section, no other process is allowed to execute in
its critical section.
o There should be no two processes that are executing in their critical sections at the same time.
Mutex Locks
o Mutex locks protect critical regions and prevent race conditions.
o A process must acquire the lock before entering a critical section and release the lock when it
exits the critical section.
o One of the disadvantages of mutex locks is that it requires busy waiting.
o Busy waiting wastes CPU cycles that some other processes might be able to use
productively, which makes mutex locks a problem in multiprogramming systems.
o Since while a process is still in its critical section, any other process that tries to enter
its critical section must loop continuously in the call to acquire().
o In fact, mutex lock is also called spinlock because the process “spins” while waiting
for the lock to become available.
Hardware Based Solution to Critical-Section Problem
Test and Set Lock
o There is a shared lock variable which can take either of the two values, 0 or 1.
o Before entering into the critical section, a process inquires about the lock.
➢ It is locked, it keeps on waiting until it becomes free.
➢ If it is not locked, it takes the lock and executes the critical section.
Semaphores
o Semaphores was proposed by Edsger Dijkstra, it is a technique to manage concurrent processes
by using a single integer value, which is known as semaphore.
o Semaphore is simply a variable which is non-negative and shared between threads. This
variable is used to solve the critical section problem and to achieve process synchronization in
the multiprocessing environment.
o A semaphore S is an integer variable that, apart from initialization is accessed only through 2
standard atomic operations: wait() and signal().
o All the modifications to the integer value of the semaphore in the wait() and signal() operations
must be executed indivisibly.
o That is, when one process modifies the semaphore value, no other process can simultaneously
modify that same semaphore value.
Types of Semaphores
1) Binary Semaphore
o The value of a binary semaphore can range only between 0 and 1.
o On some systems, binary semaphores are known as mutex locks, as they are locks that
provides mutual exclusion.
2) Counting Semaphores
o Its value can range over an unrestricted domain.
o It is used to control access to a resource that has multiple instances