You are on page 1of 2

SANTOS, RENZ MARIE Z.

BE2101

OPREATING SYSTEMS

Case Problem:

Process Synchronization Problem

In a training session, you are allowed to eat and write but here are the following conditions.

1. There are five members in your team and you are all seated on a round table.
2. You are allowed to eat and write but everyone must eat using two forks.
3. There are only 5 forks placed in between your meals (see figure below).

Explain how can everyone can eat simultaneously such that no one on the table will starve. [20 points]

Solution:

One simple solution is to represent each fork with a semaphore.

One simple solution is to represent each fork with a semaphore. We know that a fork should be used by
only one member at a time and when one member is holding it, no other member should be allowed to
take it away from him and it should allow to access that same fork at the same time. So we can do that
with the help of semaphore.

A member tries to grab a fork by executing a wait() operation on that semaphore.

A member tries to grab a fork by executing a wait() operation on that semaphore. We will be representing
each of these forks with a help of semaphore. When a member wants to grab a fork, he will be executing
wait() semaphore on the fork. We know that when a wait() operation is executed on a semaphore, it
implies that particular process wants to use that semaphore and it wants to get hold of it and when it gets
hold of it no other processes will be allowed to use that semaphore. Similarly, we’ll be applying this on
the fork, so the member will execute a wait() operation on the fork when he wants to use it and then he
releases his fork by executing signal() operation on the appropriate semaphore.

He releases his fork by executing signal() operation on the appropriate semaphore.

After a member finished eating, he will have to put back the forks at their places so, at that time will be
doing so by executing the signal() operation on the fork because we are assuming that they are
semaphores. When a signal() operation is executed, semaphores signal() operation means we are
releasing it, in other words, a member will be releasing his forks by executing the signal() operation.

Thus, the shared data are:

Semaphore fork[5];

Where all the elements of fork are initialized to 1

This means that we have an array of five 5 elements. So, we’ll be having fork1, fork2, fork3, fork4, and
fork5, and they are all declared as semaphores.

We, will be using binary semaphore. In binary semaphore, it can take two values either one 1 or zero 0.
Each of the fork can have two values either 0 or 1. Initially, all the elements of forks are initialized to one
1. In binary semaphore, when it is initialized to 1 means it is free, it can be used by the processes that
wants to use it and if the value is 0, that means it is currently being held by some processes, it is not free.
So, all the forks will be initialized to 1.

The structure of member i


do {
wait(fork[i]);
wait(fork [(i + 1) % 5]);
…………
//eat
signal(fork[i]);
signal(fork [(i + 1) % 5]);
//write
} while (true);

You might also like