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
If there’s an unidentified puddle under your car, it means ______.
algol13
Someone dropped a gallon of milk and you parked over it
6 0
4 years ago
A search will start from a visual lead<br> true<br> false
BlackZzzverrR [31]
True

An organized searching process will need to start from the visual lead area. Eye focus and eye movements from the path of travel in an organized pattern describes a visual search process.
4 0
2 years ago
Read 2 more answers
What is the first step in the problem-solving process, as well as in the engineering design process?
DENIUS [597]

Answer:

C. Brainstorm

Explanation:

<em>Brainstorming refers to thinking and developing ideas and solutions to problems</em>. It involves a free-thinking approach and putting oneself in the context of the problem you are attempting to solve. You can use the empathy maps to combine ideas from interviewing other people on the same. This stage is followed by defining the problem, concept generation, developing a solution, constructing a testing a prototype, evaluating the solution and finally presenting the solution.

Answer choice A is incorrect because it is the last stage .

Answer choice B is incorrect because it is the second stage.

Answer choice D is incorrect because it is step 5 and 6.

5 0
4 years ago
A 75-hp (shaft output) motor that has an efficiency of 91.0 percent is worn out and is replaced by a high-efficiency 75-hp motor
Naya [18.7K]

Answer:

4.536hp

Explanation:

The decrease in the heat gain of the room is determined from difference in electrical inputs:

Q = W_{shaft} (\frac{1}{n_{1} } - \frac{1}{n_{2} })\\Q = (75hp)*(\frac{1}{0.91 } - \frac{1}{0.963 })\\\\Q = 4.536 hp

8 0
3 years ago
The flow rate of liquid metal into the downsprue of a mold = 0.7 L/sec. The cross-sectional area at the top of the sprue = 750 m
katrin [286]

Answer:

367.43 mm²

Explanation:

Given:

Flow rate, Q = 0.7 L/s

1000 L = 1 m³ = 10⁹ mm³

thus,

1 L = 10⁶ mm³

Therefore,

Q = 0.7 × 10⁶ mm³/s

Cross-sectional area at the top of the sprue = 750 mm²

Length of the sprue = 185 mm

Now,

Velocity = \sqrt{2gh}

where,  g is the acceleration due to gravity = 9.81 m/s²

h is the height through which flow is taking place = 185 mm = 0.185

thus,

Velocity = \sqrt{2\times9.81\times0.185}

or

velocity = 1.9051 m/s = 1905.1 mm/s

Also,

Q = Area × Velocity

thus,

0.7 × 10⁶ = Area × 1905.1

or

Area = 367.43 mm²

3 0
3 years ago
Other questions:
  • A wide-sense stationary process X(t) is the input to a linear system whose impulse response is h(t)=2e^(-7t), t≥0.
    11·1 answer
  • An oil pump is drawing 44 kW of electric power while pumping oil with rho = 860 kg/m^3 at a rate of 0.07 m^3/s. The inlet and ou
    8·1 answer
  • The energy flux associated with solar radiation incident on the outer surface of the earth’s atmosphere has been accurately meas
    11·1 answer
  • Can someone please do this for me ive been sick for the past week and am behind
    6·1 answer
  • List six possible valve defects that should be included in the inspection of a used valve?
    7·1 answer
  • PLZ HELP ASAP!!!
    6·1 answer
  • It is desired to produce and aligned carbon fiber-epoxy matrix composite having a longitudinal tensile strength of 630 MPa. Calc
    6·1 answer
  • 1- The preexponential and activation energy for the diffusion of iron in cobalt are 1.1×10-5 m 2 /s and 253,300 J/mol, respectiv
    8·1 answer
  • Which stages occur during incomplete metamorphosis?
    12·1 answer
  • Why does the us import oil? because it does not produce any oil at all because it needs higher quality oil than it produces beca
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!