Comp346 PA2 w2024
Comp346 PA2 w2024
Comp346 PA2 w2024
Operating Systems
Programming assignment 2
______________________________________________________________________
Warning: Redistribution or publication of this document or its text, by any means,
is strictly prohibited. Additionally, publishing the solution publicly, at any point of
time, will result in an immediate filing of an academic misconduct.
______________________________________________________________________
Deadline: Monday March 11, 2024
Late Submission: No late submission.
Teams: The assignment can be done individually or in teams of 2
Purpose: The purpose of this assignment is to apply in practice the
synchronization features of the Java programming language.
• Specification:
In the first programming assignment, you have implemented the threads that
allowed the operations of the client application and the server application to run
concurrently. However, there were no critical section problem because only one server
thread was updating the accounts and there was only one transaction on each account.
In this programming assignment, the transaction file has been modified so that multiple
transactions can be done on a single account. Therefore, if a thread blocks in a critical
section while updating an account balance, then the result could be inconsistent if
another thread also attempts another update operation on that account.
• Problem:
The Java code provided is similar to that of PA1 but there have been some changes
to adapt it to the requirements of PA2 as shown below. For this assignment, the server
will use two concurrent threads to update the accounts and thus we may have
inconsistent results in case the critical section is not well protected. In addition, the
synchronization of the network buffers (i.e. inComingPacket, outGoingPacket) is using
busy-waiting so you need now to block a thread when a buffer is full or empty.
• Changes in PA1:
o Use of static methods in class Network in order to call the methods using
the class Name (i.e. Network) instead of using the instance variable
• Implementation:
This problem will be implemented in two phases, phase (i) will synchronize the
access to the critical section for updating the accounts and phase (ii) will coordinate
the threads when accessing a full or an empty network buffer.
Phase (i):
o First, you must adapt the Java code provided to your solution of
PA1. In case your PA1 code doesn’t work properly, you will be
provided help in the lab.
o Implement a second server thread in the main() method by
respecting the changes already made in the constructor of the Server
class. Consequently you need to modify the run() method of the
Server class to accommodate the two threads and also to display the
running time of each thread. The server can disconnect only when
both threads have terminated.
o Now execute the program with DEBUG flags and notice the
accounts with inconsistent results as shown in the file os-pa2-
outputunsynchronized.txt. The accounts 60520, 22310 and 91715
should be inconsistent but that may sometimes change depending on
the sequence of execution. I have forced inconsistency by sleeping
for 100 ms a thread accessing the critical section in the deposit()
method of the Server class.
o Next, using synchronized methods or synchronized statements,
protect properly the critical section of the methods deposit(),
withdraw() and query(). Execute the program again and there should
be no inconsistent results. Explain your choice of using either
synchronized methods or synchronized statements.
• Evaluation:
You will be evaluated mostly on the implementation of the required methods and the
use of the synchronization tools.
Evaluation criteria
Criteria Marks
Implementation of the server threads in the main method. 5%
Implementation of the third server thread in Phase (ii) and comments 10%
about the running time.
• Submission:
o Create one .zip file, containing the necessary files (.java, .txt and test cases). If
the assignment is done individually, your file should be called pa2_studentID,
where pa2 is the number of the assignment and studentID is your student ID
number. If the work is done in a team of 2 people, the zip file should be called
pa2_studentID1_studentID2 where studentID1 and studentID2 are the student
ID numbers of each student.
o Upload your .zip file on moodle (or on your section web page) as Programming
Assignment 2 before midnight on the due date.
A demo will take place with the markers afterwards. Markers will inform you about the
details of demo time and how to book a time slot for your demo. If working in a group,
both members must be present during demo time. Different marks may be assigned to
teammates based on this demo.
If you fail to demo, a zero mark is assigned regardless of your submission.
If you book a demo time, and do not show up, for whatever reason, you will be
allowed to reschedule a second demo but a penalty of 50% will be applied.
Failing to demo at the second appointment will result in zero marks and no more
chances will be given under any conditions.
• Notice:
o Note that this code has been tested on Windows using Eclipse. You may need
to make changes if you would like to run on other OS.
o You must not modify the original Java code provided nor change the size of the
arrays but simply implement the required elements.