TSN2101/TOS2111 - Tutorial 5 (Process Synchronization) : Deposit (Amount) Withdraw (Amount) Withdraw Deposit
TSN2101/TOS2111 - Tutorial 5 (Process Synchronization) : Deposit (Amount) Withdraw (Amount) Withdraw Deposit
1
4. Write two short methods that implement the simple semaphore wait() and signal() operations
on global variable, S.
Solution:
wait (S) {
while (S <= 0);
S--;
}
signal (S) {
S++;
}
5. What is the meaning of the term busy waiting? Can busy waiting be avoided altogether? Explain your
answer.
Solution:
Busy waiting means that a process is waiting for a condition to be satisfied in a tight loop without
relinquishing the processor. Alternatively, a process could wait by relinquishing the processor, and
block on a condition and wait to be awakened at some appropriate time in the future. Busy waiting can
be avoided but incurs the overhead associated with putting a process to sleep and having to wake it up
when the appropriate program state is reached.
6. The following program segment is used to manage a finite number of instances of an available
resource. The maximum number of resources and the number of available resources are declared as
follows:
#define MAX_RESOURCES 5
int available_resources = MAX_RESOURCES;
2
When a process wants to return a number of resources, it calls the increase_count() function:
Solution: