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
A 400-m^3 storage tank is being constructed to hold LNG, liquefied natural gas, which may be assumed to be essentially pure meth
GuDViN [60]

Answer:

mass of LNG: 129501.3388 kg

quality: 0.005048662

Explanation:

Volume occupied by liquid:

400 m^3*0.9 = 360 m^3

Volume occupied by vapor

400 m^3*0.1 = 40 m^3  

A figured with thermodynamic properties of saturated methane is attached. Notice that a liquid-gas mixture is present

For liquid phase specific volume (vf) at 150 K is 0.002794 m^3/kg and for vapor phase specific volume (vg) is 0.06118 m^3/kg

From specific volume definition:

vf = liquid volume/liquid mass

liquid mass = liquid volume/vf

liquid mass = 360 m^3/0.002794 m^3/kg

liquid mass = 128847.5304 kg

vg = vapor volume/vapor mass

vapor mass = liquid volume/vg

vapor mass = 40 m^3/0.06118 m^3/kg

vapor mass = 653.8084341 kg

total mass = 128847.5304 kg + 653.8084341 kg = 129501.3388 kg

Quality is defined as the ratio between vapor mass and total mass

quality =  653.8084341 kg/129501.3388 kg = 0.005048662

4 0
3 years ago
From the following numbered list of characteristics, decide which pertain to (a) precipitation hardening, and which are displaye
Blababa [14]

Answer:

(a) Precipitation hardening

(1) The strengthening mechanism involves the hindering of dislocation motion by precipitates/particles.

(2) The hardening/strengthening effect is not retained at elevated temperatures for this process.

(4) The strength is developed by a heat treatment.  

(b) Dispersion strengthening

(1) The strengthening mechanism involves the hindering of dislocation motion by precipitates/particles.  

(3) The hardening/strengthening effect is retained at elevated temperatures for this process.

(5) The strength is developed without a heat treatment.  

7 0
3 years ago
Which is not required when working in a manufacturing facility?
Artyom0805 [142]
Flip flops are not required
5 0
3 years ago
A geothermal heat pump absorbs 15 KJ/s of heat from the Earth 15 m below a house. This heat pump uses a 7.45 kJ/s compressor.
Anna007 [38]

Answer:

COP of the heat pump is 3.013

OP of the cycle is  1.124

Explanation:

W = Q₂ - Q₁

Given

a)

Q₂ = Q₁ + W

     = 15 + 7.45

     = 22.45 kw

COP = Q₂ / W = 22.45 / 7.45 = 3.013

b)

Q₂ = 15 x 1.055 = 15.825 kw

therefore,

Q₁ = Q₂ - W

Q₁ = 15.825 - 7.45 = 8.375

∴ COP = Q₁ / W = 8.375 / 7.45 = 1.124

4 0
4 years ago
Read 2 more answers
5. Name two health problems that fume can cause?<br> a)....<br> b)......
Vlad [161]

Answer:

A) Cancer of the Lungs

B)Larynx and Urinary Tract, as well as nervous system and kidney damage

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • An engineer is working with archeologists to create a realistic Roman village in a museum. The plan for a balance in a marketpla
    9·1 answer
  • Harmony in music is characterized by _____.
    14·2 answers
  • Your coworker was impressed with the efficiency you showed in the previous problem and would like to apply your methods to a pro
    5·1 answer
  • Which of the following is true Select one: a. HTML stands for Hyper Text Markup Language is a language for describing web pages
    6·1 answer
  • : A cyclical load of 1500 lb is to be exerted at the end of a 10 in. long aluminium beam (see Figure below). The bar must surviv
    6·1 answer
  • Convert 103.69 kN to TN.
    14·1 answer
  • Determine the wattmeter reading when it is connected to resistor load.​
    11·1 answer
  • The forklift exiting an aisle in a warehouse has the right of way?
    15·1 answer
  • Algorithm for sum 2+4+6+….+n
    11·1 answer
  • A fully charged new battery will have a low conductance reading.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!