Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

Assignment: Operating Systems

Write a Java program to solve the following synchronization & deadlock problem.

Two mountain resorts are connected by a road that becomes single lane inside a tunnel that goes through the mountain. One resort is at the left end of the tunnel and the other at the right end of the tunnel. Cars moving from left to right are called "Right-bound" cars and are indicated with odd numbers starting from 1 (like, 1, 3, 5, ...). "Left-bound" cars move from right to left and are indicated with even numbers starting from 0 (like 0, 2, 4, ..). The tunnel can become deadlocked if a left-bound and a right-bound car enter into the tunnel at the same time.

Task: Write a program that prevents the deadlock. You may use semaphores. You should also take care of the starvation situation in which left-bound cars prevent right-bound cars to enter into the tunnel indefinitely, or vice versa. There will be steady stream of cars from each side, however, there can be more than one car from one side before we see a car from the opposite side. The tunnel may allow more than one car (but not infinite number of cars) passing through it in one direction.

Execution: User is not expected to supply any input. The user will simply compile and run your program. A segment from a sample output could be like the following:
...
Right-bound Car 1 wants to enter the tunnel.
Right-bound Car 1 is in the tunnel.
Right-bound Car 1 is exiting the tunnel.
Left-bound Car 4 wants to enter the tunnel.
Left-bound Car 4 is in the tunnel.
Left-bound Car 4 is exiting the tunnel.
Right-bound Car 5 wants to enter the tunnel.
Right-bound Car 5 is in the tunnel.
Left-bound Car 8 wants to enter the tunnel. // note: Car 8 cannot enter as Car 5 is already in tunnel
Right-bound Car 5 is exiting the tunnel.
Left-bound Car 8 is in the tunnel. // note: now car 8 can go as car 5 has left the tunnel
Right-bound Car 7 wants to enter the tunnel. // note: Car 7 cannot enter as Car 8 is already in tunnel
Left-bound Car 10 wants to enter the tunnel. // note: the tunnel may have allowed 10 to enter Left-bound Car 8 is exiting the tunnel.
Left-bound Car 10 is in the tunnel. // note: this could have been before the previous statement
Left-bound Car 12 wants to enter the tunnel.
...

CAUTION: two cars from opposite sides cannot be in the tunnel at the same time. So, if "Right-bound car x in the tunnel" appears in the output with ‘x' being odd, there must be a "Right-bound car x exiting the tunnel" in the output before a "Left-bound car y in the tunnel" appears in the output with ‘y' being even. Another caution: if ‘car m' and ‘car n' are waiting to enter tunnel from same direction, the car which is first will enter the tunnel first. Also, once a particular car enters and then leaves the tunnel, it does not come back again to enter the tunnel.

[Hint: you may create two threads for controlling left-bound and right-bound cars. You may also use Thread.sleep() with different sleep times for left-bound and right-bound cars when they enter tunnel. You may also use synchronized tunnel controller method and use wait() and notify()/notifyAll() appropriately.]

If two cars are waiting from two directions, JVM may pick up one randomly to allow entering into the tunnel.

Delay should be used appropriately to observe the progress of the output in a run of the program.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92776976

Have any Question?


Related Questions in Computer Engineering

Section 26 in the smith textbook elementary information

Section 2.6 in the Smith textbook ( elementary information security 2nd edition) offers a list of 6 high-level security controls. Pick two of them and describe how you personally experience those controls in use on perso ...

Letang industrial systems company lisc is trying to decide

Letang Industrial Systems Company (LISC) is trying to decide between two different conveyor belt systems. System A costs $300,000, has a four-year life, and requires $101,000 in pretax annual operating costs. System B co ...

Structureswrite the program in c onlytask create a seating

Structures Write the Program in C++ only. Task: Create a seating reservation program for Podunk Airlines. The air fleet consists of a single plane with a seating capacity of 12. The plane makes one flight daily. Your pro ...

Strong passwords are at least 8 characters in length

Strong passwords are at least 8 characters in length, contain both upper- and lower-case letters, as well as some digits and special characters. Use the requirements (specified below) for a password to define in BNF a gr ...

Please help with anbspfunctionnbspcodesymbol to convert

Please help with a function  codeSymbol , to convert each mark to a symbol (A, B, C, D, E, F) and a code (7,6,5,4,3,2,1) according to the table below. And call it in the main function. Use the table below to determine th ...

A new machine averages 4 clock cycles per instruction and

A new machine averages 4 clock cycles per instruction, and runs at a system clock of 20 MHz. The Axiom-Verge algorithm set to benchmark the system will take an even 3000 instructions to complete. a) Knowing how many cloc ...

Refer to the reading e-business strategy how to benefit

Refer to the reading, "E-Business Strategy: How to Benefit From a Hype" and review its alignment between such models as SWOT and Five Forces and the e-business that it uses as a model. In your posting, address the follow ...

Task you want to calculate a students gpa for a number of

Task: You want to calculate a student's GPA for a number of classes taken by the student during a single semester. (In C# (C sharp)) Inputs: 1. the student's name 2. Class names for the classes taken by the student 3. Cl ...

Short answer1 what must be installed or prepared to utilize

Short answer: 1. What must be installed or prepared to utilize Windows Remote Management (WinRM) on a Windows Server 2012 R2? 2. What is the PowerShell command to check the status of Windows Remote Management (WinRM)? 3. ...

Consider a valleyed array a1 2 middot middot middot n with

Consider a valleyed array A[1, 2, · · · , n] with the property that the subarray A[1..i] has the property that A[j] > A[j + 1] for 1 ≤ j (a) What is a recursive algorithm that takes asymptotically sub-linear time to find ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As