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
3.
Andreyy89

Answer:

7

Explanation:

5 + 2 = 7

4 0
3 years ago
if you had 100 B size sheets and you cut them into A size sheets, how many sheets of A size paper would you have
castortr0y [4]

Answer:

200

Explanation:

A size sheets (also known as letter size) are 8.5 inches by 11 inches.

B size sheets (also known as ledger size) are 11 inches by 17 inches.

One B size sheet is twice as large as a A size sheet.  So if you have 100 B size sheets and cut each one in half, you'll get 200 A size sheets.

8 0
3 years ago
What is the measurement below?
Bess [88]

Explanation:

इसिसिसिसैस्स्स्स्स्स्स्स्स्स्सूस्सोस्स्स्स्स्स

8 0
3 years ago
Need help with both giving out brainlest for the people to help me
sammy [17]
The first one is d or the 4th answer choice and the second one is false. Hope this helps!
4 0
3 years ago
How are project deliverables determined?
Greeley [361]

Answer:

The essence including its problem is listed throughout the clarification section following.

Explanation:

Projects build deliverable that seem to be the products of the venture or indeed the implementation of the project. This ensures that perhaps the agile methodology may be as broad as either the goal of the study itself as well as the coverage that would be part of a much larger venture.

For every other production to have been marked as "deliverable" within the same project, this should satisfy a few eligibility requirements:

  • It should be within the development of the work.
  • The interested parties-external or internal-must consent to the above. This is perhaps the product of hard effort.

So that the above seems to be the right answer.

7 0
3 years ago
Read 2 more answers
Other questions:
  • A rod is 2m long at temperature of 10oC. Find the expansion of the rod, when the temperature is raised to 80oC. If this expansio
    7·1 answer
  • Some engineers have developed a device that provides lighting to rural areas with no access to grid electricity. The device is i
    13·1 answer
  • A vernier caliper will measure in what ( URGENT)
    10·1 answer
  • In a photonic material, signal transmission occurs by which of the following? a)- Electrons b)- Photons
    9·1 answer
  • . A piston-cylinder device whose piston is resting on top of a set of stops initially contains 0.5 kg of helium gas at 100 kPa a
    14·1 answer
  • An ideal reheat Rankine cycle with water as the working fluid operates the boiler at 15,000 kPa, the reheater at 2000 kPa, and t
    13·1 answer
  • What is the maximum value of the bending stress at the critical cross-section?
    14·1 answer
  • If i build thing a and thing a builds thing b did i build thing b
    5·2 answers
  • I really need help with my last topic,Hazard communication,if anyone can help me as soon as possible,that could be my Christmas
    12·1 answer
  • A) Your friend is faced with a situation i n whi ch t here is a difficulty in
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!