4.process Synchronization
4.process Synchronization
1) Mutual Exclusion:
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.
3) Bounded Waiting:
After a process makes a request to enter its critical section, there is a bound on
the number of times that other processess are allowed to enter their critical
sections, before the request is granted.
Semaphore :
To solve the critical section problem we can use 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 to access 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 following :
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
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 are 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.
With each semaphore there is an associated waiting queue. Each entry in a waiting
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.
Classical Problem in Synchronization :
Bounded-Buffer Problem
Dining-Philosophers Problem
1. Bounded-Buffer Problem :
It is also known as Producer-Consumer Problem wherein access is controlled to a
shared group of buffers of a limited size.
In this solution, the two counting semaphores "full" and "empty" keep track of
the current number of full and empty buffers respectively (and initialized to 0
and N respectively.) The binary semaphore mutex controls access to the critical
section. The producer and consumer processes are nearly identical - One can
think of the producer as producing full buffers, and the consumer producing
empty buffers.
2. Reader Writer Problem :
In the readers-writers problem there are some processes (termed readers) who only
read the shared data, and never change it, and there are other processes (termed
writers) who may change the data in addition to or instead of reading it. There is no
limit to how many readers can access the data simultaneously, but when a writer
accesses the data, it needs exclusive access.
There are several variations to the readers-writers problem, most centered around
relative priorities of readers versus writers.
The first readers-writers problem gives priority to readers. In this problem, if a
reader wants access to the data, and there is not already a writer accessing it,
then access is granted to the reader. A solution to this problem can lead to
starvation of the writers, as there could always be more readers coming along to
access the data. ( A steady stream of readers will jump ahead of waiting writers
as long as there is currently already another reader accessing the data, because
the writer is forced to wait until the data is idle, which may never happen if there
are enough readers. )
The second readers-writers problem gives priority to the writers. In this problem,
when a writer wants access to the data it jumps to the head of the queue - All
waiting readers are blocked, and the writer gets access to the data as soon as it
becomes available. In this solution the readers may be starved by a steady
stream of writers.
The following code is an example of the first readers-writers problem, and involves an
important counter and two binary semaphores:
readcount is used by the reader processes, to count the number of readers currently
accessing the data.
mutex is a semaphore used only by the readers for controlled access to readcount.
rw_mutex is a semaphore used to block and release the writers. The first reader to
access the data will set this lock and the last reader to exit will release it; The remaining
readers do not touch rw_mutex.
3. The Dinning Philosophers Problem :
The dining philosophers problem is a classic synchronization problem involving the
allocation of limited resources among a group of processes in a deadlock-free and starvation-free
manner:
Consider five philosophers sitting around a table, in which there are five chopsticks
evenly distributed and an endless bowl of rice in the center, as shown in the diagram
below. ( There is exactly one chopstick between each pair of dining philosophers. )
These philosophers spend their lives alternating between two activities: eating and
thinking.
When it is time for a philosopher to eat, it must first acquire two chopsticks - one from
their left and one from their right.
When a philosopher thinks, it puts down both chopsticks in their original locations.
One possible solution, as shown in the following code section, is to use a set of five
semaphores ( chopsticks[ 5 ] ), and to have each hungry philosopher first wait on their
left chopstick ( chopsticks[ i ] ), and then wait on their right chopstick ( chopsticks[ ( i + 1
)%5])
But suppose that all five philosophers get hungry at the same time, and each starts by
picking up their left chopstick. They then look for their right chopstick, but because it is
unavailable, they wait for it, forever, and eventually all the philosophers starve due to the
resulting deadlock.
Inter process Communication and Synchronization :
There are several reasons for providing an environment that allows process cooperation :
Information sharing: Several users may which to share the same information e.g.
a shared file. The O/S needs to provide a way of allowing concurrent access.
Modularity: The most efficient architecture may be to break a system down into
cooperating modules. (E.g. databases with a client-server architecture)
Critical Region:
A critical region is a section of code that is always executed under mutual exclusion.
Critical regions shift the responsibility for enforcing mutual exclusion from the
programmer (where it resides when semaphores are used) to the compiler.
They consist of two parts:
1. Variables that must be accessed under mutual exclusion.
2. A new language statement that identifies a critical region in which the variables
are accessed.