0% found this document useful (0 votes)
10 views

Process Syncronization For Students 1684053733253

Uploaded by

MADHUSUDAN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Process Syncronization For Students 1684053733253

Uploaded by

MADHUSUDAN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Process Synchronization

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.

Progress: If no one is in the critical section and someone wants


in, then those processes not in their remainder section must be
able to decide in a finite time who should go in.

Bounded Wait: All requesters must eventually be let into the


critical section.
A semaphore is an object that consists of a counter, a waiting list of processes and
two methods (e.g., functions): signal and wait.
Types of Semaphores
Semaphores are mainly of two types:
• Binary semaphors: Only True/False or 0/1
values.
• Counting Semaphores: Non-negative value,
These are used to implement bounded
concurrency.
Binary Semaphore
• Assume there are two processes A and B.
• At the beginning the value of semaphore is initialized as 1.
• Assume process A wants to enter the critical section
– Before it can do that it checks the value of semaphore which is 1 thus, it can enter the CS and

• Now Assume that process B wants to enter too


– It checks the semaphore value which is 0 thus it can’t enter and

• 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)

• Producer pushes items into the buffer.


• Consumer pulls items from the buffer.
• Producer needs to wait when buffer is full.
• Consumer needs to wait when the buffer is empty.
producer–consumer problem (also known as
the bounded-buffer problem)
• It is a multi-process 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 a piece of data, put it into the buffer and start again. At the same
time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time.
• 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. The next
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.
• The next time the producer puts data into the buffer, it wakes up the sleeping consumer.
• The solution can be reached by means of inter-process communication, typically using semaphores.
• An inadequate solution could result in a deadlock where both processes are waiting to be
awakened. The problem can also be generalized to have multiple producers and consumers.
Let us consider 3 semaphore variables:
S = 1 => this is going to control that at one time only one from (producer or consumer) is
going to access a single slot.
E = n => this will keep a track of “empty slots”.
F = 0 => this will keep a track of “how many slots are full”.

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.

Method Used by P1 Method Used by P2


while (S1 == S2) ; while (S1 != S2) ;
Critica1 Section Critica1 Section
S1 = S2; S2 = not (S1);

Which one of the following statements describes the properties achieved?


(a) Mutual exclusion but not progress
(b) Progress but not mutual exclusion
(c) Neither mutual exclusion nor progress
(d) Both mutual exclusion and progress
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.

Method Used by P1 Method Used by P2


while (S1 == S2) ; while (S1 != S2) ;
Critica1 Section Critica1 Section
S1 = S2; S2 = not (S1);

Which one of the following statements describes the properties achieved?


(a) Mutual exclusion but not progress
(b) Progress but not mutual exclusion
(c) Neither mutual exclusion nor progress
(d) Both mutual exclusion and progress
4. A counting semaphore was initialized to 10. Then 6 P (wait) operations and 4V
(signal) operations were completed on this semaphore. The resulting value of the
semaphore is
(a) 0 (b) 8 (c) 10 (d) 12
4. A counting semaphore was initialized to 10. Then 6 P (wait) operations and 4V
(signal) operations were completed on this semaphore. The resulting value of the
semaphore is
(a) 0 (b) 8 (c) 10 (d) 12
5. Let m*0+…m*4+ be mutexes (binary semaphores) and P*0+ …. P*4+ be processes.
Suppose each process P[i] executes the following:
wait (m[i]);wait (m[(i+1) mode 4]);
.........
release (m[i]); release (m[(i+1)mod 4]);
This could cause
(a) Thrashing (b) Deadlock
(c) Starvation, but not deadlock (d) None of the above
5. Let m*0+…m*4+ be mutexes (binary semaphores) and P*0+ …. P*4+ be processes.
Suppose each process P[i] executes the following:
wait (m[i]);wait (m[(i+1) mode 4]);
.........
release (m[i]); release (m[(i+1)mod 4]);
This could cause
(a) Thrashing (b) Deadlock
(c) Starvation, but not deadlock (d) None of the above
7. The following program consists of 3 concurrent processes and 3 binary semaphores. The
semaphores are initialized as S0=1, S1=0, S2=0.
Process P0 Process P1 Process P2
while (true) { wait (S1); wait (S2);
wait (S0); Release (S0); release (S0);
print (0);
release (S1);
release (S2);
}

How many times will process P0 print '0'?


(a) At least twice (b) Exactly twice
(c) Exactly thrice (d) Exactly once
7. The following program consists of 3 concurrent processes and 3 binary semaphores. The
semaphores are initialized as S0=1, S1=0, S2=0.
Process P0 Process P1 Process P2
while (true) { wait (S1); wait (S2);
wait (S0); Release (S0); release (S0);
print (0);
release (S1);
release (S2);
}

How many times will process P0 print '0'?


(a) At least twice (b) Exactly twice
(c) Exactly thrice (d) Exactly once
Consider the methods used by processes P1 and P2 for accessing their critical sections. 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;

Which one of the following statements describes the properties achieved ?


a. Mutual exclusion but not progress
b. Progress but not mutual exclusion
c. Neither mutual exclusion nor progress
d. Both mutual exclusion and progress
A relationship between processes such that each has some part (critical section)
which must not be executed while the critical section of another is being executed,
is known as
a. Semaphore
b. Mutual exclusion
c. Multiprogramming
d. Message passing
A relationship between processes such that each has some part (critical section)
which must not be executed while the critical section of another is being executed,
is known as
a. Semaphore
b. Mutual exclusion
c. Multiprogramming
d. Message passing
………….. synchronizes critical resources to prevent dead lock.
a. P-operator
b. V-operator
c. Semaphore
d. Swapping
………….. synchronizes critical resources to prevent dead lock.
a. P-operator
b. V-operator
c. Semaphore
d. Swapping
In order to allow only one process to enter its critical section, binary
semaphore are initialized to
a. 0
b. 1
c. 2
d. 3
In order to allow only one process to enter its critical section, binary
semaphore are initialized to
a. 0
b. 1
c. 2
d. 3
A semaphore count of negative n means (s = –n) that the queue contains
………….. waiting processes.
a. n+1
b. N
c. N-1
d. 0
A semaphore count of negative n means (s = –n) that the queue contains
………….. waiting processes.
a. n+1
b. N
c. N-1
d. 0
Semaphores are used to:
a. Synchronise critical resources to prevent deadlock
b. Synchronise critical resources to prevent contention
c. Do I/o
d. Facilitate memory management
When a process is accessing shared modifiable data, the process
is said to be in:
a. Dead lock
b. Critical Section
c. Multitasking
d. Context Switching

You might also like