Process Synchro
Process Synchro
Unit-3 (part-2)
Process Synchronization
Processes are categorized as one of the following two types:
while (true)
Counter--
{
while (counter == 0)
Implementation
; /* do nothing */
nextConsumed = buffer [out] ,- register2 = counter
out = (out + 1) % BUFFER_SIZE; register2 = register2 - 1
counter--; counter = register2
}
Bounded buffer
Race condition
Counter=5
Several processes access and manipulates the same data concurrently and outcome of the
execution depends on the particular order in which the access takes place is called RACE
CONDITION
Peterson's solution is restricted to two processes and requires two data items.
Mutual exclusion
Implementation
compare_and_swap Instruction
Definition:
int compare _and_swap(int *value, int expected, int
new_value) {
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
1.Execute atomically
2.It compares the contents of the memory location
with a given value and only if they are the same,
modifies the contents of that memory location to a
new given value.
Mutual exclusion implementation with
Compare_and _Swap()instruction
Shared integer “lock” initialized to 0;
Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0); (in a loop)
/* do nothing */
/* critical section */
lock = 0;
/* remainder section */
} while (true);
Problem with TestAndSet and Compare_and
_Swap instructions
Both the instruction satisfy the Mutual exclusion
But do not satisfy Bounded buffer requirements
Example: P1, P2, P3 are ready to enter their Critical
section.
1) P1 Acquires lock and in its critical section
2) P2 and P3 are waiting
3) P2 may get lock and it can enter its critical
section
4) Again, P1 may get lock and enter its critical
section, Again p2 may get lock
5) P3 will not get a lock to enter its critical section :
Bounded buffer requirements not satisfied
Bounded-waiting mutual exclusion with
Common data structure are TestAndSet ().
boolean waiting[n];
boolean lock;
Mutex (mutual exclusion) locks
Hardware-based solutions are complicated and generally
inaccessible to application programmers
OS designers build software tools to solve critical section
problem
Simplest tool is mutex lock
Protect a critical section by first acquire() a lock then
release() the lock
Boolean variable indicating if lock is available or not
Calls to acquire() and release() must be atomic
Usually implemented via hardware atomic instructions
Mutex (mutual exclusion) locks
A process must acquire a lock before entering a critical
section; it releases the lock when it exits the critical section.
Solution to the
critical section
problem using
mutex locks
Acquire() and Release()
acquire() {
while (!available) ; /* busy wait */
available = false;
} Available=1, lock
is available
Available =0,
release() {
lock is not
available = true;
available
}
do {
acquire lock
critical section
release lock
remainder section
} while (true);
Disadvantage is busy waiting (must loop continuously)
This type of mutex lock is called a spinlock (process spins
while waiting for the lock to become available
(TestAndSet(), Compare_ and_ Swap()..)
Semaphores: Synchronization tool
A semaphore S is an integer variable that can be accessed only through two
standard atomic operations:
All the modifications to the integer value of the semaphore in the wait() and
signal() operations must be executed individually
Each process that wishes to use a resource performs a wait() operation on the
semaphore (thereby decrementing the count).
When the count for the semaphore goes to 0, all resources are being used. After that,
processes that wish to use a resource will block until the count becomes greater than 0.
Semaphores: Synchronization tool
2. Binary Semaphores (mutex locks)
We can use binary semaphores to deal with the critical-section problem for
multiple processes.
While a process is in its critical section, any other process that tries to enter its
critical section must loop continuously in the entry code.
Busy waiting wastes CPU cycles that some other process might be able to use
productively.
This type of semaphore is also called a spinlock because the process "spins" while
waiting for the lock.
Semaphores: Synchronization tool
To overcome the need for busy waiting, we can modify the definition
of the wait() and signal () semaphore operations
When a process executes the wait() operation and finds that the
semaphore value is not positive, it must wait.
We must guarantee that no two processes can execute wait() and signal()
operations on the same semaphore at the same time.
The mutex semaphore provides mutual exclusion for accesses to the buffer
pool and is initialized to the value 1.
The empty and full semaphores count the number of empty and full buffers.
semaphore chopstick[5];