Page 1 of 1

Task: Write a Java program to solve the following concurrency problem in a synchronized and deadlock-free manner. Highwa

Posted: Fri Jul 08, 2022 7:27 am
by answerhappygod
Task: Write a Java program to solve the following concurrencyproblem in a synchronized and deadlock-free manner.
Highway Gbit is a bidirectional road that runs from East-West ofCybertown. This summer the city transportation department startedrepairing one side of a major part of the road, and made thatportion a one-way traffic that can switch direction. When therepair work is going on, traffic controller units are controllingthe flow of the traffic. Traffic is allowed both direction throughthe “repair zone” but not simultaneously. When the West-boundtraffic are allowed through the repair zone, the East-bound trafficis blocked. When the East-bound traffic is allowed through repairzone then the West-bound traffic is blocked. Duration and timing toallow Eastbound or West-bound is completely random (at thediscretion of the controller!!).
To model the scenario, we will number the vehicles from bothsides as follows: the West-bound vehicles are numbers 0, 2, 4, 6,…. (even numbers) and the East-bound vehicles are numbered 1, 3, 5,7, … (odd numbers). If the synchronization does not happen properly(that is, if the traffic controllers do not coordinate properly)then the “repair zone” become deadlocked. That is, vehicles fromtwo opposite directions must not be allowed through the repairzone.
Task: Write a program that synchronizes movement of vehiclesthrough the repair zone in such a way that prevents thedeadlock.
You may use any synchronization technique. For example,semaphores would be a good choice. You can assume that there willbe steady stream of vehicles from each side, however, there can bemore than one vehicle from one side before we see a vehicle fromthe opposite side. The controllers allow more than one vehiclepassing through the repair zone, but only in one direction.Vehicles do not need to have same speed (the time they spend topass the repair zone). You can also assume, once a particularvehicle enters and leaves the repair zone, it does not come backagain to enter the repair zone. That is, the vehicle numbers do notrepeat!
User is not expected to supply any input. The user will simplycompile and run your program. A segment from a sample output couldbe like the following:

East-bound vehicle 1 wants enter the repair zone.
East-bound vehicle 1 is in the repair zone.
East-bound vehicle 1 exits the repair zone.
West-bound vehicle 4 wants to enter the repair zone.
West-bound vehicle 4 is in the repair zone.
West-bound vehicle 4 exits the repair zone.
East-bound vehicle 5 wants to enter the repair zone.
East-bound vehicle 5 is in the repair zone.
West-bound vehicle 8 wants to enter the repair zone. // note:Vehicle 8 cannot enter and will be blocked as vehicle 5 is alreadyin repair zone
East-bound vehicle 5 exits the repair zone.
West-bound vehicle 8 is in the repair zone. // note: vehicle 8can go as vehicle 5 has left the repair zone
East-bound vehicle 7 wants to enter the repair zone. // note:Vehicle 7 cannot enter as Vehicle 8 is already in repair zone
West-bound vehicle 10 wants to enter the repair zone. // note:the repair zone may have allowed 10 to enter
West-bound vehicle 10 is in the repair zone. // note: vehicle 10is also in the repair zone when 8 is still there
West-bound vehicle 8 exits the repair zone.
West-bound vehicle 12 wants to enter the repair zone.

[Hint: you may create two threads for controlling West-bound andEast-bound vehicles. You may also use Thread.sleep() with different(or, random) sleep times for West-bound and East-bound vehicleswhen they enter repair zone. This would allow simulating differenttime taken by the vehicles to pass the repair zone. You may alsouse synchronized controller method from Java concurrency packageappropriately.]
CAUTION/Requirements:
1. Two vehicles from opposite sides cannot be in the repair zoneat the same time. So, if “East-bound vehicle x in the repair zone”appears in the output (‘x’ = odd number), there must be a“East-bound vehicle x exits the repair zone” in the output beforeany “West-bound vehicle y in the repair zone” appears in the output(‘y’ = even number).
2. If ‘vehicle m’ and ‘vehicle n’ are waiting to enter repairzone from same direction, the vehicle which is first will enter therepair zone first. That is, a vehicle cannot overtake frombehind.
3. The flow should not be such that vehicles from only onedirection is keep entering the repair zone. If that happensaccidentally (JVM’s preference of one particular thread in a run),it is okay; but the flow should not be controlled that way throughyour code. That is, your code should not force to allow allvehicles from one direction first, followed by all vehicles fromother direction. In fact, the user can run the program multipletimes, and this unintended serialization cannot happen all thetime! If that happens in every run, then that indicates the codemust have forced it.
4. There should be steady stream of vehicles from both sides.That is, the program should continue until the user presses CTRL-Cin the shell to stop the program. There should not be a specificnumber of vehicles.
5. (preferred) There should be some delay (may be half a secondor, a second) between each line of the output so that user canfollow the display flow.