Example: Producer-Consumer problem
There are two processes: Producer and Consumer. The producer produces some items and the Consumer consumes that item. The two processes share a common space or memory location known as a buffer where the item produced by the Producer is stored and from which the Consumer consumes the item if needed. There are two versions of this problem: the first one is known as the unbounded buffer problem in which the Producer can keep on producing items and there is no limit on the size of the buffer, the second one is known as the bounded buffer problem in which the Producer can produce up to a certain number of items before it starts waiting for Consumer to consume it. We will discuss the bounded buffer problem. First, the Producer and the Consumer will share some common memory, then the producer will start producing items. If the total produced item is equal to the size of the buffer, the producer will wait to get it consumed by the Consumer. Similarly, the consumer will first check for the availability of the item. If no item is available, the Consumer will wait for the Producer to produce it. If there are items available, Consumer will consume them.
In this method, processes communicate with each other without using any kind of shared memory. If two processes p1 and p2 want to communicate with each other, they proceed as follows:
Rules of Critical Section
int turn
Boolean flag [2]
Structure of process Pi
while(true)
{
flag[i] = true; // entry section
turn = j;
while(flag[j] == true && turn == j); // checking if other process is interested or it is already in CS.
// critical section
flag[i] = false; // exit section
}
P0
while(true)
{
flag[0] = true;
turn = 1;
while(flag[1] == true && turn == 1);
// critical section
flag[0] = false;
}
P1
while(true)
{
flag[1] = true;
turn = 0;
while(flag[0] == true && turn == 0);
// critical section
flag[1] = false;
}
Now let us see how it does so.
P(Semaphore s)
{
while(s == 0); /* wait until s = 0 */
s = s - 1;
}
V(Sempahore s)
{
s = s + 1;
}
Some points regarding P and V operaton:
Now, let use see how it implements mutual exclusion.
Limitations:
The main problem with semaphores is that they require busy waiting, if a process is in the critical section, then other processes trying to enter the critical section will be waiting until the critical section is not occupied by any other process. Whenever any process waits then it continously checks for semaphore value (look at this likne while(s == 0); in P operation) and waste CPU cycle.
Advantages of Semaphore
Disadvantages of Semaphore
Examples of deadlock
Deadlock can arise if the following four conditions hold simultaneously (Necessary Conditions)
There are three ways to handle deadlock
1. Deadlock Prevention
Let's see how we can prevent each of the conditions.
2. Deadlock avoidance
3. Deadlock ignorance
Advantages
Disadvantages of Banker's algorithm
When working with Banker's algorithm, it requests to know about three things:
Following are the important data structures terms applied in the banker's algorithm as follows:
Suppose n is the number of processes, and m is the number of each type of resource used in a computer system.
Consider a system that contains five processes P1, P2, P3, P4, P5 and the three resource types A, B and C. Following are the resources types: A has 10, B has 5 and the resource type C has 7 instances.
Answer the following questions using the banker's algorithm:
Ans 1.
Content of the need matrix is as follows:
Need[i] = Max[i] - Allocation[i]
Need for P1: (7, 5, 3) - (0, 1, 0) = 7, 4, 3
Need for P2: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P3: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P4: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P5: (4, 3, 3) - (0, 0, 2) = 4, 3, 1
Hence, we created the context of need matrix.
Ans 2.
Apply the Banker's algorithm
Available resources of A, B and C are 3, 3 and 2.
Now we check if each type of resource request is available for each process.
Steps ↓
Ans 3.
For granting the request (1, 0, 2) first we have to check that if Request <= Available, that is (1, 0, 2) <=(3, 3, 2), since the condition is true. So the process gets the request immediately.
Advantages of Process Termination
Disadvantages of Process Termination:
Advantages of Resource Preemption:
Disadvantages of Resource Preemption:
Process Allocation Max Available
A B C A B C A B C
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
Answer the following questions using the Banker's algorithm:
Process Allocated Max Need Available
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 1 0 7 5 3 3 3 4
P2 2 0 2 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2