Process Syncronization For Students 1684053733253
Process Syncronization For Students 1684053733253
Process Synchronization
When multiple processes execute concurrently sharing system resources, then
inconsistent results might be produced.
Process Synchronization is a mechanism that deals with the synchronization of
processes.
It controls the execution of processes running concurrently to ensure that
consistent results are produced.
Synchronization is required
When multiple processes execute concurrently sharing some system resources.
To avoid the inconsistent results.
Critical Section
Critical section is a section of the program where a process access the shared
resources during its execution.
Solution to
Critical Section
Problem
1.Mutual 3. Bounded
2. Progress
Exclusion Waiting
Mutual Exclusion: No more than one process can execute in its
critical section at one time.
• Now, Process A finishes and signals semaphore which in turns changes semaphore
value to 1
• Thus, now process B can enter Critical section
• In this way mutual exclusion was achieved.
Counting Semaphore
• For Counting Semaphore we initialise the value of semaphore as the number of concurrent access
of critical sections we want to allow.
• For example Let us assume that the value is 3.
• Process 1 enters Critical section and semaphore value is changed to 2
• Process 2 also enters critical section and semaphore value is changed to 1
• Process 2 signals semaphore and comes out of critical section and Semaphore value is 2
• Note at this moment only 1 process that is process 1 is in critical section
• Process 3 and 4 also enter critical section simultaneously and semaphore value is 0
• At this moment there are three processes in Critical section which are process Process 1, 3, 4
• Now assume that process 5 wants to enter the CS. It would not be able to enter as semaphore
value is 0
• It can only enter once any of the process 1, 3, 4 signals out of the critical section.
• Thus value of semaphore in counting semaphore is equal to number of simultaneous processes
allowed to access the critical section.
Semaphore Method: wait
void wait(sem S)
{
S.count--;
if (S.count < 0) {
add the caller to the waiting list;
block();
}
}
After decreasing the counter by 1, if the counter value becomes
negative, then
add the caller to the waiting list, and then
block itself.
Semaphore Method: signal
void signal(sem S)
{
S.count;
if (S.count < 0) {
remove a process P from the waiting list;
resume(P);
}
}
After increasing the counter by 1, if the new counter value is not
positive, then
remove a process P from the waiting list,
resume the execution of process P, and return .
Semaphores
• Creating and Accessing Semaphore Sets
– Semaphores are allocated in sets.
– They are allocated with a value of 0 (blocked)
• Semaphore Operations
– wait (P)
– signal (V)
• Semaphore Control
– get info on a set
– set all semaphore values in a set or get all values
– set one value in a set or get one value
– remove the set
The Producer - Consumer Problem(also
known as the bounded-buffer problem)
Following is the code for Producer(): Following is the code for Consumer():
void producer() void consumer()
{ {
while(True) while(True)
{ produce() { wait(F)
wait(E) wait(S)
wait(S) take()
append() signal(S)
signal(S) signal(E)
signal(F) } use() }
} }
The Dining Philosopher Problem
The Dining Philosopher Problem states that K
philosophers seated around a circular table with
one chopstick between each pair of philosophers.
There is one chopstick between each philosopher.
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.
1. A critical section is a program segment
(a) which should run in a certain specified amount of time
(b) which avoids deadlocks
(c) where shared resources are accessed
(d) which must be enclosed by a pair of semaphore operations, P and V
1. A critical section is a program segment
(a) which should run in a certain specified amount of time
(b) which avoids deadlocks
(c) where shared resources are accessed
(d) which must be enclosed by a pair of semaphore operations, P and V
(
A solution to the Dining Philosophers Problem which avoids deadlock is
(a) ensure that all philosophers pick up the left fork before the right fork
(b) ensure that all philosophers pick up the right fork before the left fork
(c) ensure that one particular philosopher picks up the left fork before the right
fork, and that all other philosophers pick up the right fork before the left fork
(d) None of the above
A solution to the Dining Philosophers Problem which avoids deadlock is
(a) ensure that all philosophers pick up the left fork before the right fork
(b) ensure that all philosophers pick up the right fork before the left fork
(c) ensure that one particular philosopher picks up the left fork before the right
fork, and that all other philosophers pick up the right fork before the left fork
(d) None of the above
(
3. Consider the methods used by processes P1 and P2 for accessing their critical sections whenever needed, as
given below. The initial values of shared boolean variables S1 and S2 are randomly assigned.
P1 P2
while (S1 = = S2); while (S1 = = S2);
critical section critical section
S1 = S2; S1 = S2;