Assignment 2 (due
Sunday, 30 July, 10 pm)
This assignment
has two parts. In the first part, you
will use the JAVA monitor to implement a semaphore. In the second part, you will implement a centralized
semaphore server which will help you use this semaphore for inter process
communication. In this assignment, you
will learn about inter process communication primitives such as monitors, and
semaphores and about client-server programming using sockets. This assignment can be done either
individually or in groups of two. Assignments submitted with more
than two group members will not be accepted.
You can stick with the same group members or change your group. This assignment will be done in JAVA. As this assignment will form a part of later
assignments, we urge you not to delete this assignment after submitting
it.
What
to do?
Part I:
In this part of
the assignment, you will use JAVA monitor to implement semaphores. More specifically, you will define a class
called Semaphore. This class
will have the following interface.
You may need to use the synchronized primitive and wait (), notify
(), and notifyAll () methods of
JAVA. For a tutorial on how to
use these, see online
java tutorial .
Part II:
In this part of
the assignment, you will use JAVA sockets to implement a client-server
semaphore system. This client-server
system will help us implement a distributed semaphore system, i.e., independent
programs running on different machines (in the same LAN) will be able to use
this distributed semaphore system to do inter process communication. For this assignment, each semaphore will
have a global identifier called semaphoreid, which will be a value of
type int. You may need to define
two classes to implement this system.
The
SemaphoreServer is a free standing JAVA program, which keeps waiting for
requests from the clients. The requests
to the server will be semaphore operations.
In particular, the server must handle the following kinds of semaphore
operations.
Remember
that handling an Enter request might make the server block until an Exit
request is handled. To avoid this
problem, the server must spawn a new thread each time it wants to handle a
request.
In order to do the communication, the server will have to open
a datagram socket specifying a port number and keep listening to it in an
infinite loop. See data
grams and sockets
for examples on how to do this.
The DistributedSemaphore is a class whose instances will be
used by the clients (applications) that need to use distributed
semaphores. This class will provide the
following interface.
Remember that in all the above methods, the
distributed semaphore object simply sends a request to the server and waits for
the reply and returns the status as in the reply. Thus this class is simply a wrapper that hides all the
communication details from the user.
Using datagram sockets to connect to the already implemented server, you
should be able to use the distributed semaphores in programs running on
different machines, while the server runs on a specific machine whose address
and port number is fixed a priori.
How
to test the system?
In order to
test this distributed semaphore system, you will implement a solution of the
dining philosopher’s problem using distributed semaphores. Each philosopher will be an independent JAVA
program. These programs will run on
same or different machines, each invoked separately. They will use instances of the DistributedSemaphore object in
order to synchronize themselves.
What
to submit?
You should
submit the following things as a part of this assignment.
1.
The Java programs used to implement the
different aspects of this assignment.
2.
A file called README.txt where you give a
tutorial on how to compile and run your programs. This file should also contain the names, netids and cornellids of
all the individuals in the group.
How
will you be graded?
The following will play a crucial role in
your grades for this assignment.
1.
Correctness
of the JAVA programs written to implement distributed semaphores.
2.
Robustness
of the JAVA programs written.
3.
Clarity of the JAVA
programs (comments!!!).
4.
Ease of using the README to test your
programs and results.