Java代写|CISC324: Operating Systems Lab 3



This lab may be your first time programming exercise in which you will be writing concurrent programs that use semaphores in Java. Java provides you with a package java.util.concurrent.semaphore, in order to use semaphore operations. You will learn how to create and initiate semaphores and use them in both styles: process execution ordering (waiting style) or for critical section (mutual exclusion style). You will also learn how to use the primitives acquire() and release() that have been discussed in the lecture.

This lab has been prepared to be conducted on any operating system that runs the Java Virtual Machine. Please make sure to have a well set up environment. You need a computer, an operating system, a Java Virtual Machine, and optionally a Java IDE (e.g., Eclipse).

Background and Outcomes

In this lab, you are asked to understand and execute the Java code which is provided to you. This Java code is an implementation for the readers and writers classical synchronization problem with readers having priority. The readers and writers are basically implemented by their respective threads. In the readers and writers problem, a shared data structure, usually a file or a database, is accessed by reader threads and writer threads. For efficiency, many readers should be allowed to read simultaneously. For correctness, only one writer is allowed to write into the data structure at a time, all other threads(readers and writers) must wait. There exists three possible solutions. Each solution defines a kind of scheduling policy to decide which thread should first have access to the data structure as described below: Main Java class that creates reader and writer threads. Reader Java class that defines the reader thread. Writer Java class that defines the writer thread. This Java class declares the semaphores (mutex and wrt) and counter (readcount) used to synchronize readers and writers. This Java class defines a “doSleep” method for suspending a thread.

1 Reader-Writers Problem (Writers Have Priority)

Modify and adapt the given Java code that implements the Readers have priority solution so that it becomes a Java code that implements the writer have priority.

Below are some directions that express the requirements of the new solution.


Here are the synchronization requirements for “giving writers priority”:


2 What to submit

324-1234-Lab3 where 1234 stands for the last 4 digits of your student ID,e.g.: For student with ID 20196072, the folder should be named 324-6072-Lab3.

3 What to check during submission