Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Distributed Systems

Lab 3 – Threads and Socket Programming

Lab 3 – Threads and Socket Programming

Lab contents
1. Dining Philosophers problem overview (10 mins)

2. Task 1: Developing the source code for the dining philosophers problems using threads

3. Task 2: Email server using TCP protocol

1. Dining Philosophers Problem


The Dining Philosophers Problem is an example of a concurrency problem dealing with the
allocation of limited resources among competing processes. In this section, we will
understand how to avoid deadlock conditions in dining philosophers’ problem. It is the
undesirable condition of concurrent systems. It is marked as in a circular waiting state. First,
we will discuss the Dining Philosophers Problem that is used in the operating systems after
that we will move towards the solution. Also, we will implement the solution in a Java
program.

Dining Philosophers Problem:


The pictorial representation of the problem is as follows.
Distributed Systems
Lab 3 – Threads and Socket Programming
Distributed Systems
Lab 3 – Threads and Socket Programming

The above figure represents that there are five philosophers (labeled with P1, P2, P3, P4, and
P5) sitting around a circular dining table. There are five plates of noodles to eat when
philosophers feel hungry. To eat noodles, there are five forks/ chopsticks (labeled with 1 to
5) placed between each philosopher.

Each philosopher eats and thinks alternatively. There are the following conditions followed
by each philosopher:

1. A philosopher will use both forks/ chopsticks (right and left) to eat.
2. The remaining fork may be picked up by any one of its adjacent philosophers but not
both.
3. A philosopher may have noodles if both forks are available.
4. After eating, a philosopher will put down both forks and starts thinking again.
5. Those forks can be picked by the other philosophers who will repeat the same process.
6. No two neighbor philosophers (right and left) can eat together.

Initially, all philosophers are thinking. After some time, gets hungry and want to eat
noodles. The philosopher looks for the forks on either side. When the philosopher gets both
the forks, he starts eating. After eating, he puts down the forks and starts thinking again.
When the philosopher puts down the forks, those forks may be used by neighbor
philosophers.

In such a case, there is the possibility of deadlock, a condition in which two or more
processes cannot continue execution. The problem is used to design a scheduling technique
in such a way no philosopher will starve.

Task 1 (20 mins):

Develop the source code for the dining philosophers’ problems using threads.

Task 2 (45 mins):

Consider the provided source code for both an email server (within the file EmailServer.java),
and an email client (within the file EmailClient.java). You should complete the client, update
the server as needed, and run the program so that several clients would be communicating
with the server simultaneously. The details of this simplified client-server application follow:

• The server recognizes only two users, called Nader and Nada.
Distributed Systems
Lab 3 – Threads and Socket Programming

• Each of the above users has a message box on the server that can accept a maximum
of 10 messages.
• Each user may either send a one-line message to the other or read
his/her own messages.
• A count is kept of the number of messages in each mailbox. As another message is
received, the appropriate count is incremented (if the maximum has not been
reached). When messages are read, the appropriate count is reduced to zero.
• When sending a message, the client sends three things: the user's name, the word
'send' and the message itself.
• When requesting reading of mail, the client sends two things: the user's name and
the word 'read'.
• As each message is received by the server, it is added to the appropriate mailbox (if
there is room). If the mailbox is full, the message is ignored.
• When a read request is received, the server first sends an integer indicating the
number of messages (possibly 0) that will be sent and then transmits the messages
themselves (after which it reduces the appropriate message count to 0).

You might also like