Unit 2
Unit 2
Unit 2
Independent Process
Cooperative Process
Independent Processes
Cooperative Processes
Process Synchronization
It is the task phenomenon of coordinating the execution of
processes in such a way that no two processes can have access to
the same shared data and resources.
2. Progress
3. Bounded Waiting
After a process makes a request for getting into its critical section,
there is a limit for how many other processes can get into their
critical section, before this process's request is granted. So after the
limit is reached, the system must grant the process permission to
get into its critical section.
Some widely used method to solve the critical section problem are
as follows:
1.Peterson's Solution
PROCESS Pi
FLAG[i] = true
while( (turn != i) AND (CS is !free) )
{ wait;
}
CRITICAL SECTION
FLAG[i] = false
turn = j; //choose another process to go to CS
Dekkers solution
The mutual exclusion problem has different solutions. Dekker was a Dutch
mathematician who introduced a software-based solution for the mutual exclusion
problem. This algorithm is commonly called Dekker’s algorithm. The Dekker’s
algorithm was developed for an algorithm for mutual exclusion between two
processes.
flag[i] := true;
while flag[j] do
if turn = j then
begin
flag[i] := false;
while turn = j do no-op;
flag[i] := true;
end;
turn := j;
flag[i] := false;
until false;
Hardware Synchronization Algorithms :
Lock, Test and Set
Process Synchronization problems occur when two processes running concurrently
share the same data or same variable. The value of that variable may not be updated
correctly before its being used by a second process. Such a condition is known as
Race Around Condition. There are software as well as hardware solutions to this
problem. In this article, we will talk about the most efficient hardware solution to
process synchronization problems and its implementation.
Test and Set algorithms in the hardware approach of solving Process Synchronization
problem:
while(1){
while (TestAndSet(lock));
critical section
lock = false;
remainder section
}
There is one Producer in the producer-consumer problem, Producer is producing some items,
whereas there is one Consumer that is consuming the items produced by the Producer. The same
memory buffer is shared by both producers and consumers which is of fixed-size.
The task of the Producer is to produce the item, put it into the memory buffer, and again start
producing items. Whereas the task of the Consumer is to consume the item from the memory
buffer.
Below are a few points that considered as the problems occur in Producer-Consumer:
o The producer should produce data only when the buffer is not full. In case it is found that
the buffer is full, the producer is not allowed to store any data into the memory buffer.
o Data can only be consumed by the consumer if and only if the memory buffer is not empty.
In case it is found that the buffer is empty, the consumer is not allowed to use any data from
the memory buffer.
o Accessing memory buffer should not be allowed to producer and consumer at the same
time.
Producer Code
Consumer Code
Problem Case:
The solution of Producer-Consumer Problem using Semaphore
The above problems of Producer and Consumer which occurred due to context switch and producing
inconsistent result can be solved with the help of semaphores.
To solve the problem occurred above of race condition, we are going to use Binary Semaphore and
Counting Semaphore
Binary Semaphore: In Binary Semaphore, only two processes can compete to enter into
its CRITICAL SECTION at any point in time, apart from this the condition of mutual exclusion is also
preserved.
Counting Semaphore: In counting semaphore, more than two processes can compete to enter into
its CRITICAL SECTION at any point of time apart from this the condition of mutual exclusion is also
preserved.
2. {
3. while( S <= 0) ;
4. S--;
5. }
2. {
3. S++;
4. }
From the above definitions of wait, it is clear that if the value of S <= 0 then it will enter into an
infinite loop (because of the semicolon; after while loop). Whereas the job of the signal is to
increment the value of S.
Let's see the code as a solution of producer and consumer problem using semaphore (Both Binary
and Counting Semaphore):
1. void producer( void )
2. {
3. wait ( empty );
4. wait(S);
5. Produce_item (item P)
6. buffer[ in ] = item P;
7. in = (in + 1)mod n
8. signal(S);
9. signal(full);
10.
11. }
1. void consumer(void)
2. {
3. wait ( full );
4. wait(S);
5. itemC = buffer[ out ];
6. out = ( out + 1 ) mod n;
7. signal(S);
8. signal(empty);
9. }
Before Starting an explanation of code, first, understand the few terms used in the above code:
6. wait(empty) will decrease the value of the counting semaphore variable empty by 1, that is when
the producer produces some element then the value of the space gets automatically decreased by
one in the buffer. In case the buffer is full, that is the value of the counting semaphore variable
"empty" is 0, then wait(empty); will trap the process (as per definition of wait) and does not allow to
go further.
7. wait(S) decreases the binary semaphore variable S to 0 so that no other process which is willing to
enter into its critical section is allowed.
8. signal(s) increases the binary semaphore variable S to 1 so that other processes who are willing to
enter into its critical section can now be allowed.
9. signal(full) increases the counting semaphore variable full by 1, as on adding the item into the
buffer, one space is occupied in the buffer and the variable full must be updated.
10. wait(full) will decrease the value of the counting semaphore variable full by 1, that is when the
consumer consumes some element then the value of the full space gets automatically decreased by
one in the buffer. In case the buffer is empty, that is the value of the counting semaphore variable
full is 0, then wait(full); will trap the process(as per definition of wait) and does not allow to go
further.
11. wait(S) decreases the binary semaphore variable S to 0 so that no other process which is willing
to enter into its critical section is allowed.
12. signal(S) increases the binary semaphore variable S to 1 so that other processes who are willing
to enter into its critical section can now be allowed.