OS Process Management
Question 1 |
if (fork() == 0) { a = a + 5; printf("%d,%dn", a, &a); } else { a = a –5; printf("%d, %dn", a, &a); }Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?
u = x + 10 and v = y | |
u = x + 10 and v != y | |
u + 10 = x and v = y | |
u + 10 = x and v != y |
Discuss it
Question 2 |
void P (binary_semaphore *s) { unsigned y; unsigned *x = &(s->value); do { fetch-and-set x, y; } while (y); } void V (binary_semaphore *s) { S->value = 0; }Which one of the following is true?
The implementation may not work if context switching is disabled in P. | |
Instead of using fetch-and-set, a pair of normal load/store can be used | |
The implementation of V is wrong | |
The code does not implement a binary semaphore |
Discuss it
Question 3 |
Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the P operation on semaphores c, d, and a before entering the respective code segments. After completing the execution of its code segment, each process invokes the V operation (i.e., signal) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one of the following represents a deadlockfree order of invoking the P operations by the processes? (GATE CS 2013)
X: P(a)P(b)P(c) Y:P(b)P(c)P(d) Z:P(c)P(d)P(a) | |
X: P(b)P(a)P(c) Y:P(b)P(c)P(d) Z:P(a)P(c)P(d) | |
X: P(b)P(a)P(c) Y:P(c)P(b)P(d) Z:P(a)P(c)P(d) | |
X: P(a)P(b)P(c) Y:P(c)P(b)P(d) Z:P(c)P(d)P(a) |
Discuss it
Option A can cause deadlock. Imagine a situation process X has acquired a, process Y has acquired b and process Z has acquired c and d. There is circular wait now. Option C can also cause deadlock. Imagine a situation process X has acquired b, process Y has acquired c and process Z has acquired a. There is circular wait now. Option D can also cause deadlock. Imagine a situation process X has acquired a and b, process Y has acquired c. X and Y circularly waiting for each other. See http://www.eee.metu.edu.tr/~halici/courses/442/Ch5%20Deadlocks.pdf Consider option A) for example here all 3 processes are concurrent so X will get semaphore a, Y will get b and Z will get c, now X is blocked for b, Y is blocked for c, Z gets d and blocked for a. Thus it will lead to deadlock. Similarly one can figure out that for B) completion order is Z,X then Y. This question is duplicate of https://www.geeksforgeeks.org/gate-gate-cs-2013-question-16/ Watch GeeksforGeeks Video Explanation :
Question 4 |
-2 | |
-1 | |
1 | |
2 |
Discuss it
Semaphore S is initialized to 2 Process W executes S=1, x=1 but it doesn't update the x variable. Then process Y executes S=0, it decrements x, now x= -2 and signal semaphore S=1 Now process Z executes s=0, x=-4, signal semaphore S=1 Now process W updates x=1, S=2 Then process X executes X=2So correct option is (D). Watch GeeksforGeeks Video Explanation :
Question 5 |
-2 | |
-1 | |
1 | |
2 |
Discuss it
Question 6 |
Process X: Process Y: private i; private i; for (i=0; i < n; i++) { for (i=0; i < n; i++) { a[i] = f(i); EntryY(R, S); ExitX(R, S); b[i]=g(a[i]); } }Which one of the following represents the CORRECT implementations of ExitX and EntryY? (A)
ExitX(R, S) { P(R); V(S); } EntryY (R, S) { P(S); V(R); }(B)
ExitX(R, S) { V(R); V(S); } EntryY(R, S) { P(R); P(S); }(C)
ExitX(R, S) { P(S); V(R); } EntryY(R, S) { V(S); P(R); }(D)
ExitX(R, S) { V(R); P(S); } EntryY(R, S) { V(S); P(R); }
A | |
B | |
C | |
D |
Discuss it
The purpose here is neither the deadlock should occur nor the binary semaphores be assigned value greater than one. A leads to deadlock B can increase value of semaphores b/w 1 to n D may increase the value of semaphore R and S to 2 in some cases
Question 7 |
X: P(a)P(b)P(c) Y: P(b)P(c)P(d) Z: P(c)P(d)P(a) | |
X: P(b)P(a)P(c) Y: P(b)P(c)P(d) Z: P(a)P(c)P(d) | |
X: P(b)P(a)P(c) Y: P(c)P(b)P(d) Z: P(a)P(c)P(d) | |
X: P(a)P(b)P(c) Y: P(c)P(b)P(d) Z: P(c)P(d)P(a) |
Discuss it
Question 8 |
A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution?
-2 | |
-1 | |
1 | |
2 |
Discuss it
Background Explanation: A critical section in which the process may be changing common variables, updating table, writing a file and perform another function. The important problem is that if one process is executing in its critical section, no other process is to be allowed to execute in its critical section. Each process must request permission to enter its critical section. A semaphore is a tool for synchronization and it is used to remove the critical section problem which is that no two processes can run simultaneously together so to remove this two signal operations are used named as wait and signal which is used to remove the mutual exclusion of the critical section. as an unsigned one of the most important synchronization primitives, because you can build many other Decrementing the semaphore is called acquiring or locking it, incrementing is called releasing or unlocking. Solution : Since initial value of semaphore is 2, two processes can enter critical section at a time- this is bad and we can see why. Say, X and Y be the processes.X increments x by 1 and Z decrements x by 2. Now, Z stores back and after this X stores back. So, final value of x is 1 and not -1 and two Signal operations make the semaphore value 2 again. So, now W and Z can also execute like this and the value of x can be 2 which is the maximum possible in any order of execution of the processes. (If the semaphore is initialized to 1, processed would execute correctly and we get the final value of x as -2.) Option (D) is the correct answer. Another Solution: Processes can run in many ways, below is one of the cases in which x attains max value Semaphore S is initialized to 2 Process W executes S=1, x=1 but it doesn't update the x variable. Then process Y executes S=0, it decrements x, now x= -2 and signal semaphore S=1 Now process Z executes s=0, x=-4, signal semaphore S=1 Now process W updates x=1, S=2 Then process X executes X=2 So correct option is D Another Solution: S is a counting semaphore initialized to 2 i.e., Two process can go inside a critical section protected by S. W, X read the variable, increment by 1 and write it back. Y, Z can read the variable, decrement by 2 and write it back. Whenever Y or Z runs the count gets decreased by 2. So, to have the maximum sum, we should copy the variable into one of the processes which increases the count, and at the same time the decrementing processed should run parallel, so that whatever they write back into memory can be overridden by incrementing process. So, in effect decrement would never happen.
Related Links: http://quiz.geeksforgeeks.org/process-synchronization-set-1/ https://www.geeksforgeeks.org/operating-systems-process-management-question-11/ for explanation This solution is contributed by Nitika Bansal
Question 9 |
Process X: Process Y: private i; private i; for (i=0; i < n; i++) { for (i=0; i < n; i++) { a[i] = f(i); EntryY(R, S); ExitX(R, S); b[i]=g(a[i]); } }Which one of the following represents the CORRECT implementations of ExitX and EntryY? (A)
ExitX(R, S) { P(R); V(S); } EntryY (R, S) { P(S); V(R); }(B)
ExitX(R, S) { V(R); V(S); } EntryY(R, S) { P(R); P(S); }(C)
ExitX(R, S) { P(S); V(R); } EntryY(R, S) { V(S); P(R); }(D)
ExitX(R, S) { V(R); P(S); } EntryY(R, S) { V(S); P(R); }
A | |
B | |
C | |
D |
Discuss it
The purpose here is neither the deadlock should occur nor the binary semaphores be assigned value greater than one. A leads to deadlock B can increase value of semaphores b/w 1 to n D may increase the value of semaphore R and S to 2 in some casesSee http://geeksquiz.com/operating-systems-process-management-question-13/
Question 10 |
fork(); fork(); fork();The total number of child processes created is
3 | |
4 | |
7 | |
8 |
Discuss it
fork (); // Line 1 fork (); // Line 2 fork (); // Line 3 L1 // There will be 1 child process created by line 1 / \ L2 L2 // There will be 2 child processes created by line 2 / \ / \ L3 L3 L3 L3 // There will be 4 child processes created by line 3We can also use direct formula to get the number of child processes. With n fork statements, there are always 2^n – 1 child processes. Also see this post for more details.