1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nikitich [7]
3 years ago
5

A single-lane bridge connects the two Vermont villages of North Tunbridge and South Tunbridge. Farmers in the two villages use t

his bridge to deliver their produce to the neighboring town. The bridge can become deadlocked of a northbound and a southbound farmer get on the bridge at the same time. (Vermont farmers are stubborn and are unable to back up.)a. Using exactly one semaphore, design an algorithm that prevents deadlock. Do not be
concerned about starvation and inefficency.

b. Provide a solution using Monitor that is starvation-free.

Engineering
1 answer:
Naddika [18.5K]3 years ago
4 0

Answer:

Check the explanation

Explanation:

Main1.java

import java.lang.InterruptedException;

import java.lang.Thread;

import java.util.Random;

public class Main {

final private static int FARMERS = 10;

public static void main(String[] args) {

Bridge bridge = new Bridge();

Random r = new Random();

System.out.println("Running with " + FARMERS + " farmers...");

// Enter a bunch of farmers from different directions.

for (int i = 0; i < FARMERS; i++) {

Farmer farmer;

if (r.nextBoolean()) {

farmer = new SouthBoundFarmer(bridge);

} else {

farmer = new NorthBoundFarmer(bridge);

}

cross(farmer);

}

}

private static void cross(Farmer f) {

new Thread(f).start();

}

}

SouthBoundFarmer.java

public class SouthBoundFarmer extends Farmer {

public SouthBoundFarmer(Bridge b) {

super(b);

this.name = "South";

}

}

Farmer.java

import java.lang.InterruptedException;

import java.util.Random;

public class Farmer implements Runnable {

private Bridge bridge;

private Random random;

protected String name;

public Farmer(Bridge b) {

this.bridge = b;

this.random = new Random();

}

public void crossBridge(Bridge bridge) {

System.out.println("[" + this.name + "] Waiting to enter bridge...");

try {

bridge.enter();

System.out.println("[" + this.name + "] Entering bridge...");

// Crossing bridge...some farmers are fast, others are slow :P

Thread.sleep(1000 + random.nextInt(9000));

System.out.println("[" + this.name + "] Leaving bridge...");

} catch (InterruptedException e) {

System.out.println("...Interrupted!");

} finally {

bridge.leave();

}

}

public void run() {

this.crossBridge(this.bridge);

}

}

Bridge.java

import java.lang.InterruptedException;

import java.util.concurrent.Semaphore;

public class Bridge {

private Semaphore lock;

public Bridge() {

this.lock = new Semaphore(1);

}

public void enter() throws InterruptedException {

this.lock.acquire();

}

public void leave() {

this.lock.release();

}

}

NorthBoundFarmer.java

public class NorthBoundFarmer extends Farmer {

public NorthBoundFarmer(Bridge b) {

super(b);

this.name = "North";

}

}

KINDLY CHECK THE OUTPUT BELOW :

You might be interested in
What type of footwear protects your toes from falling objects and being crushed?
Lady bird [3.3K]

Answer:Steel toe

Explanation:

6 0
4 years ago
Read 2 more answers
The biggest advantage of sketches is that
steposvetlana [31]

Answer:

They communicate ideas very quickly.

Explanation:

8 0
3 years ago
What does it take to launch a rocket in space?
Dafna11 [192]
To get rockets into orbit, they need much more thrust than the amount that will get them up to the required altitude. They also need sufficient thrust to allow them to travel with very high orbital speed. ... If speed is less than this, an object will fall back to the Earth
3 0
3 years ago
Read 2 more answers
A distribution center is used in which of the following applications?
FrozenT [24]

both b and c are the right

5 0
3 years ago
How are the accelerator and brake pedal positioned in relation to each other? A. The brake pedal is to the right of the accelera
Lunna [17]

Answer:B

Explanation:

7 0
4 years ago
Other questions:
  • 1. Copy the file Pay.java (see Code Listing 1.1) from the Student CD or as directed by your instructor. 2. Open the file in your
    10·1 answer
  • A large well-mixed tank of unknown volume, open to the atmosphere initially, contains pure water. The initial height of the solu
    12·1 answer
  • The underground storage of a gas station has leaked gasoline into the ground. Among the constituents of gasoline are benzene, wi
    12·2 answers
  • A 1 m wide continuous footing is designed to support an axial column load of 250 kN per meter of wall length. The footing is pla
    11·1 answer
  • A(n) _________ is a current greater than the equipment rated current or conductor ampacity, which is confined to the normal cond
    12·1 answer
  • List the three main methods employed in dimensional analysis
    6·1 answer
  • I need answers for this sheet please.
    15·1 answer
  • What should one do with a load if one is going to leave a jack under a load?
    11·1 answer
  • Chọn dữ liệu phù hợp và biểu diễn chúng dưới dạng biểu đồ, từ đó cho biết 3 yếu tố
    7·1 answer
  • How many and what type of<br> receptacles are connected to<br> this circuit?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!