Operating System (Algorithms) : Q. Producer - Consumer Problem Ans
Operating System (Algorithms) : Q. Producer - Consumer Problem Ans
Q. Peterson's Solution
Ans:
do {
flag[i] = TRUE;
turn = j ;
while (flag[j] turn == j ) ;
//critical section
flag[i] = FALSE;
//remainder section
} while (TRUE);
// critical section
lock = FALSE;
// remainder section
}while (TRUE);
*a = *b;
*b = temp;
// critical section
lock = FALSE;
// remainder section
}while (TRUE);
Q. Bounded-waiting mutual exclusion with TestAndSet ()
Ans:
do {
waiting [i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting [i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
}while (TRUE);
wait(S) {
while S <= 0
; // no-op
S--;
}
signal(mutex);
// remainder section
}while (TRUE);
Q. Implementation of Semaphore
Ans:
typedef struct {
int value;
struct process *list;
}semaphore;
Producer:
anytype item;
do {
/* produce something */
item = produce();
} while (TRUE);
Consumer:
anytype item;
do {
/* consume it */
consume(item);
} while (TRUE);
...
// reading is performed
...
wait (mutex)
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex);
}while (TRUE);
Q. Syntax of Monitor
Ans:
monitor monitor name
{
// shared variable declarations
procedure P1 (. . .) {
...
}
procedure P2 (. . .) {
...
}
..
..
..
procedure Pn (. . .) {
...
}
initialization code (. . .) {
...
}
signal(chopstick [i]);
signal(chopstick [(i + 1) % 5]);
// think
}while (TRUE);
void putdown(int i) {
state [i] = THINKING;
test((i + 4) % 5} ;
test( (i + 1) % 5) ;
}
void test(int i) {
if ((state [(i + 4) % 5] != EATING) && (state [i] == HUNGRY) &&
(state [(i + 1) % 5] != EATING)) {
state [i] = EATING;
self [i] .signal() ;
}
initialization-code () {
for (int i = 0; i < 5; i++)
state [i] = THINKING;
}
}
procedure insert(item) {
if (count == MAX)
wait(full); // if buffer is full, block
insert_item(item); // put item in buffer
count = count + 1; // increment count of full slots
if (count == 1)
signal(empty); // if buffer was empty, wake consumer
}
procedure remove() {
if (count == 0)
wait(empty); // if buffer is empty, block
remove_item(item); // remove item from buffer
count = count – 1; // decrement count of full slots
if (count == MAX – 1)
signal(full); // if buffer was full, wake producer
}
count = 0;
end monitor;
procedure Producer() {
while (TRUE)
{
produce_item(item); // make a new item
ProducerConsumer.insert(item); // call enter function in monitor
}
}
procedure Consumer() {
while (TRUE)
{
ProducerConsumer.remove; // call remove function in monitor
consume_item; // consume an item
}
}
➔ Available. A vector of length m indicates the number of available resources of each type. If
Available[j] equals k, there are k instances of resource type Rj available.
➔ Max. An n x m matrix defines the maximum demand of each process. If Max[i][j] equals k, then
process Pi may request at most k instances of resource type Rj.
➔ Allocation. An n x in matrix defines the number of resources of each type currently allocated to
each process. If Allocation[i][j] equals k, then process Pi is currently allocated k instances of
resource type Rj.
➔ Need. An n x m matrix indicates the remaining resource need of each process. If Need[i][j]
equals k, then process Pi may need k more instances of resource type Rj to complete its task.
Note that Need[i][j] equals Max[i][j] - Allocation[i][j].
Safety Algorithm
We can now present the algorithm for finding out whether or not a system is in a safe state. This
algorithm can be described, as follows:
a. Finish[i] == false
Finish[i] = true
Go to step 2.
Resource-Request Algorithm
We now describe the algorithm which determines if requests can be safely granted.
Let Requesti be the request vector for process Pi, If Request[i] == k, then process Pi, wants k
instances of resource type Rj. When a request for resources is made by process Pi, the following
actions are taken:
1. If Requesti <= Needi, go to step 2. Otherwise, raise an error condition, since
a. Finish[i] == false
Finish[i] = true
Go to step 2.
4. If Finish[i] == false, for some i, 0 < i < n, then the system is in a deadlocked state.
Moreover, if Finish[i] == false, then process Pi is deadlocked.