0% found this document useful (0 votes)
20 views4 pages

Dining Philosophers & Email Server Lab

Uploaded by

Amr Mohamed
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)
20 views4 pages

Dining Philosophers & Email Server Lab

Uploaded by

Amr Mohamed
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

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 [Link]),
and an email client (within the file [Link]). 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).

Common questions

Powered by AI

The Dining Philosophers problem highlights the trade-off between simplicity and control in concurrent system design. A simple design aims to provide all philosophers with the same level of access to resources, which can preserve fairness but may lead to resource contention and deadlock due to lack of control. Introducing control through hierarchies, waiters, or resource allocation protocols complicates the system but better manages resources and reduces deadlock risk. Balancing these aspects requires carefully designing algorithms that ensure the system is both efficient and simple enough to manage easily while avoiding starvation and ensuring fairness .

To simulate interaction between multiple email clients and the server in a distributed system, multi-threading can be utilized to handle simultaneous client connections independently. Each client can operate on an independent thread, enabling them to send and receive messages without interference. The server can use thread pooling to manage resource allocation efficiently and ensure each client session does not impact others. The use of non-blocking I/O or asynchronous communication methods can also enhance the scalability and responsiveness of the system under concurrent access .

Challenges in an email server system recognizing only two users include limited scalability and flexibility, leading to restricted practical utility. To extend the system to support more users, a dynamic user management system can be implemented, with a database storing user information and messages indexed by user identifiers. This requires expanding the server’s architecture to handle increased connections and message volumes, optimizing resource allocation, and implementing robust authentication mechanisms to manage user data securely and efficiently .

To optimize resource utilization in the Dining Philosophers problem without leading to starvation, a priority system can be introduced where each philosopher is assigned a priority level that dynamically changes based on how long they’ve been waiting to eat. By allowing philosophers with higher waiting times to gain access to forks first, this can ensure no philosopher starves. Additionally, implementing a ‘chopstick hierarchy’ or using a waiter (resource allocator) to arbitrate requests for forks can avoid deadlocks and increase efficiency by dynamically reallocating forks based on demand patterns .

The TCP protocol provides reliable, ordered, and error-checked delivery of data between server and client in the email application, ensuring messages are transmitted accurately and in sequence. This is crucial for maintaining the integrity of communication, as loss of message order or data could lead to miscommunication or missing information. TCP’s flow control and congestion management features help in managing network traffic efficiently, making it a suitable choice for systems where data integrity and reliability are paramount, such as in an email client-server architecture .

Deadlock in the Dining Philosophers problem can be compared to real-world scenarios such as traffic gridlocks, where vehicles wait for each other to move without relinquishing their position, or concurrent database transactions that hold locks over resources waiting for others to release theirs. Similarly, in operating systems, deadlock can occur when multiple processes hold locks on resources and simultaneously wait for resources locked by others. These situations require careful resource scheduling and allocation algorithms to ensure efficient and deadlock-free operation, akin to strategies used in managing the philosophers’ access to forks .

The Dining Philosophers problem specifies several conditions that can result in deadlock: each philosopher requires two forks to eat, and two adjacent philosophers cannot eat simultaneously if they share a fork. This results in a situation where all philosophers are waiting indefinitely if each holds one fork and waits for the other. This scenario exemplifies common concurrency issues of finite resource allocation, circular wait, mutual exclusion, and no preemption, demonstrating how processes can become perpetually blocked and unable to proceed without a mechanism to recover from such states .

The Email Client-Server implementation respects the mailbox limit by maintaining a count of the number of messages in each user's mailbox. When a new message is received, the server checks the current count against the maximum allowed (10 messages). If the mailbox is full, any additional messages are ignored and not added to the mailbox. This ensures that the server does not exceed the specified resource limits. When messages are read, the system resets the count to zero, ensuring space is available for new messages .

To avoid deadlock in the Dining Philosophers problem, several strategies can be implemented. One approach is to impose a limit on the number of philosophers that can pick up the forks at the same time, ensuring that at least one philosopher can always eat. Additionally, an algorithm that prevents circular wait conditions, such as having each philosopher pick up forks in a predetermined order (e.g., first pick up the lowest numbered fork), can also be effective. Monitoring for hungry philosophers and replacing a philosopher's action with a waiting step if others are unable to proceed can prevent deadlock. The use of mutexes and semaphores for locking mechanisms to handle resource allocation efficiently can also help in avoiding deadlock .

Ignoring messages when a mailbox is full could lead to data loss and communication failures, as messages intended for a user could be permanently lost, reducing the reliability and completeness of the communication system. This can impact user experience and lead to critical information never reaching its intended recipient. It ensures that system constraints are not violated but highlights the need for effective resource management and potential implementation of overflow handling strategies like queued buffering or notification systems to alert users, prompting them to read and clear messages .

You might also like