Professional Documents
Culture Documents
Sockets and Threads
Sockets and Threads
Lab contents
1. Dining Philosophers problem overview (10 mins)
2. Task 1: Developing the source code for the dining philosophers problems using threads
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.
Develop the source code for the dining philosophers’ problems using threads.
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).