Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMPUTER SCIENCE 131A

OPERATING SYSTEMS

PROGRAMMING ASSIGNMENT 4

CARS AND TUNNELS PREEMPTIVE PRIORITY SCHEDULER

Introduction

This assignment is a continuation of PA3, where you wrote non-preemptive priority scheduler for Cars and Sleds. Now we take the next logical step. We will be adding a preemptive priority scheduler that can also handle a new Vehicle type: Ambulances.

Purpose

It is important that you continue to build your understanding and ability designing, implementing, and especially debugging concurrent algorithms. These may involve synchronized methods, monitors, locks, condition variables and more (note: I am not implying that your solution will or must use all or only those tools!) You will learn to write safe concurrent code without sacrificing performance In PA3 I am sure you found that debugging such a program can be hard. You might have come across deadlocks, timeouts, and “heisenbugs” .

In addition, while you are not implementing an actual operating system, these programming assignments are designed to expose you to the thought processes of an operating system designer and implementer. For example, we see the powerful idea of multiple scheduling policies, factored out of the scheduler. This approach is useful and realistic. It allows the administrator of the operating system to experiment with different policies without having to write any code.

Description of the code and key classes

This assignment builds directly on PA3 so the code should be familiar to you! Use your submissions for PA3 as a basis for your submission to this assignment. Here is a tour through some of the important differences and additions.

Ambulance

The Ambulance is a Vehicle that is guaranteed to have the highest priority of any non- Ambulance Vehicles. It is provided in the starter code.

PreemptiveTunnel

The PreemptiveTunnel class will contain logic governing a Vehicle admittance policy that includes Ambulances.

At all times, the PreemptiveTunnels state must satisfy the same policy as the BasicTunnel with respect to Cars, Sleds, direction, capacity and so on. You can refer to the last assignment to refresh your memory.

But now, it must also handle Ambulances according to the following rules:

-    An Ambulance can always enter a PreemptiveTunnel unless there is another Ambulance already inside. That is the only condition. In other words, it doesn’t matter what other vehicles or directions are already there.

-    When an Ambulance enters a PreemptiveTunnel all other Vehicles in the   PreemptiveTunnel must stop and wait for the Ambulance to exit the Tunnel before continuing.

-    Any Vehicle entering a PreemptiveTunnel when an Ambulance is currently inside should enter then pull over immediately.

When a Vehicle is pulled over part-way through the PreemptiveTunnel, it should continue driving from where they left off.

For example, consider a Vehicle that requires 100 ms to travel through the Tunnel. Suppose it sleeps 70 ms (milliseconds) before being preempted by an Ambulance. After the Ambulance leaves the Tunnel, it should only sleep for another 30 ms.

Use Java’s System.currentTimeMillis () method to implement this behavior.

PreemptivePriorityScheduler

This class works hand in hand with the PreemptiveTunnel to achieve the required behavior and functionality.

Vehicle

You will adapt the vehicle class to operate in the new environment where they have to give deference to Ambulances. Please first read the run method of the Vehicle class. In the given implementation, the method simulating Vehicle behavior in a TunneldoWhileInTunnel () simply has the Vehicle thread sleep for some amount of time dependent on the speed field.

To implement the concept of pulling over Vehicles in the PreemptiveTunnel when an Ambulance enters, modifications need to be made to the Vehicle’s doWhileInTunnel method.

No busy waiting

You may use all the multithreading concepts you have learned thus far in Java to solve this problem, but you probably will use monitors to avoid busy waiting while an ambulance is in a tunnel. As a reminder, monitors in Java correspond to the synchronized keyword on methods, and the wait(), signal() and signalAll() methods. You may also add any additional fields or methods to the Vehicle class as you see fit. You should not change the way log entries are entered in the Log.

What to do

At the highest level, you will modify the code to implement the correct behavior of Ambulances in a PreemptiveTunnel using the PreemptivePriorityScheduler. More generally, you may have to modify the files in the submission package, and no others.

Below is a guide and summary with some reminders and pointers.

It is not a comprehensive list of every little thing you will need to do to meet the requirements of the assignment.

•   Complete the implementation of the PreemptiveTunnel class and its methods. The PreemptiveTunnel uses the new PreemptivePriorityScheduler.

•   Implement the PreemptivePriorityScheduler and its methods, focusing especially on its admittance policy.

●   Ensure that Ambulances properly cause all other Tunnel occupants to cease driving till it leaves. This includes the case of when a Vehicle enters a Tunnel occupied by an Ambulance.

●   Vehicles should properly resume driving after an Ambulance leaves.

●   Ambulances should not wait unnecessarily for an opening.

●   Properly achieve mutual exclusion using Java synchronization mechanisms. This includes controlling access to shared data structures.

●   Make sure you run all the test and they are all successful. Note that passing all the tests is necessary but not sufficient to receive a high score. You must meet all the other requirements laid out in this document.

Grading

There are no hidden unit tests that will be used for grading this assignment. To make sure you do not have race conditions, set PrioritySchedulerTest.NUM_RUNS = 10.

Passing all of the test cases does not mean you will get a high score.

Make sure you import and export the Eclipse project properly. The submitted project should have a structure that looks exactly like the skeleton. An improperly structured project will    receive a 0.

Do not modify any files outside of the submission package. Failing to follow this instruction will result in a 0.

The project needs to be renamed FirstnameLastnamePA4 (make sure to use Eclipse’s Refactor > Rename). The zip file you submit should be named FirstnameLastnamePA4.zip.

Code Commenting

These are your guidelines for commenting your code:

1.   Only add Javadoc comments to methods you defined or modified. For example, if you used created an additional method in a class, or if you modified an existing method, you should add or correct the Javadoc.

2.   The PAs in this class give you a lot of flexibility and design options. People can have different data structures, completely different implementations of a method, etc. So for  the code you write or structures you defined, please thoroughly comment.  Otherwise, if you did not change the code, you do not need write new comments.

3.   For the classes you modified, please also replace the header comment with your header comment.

Allowed Libraries

The only additional classes related to concurrency and synchronization you can use are Condition and Lock/ReentrantLock which can be found in

java.util.concurrent .locks. You are not allowed to use synchronized data structures such as LinkedBlockingQueue.

Remarks

This is an individual assignment. Any sign of collaboration will result in a 0 score for the assignment for both the giver and receiver of copied code. Please check the academic integrity policies in the syllabus of the course.

Late policy: Please check the syllabus for the late submission policies of the course.