# Operating System Questions and Answers – Semaphores – 2

This set of Operating System Questions and Answers for freshers focuses on “Semaphores – 2” and will also be useful for interview preparations for freshers.

1. What will happen if a non-recursive mutex is locked more than once?
a) Starvation
c) Aging
d) Signaling

Explanation: If a thread which had already locked a mutex, tries to lock the mutex again, it will enter into the waiting list of that mutex, which results in a deadlock. It is because no other thread can unlock the mutex.

2. What is a semaphore?
a) is a binary mutex
b) must be accessed from only one process
c) can be accessed from multiple processes
d) none of the mentioned

Explanation: None.

3. What are the two kinds of semaphores?
a) mutex & counting
b) binary & counting
c) counting & decimal
d) decimal & binary

Explanation: None.

4. What is a mutex?
a) is a binary mutex
b) must be accessed from only one process
c) can be accessed from multiple processes
d) none of the mentioned

Explanation: None.

5. At a particular time of computation the value of a counting semaphore is 7.Then 20 P operations and 15 V operations were completed on this semaphore. The resulting value of the semaphore is? (GATE 1987)
a) 42
b) 2
c) 7
d) 12

Explanation: P represents Wait and V represents Signal. P operation will decrease the value by 1 every time and V operation will increase the value by 1 every time.

6. A binary semaphore is a semaphore with integer values ____________
a) 1
b) -1
c) 0.8
d) 0.5

Explanation: None.

7. The following pair of processes share a common variable X.

```Process A
int Y;
A1: Y = X*2;
A2: X = Y;

Process B
int Z;
B1: Z = X+1;
B2: X = Z;```

X is set to 5 before either process begins execution. As usual, statements within a process are executed sequentially, but statements in process A may execute in any order with respect to statements in process B.
How many different values of X are possible after both processes finish executing?
a) two
b) three
c) four
d) eight

Explanation: Here are the possible ways in which statements from A and B can be interleaved.
A1 A2 B1 B2: X = 11
A1 B1 A2 B2: X = 6
A1 B1 B2 A2: X = 10
B1 A1 B2 A2: X = 10
B1 A1 A2 B2: X = 6
B1 B2 A1 A2: X = 12.

8. The program follows to use a shared binary semaphore T.

```Process A
int Y;
A1: Y = X*2;
A2: X = Y;
signal(T);

Process B
int Z;
B1: wait(T);
B2: Z = X+1;
X = Z;```

T is set to 0 before either process begins execution and, as before, X is set to 5.
Now, how many different values of X are possible after both processes finish executing?
a) one
b) two
c) three
d) four

Explanation: The semaphore T ensures that all the statements from A finish execution before B begins. So now there is only one way in which statements from A and B can be interleaved:
A1 A2 B1 B2: X = 11.

9. Semaphores are mostly used to implement ____________
a) System calls
b) IPC mechanisms
c) System protection
d) None of the mentioned

Explanation: None.

10. Spinlocks are intended to provide __________ only.
a) Mutual Exclusion
b) Bounded Waiting
c) Aging
d) Progress

Explanation: None.

Sanfoundry Global Education & Learning Series – Operating System.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]