0% found this document useful (0 votes)
65 views

TSN2101/TOS2111 - Tutorial 5 (Process Synchronization) : Deposit (Amount) Withdraw (Amount) Withdraw Deposit

The document discusses several key concepts in operating systems and process synchronization: 1. It describes Peterson's solution to the critical section problem, which uses shared variables and atomic instructions to allow two processes to alternate access to a critical section. 2. It discusses how semaphores can be used to solve the bounded buffer problem by implementing wait() and signal() operations on a shared semaphore variable. 3. It presents the dining philosophers problem, where multiple processes must synchronize access to limited shared resources, as an analogy for resource scheduling challenges in operating systems.

Uploaded by

Moon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

TSN2101/TOS2111 - Tutorial 5 (Process Synchronization) : Deposit (Amount) Withdraw (Amount) Withdraw Deposit

The document discusses several key concepts in operating systems and process synchronization: 1. It describes Peterson's solution to the critical section problem, which uses shared variables and atomic instructions to allow two processes to alternate access to a critical section. 2. It discusses how semaphores can be used to solve the bounded buffer problem by implementing wait() and signal() operations on a shared semaphore variable. 3. It presents the dining philosophers problem, where multiple processes must synchronize access to limited shared resources, as an analogy for resource scheduling challenges in operating systems.

Uploaded by

Moon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

TSN2101/TOS2111

– Tutorial 5 (Process Synchronization)



1. What three conditions must be satisfied in order to solve the critical section problem?

Solution:
In a solution to the critical section problem, no process may be executing in its critical section if a
process is currently executing in its critical section (Mutual Exclusion). Furthermore, only those
processes that are not executing in their remainder sections can participate in the decision on which
process will enter its critical section next and this selection cannot be postponed indefinitely
(Progress). Finally, a bound must exist on the number of times that other processes are allowed to
enter their critical section after a process has made a request to enter its critical section and before
that request is granted (Bounded Waiting).

2. Race conditions are possible in many computer systems. Consider a banking system with the
following two functions: deposit(amount) and withdraw(amount). These two functions are
passed the amount that is to be deposited or withdrawn from a bank account. Assume a shared bank
account exists between a husband and wife and concurrently the husband calls the withdraw()
function and the wife calls deposit(). Describe how a race condition is possible and what might be
done to prevent the race condition from occurring.

Solution:
Assume the balance in the account is 250.00 and the husband calls withdraw(50) and the wife calls
deposit(100). Obviously the correct value should be 300.00 Since these two transactions will be
serialized, the local value of balance for the husband becomes 200.00, but before he can commit the
transaction, the deposit(100) operation takes place and updates the shared value of balance to
300.00 We then switch back to the husband and the value of the shared balance is set to 200.00 -
obviously an incorrect value.

3. Briefly explain Peterson’s solution to the critical-section problem.

Solution:
Peterson’s solution is restricted to two processes that alternate execution between their critical
sections and remainder sections.
Assume that the LOAD and STORE instructions are atomic; that is, they cannot be interrupted.
The two processes share two variables, int turn; and Boolean flag[2].
The variable turn indicates whose turn it is to enter the critical section.
The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies
that process Pi is ready to enter its critical section.
The structure of the process Pi in Peterson’s solution is given below:
do {
flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = FALSE;
remainder section
} while (TRUE);





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;

When a process wishes to obtain a number of resources, it invokes the decrease_count()


function:

/* decrease available_resources by count resources */


/* return 0 if sufficient resources available, */
/* otherwise return -1 */
int decrease_count(int count) {
if (available_resources < count)
return -1;
else {
available_resources -= count;
return 0;
}
}

2
When a process wants to return a number of resources, it calls the increase_count() function:

/* increase available_resources by count */


int increase_count(int count) {
available_resources += count;
return 0;
}

The preceding program segment produces a race condition. Do the following:
a. Identify the data involved in the race condition.
b. Identify the location (or locations) in the code where the race condition occurs.
c. Using a semaphore, fix the race condition.

Solution:

a. The variable available_resources.


b. The code that decrements available_resources and the code that increments
available_resources are the statements that could be involved in race conditions.
c. Use a semaphore to represent the available_resources variable and replace
increment and decrement operations by semaphore increment and semaphore decrement
operations.

7. Describe the dining-philosophers problem and how it relates to operating systems.

Solution:

The scenario involves five philosophers sitting at a round table with a bowl of food and five
chopsticks. Each chopstick sits between two adjacent philosophers. The philosophers are allowed to
think and eat. Since two chopsticks are required for each philosopher to eat, and only five chopsticks
exist at the table, no two adjacent philosophers may be eating at the same time. A scheduling
problem arises as to who gets to eat at what time. This problem is similar to the problem of
scheduling processes that require a limited number of resources.


Reference:
• Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, “Operating System Concepts”, 9/E, John
Wiley & Sons, 2013.

You might also like