Open In App

Algorithms Quiz | SP Contest 1 | Question 10

Like Article
Like
Save
Share
Report

Here are the two concurrent process A, B with respective codes:

Code A:

while (true) // infinite condition
{
    M :____;
    printf("%c", b);
    printf("%c", b);
    N:____;
}

Code B:

while (true) // infinite condition
{
    O:____;
    printf("%c", a);
    printf("%c", a);
    P:____;
}

What should be the binary semaphore operation on M, N, O, P respectively and what must be the initial values of semaphore X, Y in order to get the output bbaabbaabbaa . . . ?
Where P is down and V is up operation respectively.

(A) M = P(Y), N = V(X), O = P(X), P = V(Y); X = 0, Y = 1;
(B) M = P(Y), N = V(X), O = P(X), P = P(Y); X = Y = 1;
(C) M = P(Y), N = V(Y), O = P(X), P = V(X); X = 1, Y = 0;
(D) M = P(Y), N = V(Y), O = P(X), P = V(X); X = Y = 1;


Answer: (A)

Explanation: In semaphore up is always a successful operation but down is not always successful.
In following concurrent process Operations are:
A: code

while (true) // infinite condition
{
M :P(Y); // Y become 0 successful down operation.
printf("%c", b);
printf("%c", b);
N:V(X); // X become 1 successful up operation.
}

B code:

while (true) // infinite condition
{
O:P(X); // X  become 0 successful down operation.
printf("%c", a);
printf("%c", a);
P:V(Y); // Ybecome 1 successful up operation.
}

Here all operation are successful with initial values of X and Y are 0 and 1 respectively.
So, option (A) is correct.


Quiz of this Question


Last Updated : 06 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads