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
HEY Y'ALL!!! WILL GIVE BRAINLEST AND THANKS+A LOT OF POINTS!!!!! So in class our teacher wants to help us think out of the box m
Elden [556K]

Answer:

2) phone

3) snake

Explanation:

NO EXPLANATION

5 0
3 years ago
Read 2 more answers
List three main commodity areas that drive agriculture
Alborosie

Common agricultural commodities include dairy products, wheat, and coffee. You can invest in and “trade” these products virtually, without running a farm and purchasing and storing the items yourself.

3 0
1 year ago
A ladder logic program and the associated physical input/output components are given below. Lighting changes from full darkness
Katena32 [7]

Answer:

O2 is true.

Explanation:

8 0
3 years ago
Why do side airbags say inflated for several seconds
Pavlova-9 [17]

Answer:

When the airbag is activated, the chemicals produce heated nitrogen gas, which inflates the nylon bag....

hope my answer help u!!!!

7 0
4 years ago
Read 2 more answers
The ________ is considered the normal operating control of an air conditioner, according to the Standards of Practice. thermosta
mamaluj [8]

Answer:

Thermostat

Explanation:

The thermostat is considered the normal operating control of an air conditioner, according to the Standards of Practice.

8 0
2 years ago
Other questions:
  • Some characteristics of clay products such as (a) density, (b) firing distortion, (c) strength, (d) corrosion resistance, and (e
    15·1 answer
  • Write an application that solicits and inputs three integers from the user and then displays the sum, average, product, smallest
    6·1 answer
  • Determine the average and rms values for the function, y(t)=25+10sino it over the time periods (a) 0 to 0.1 sec and (b) 0 to 1/3
    9·1 answer
  • Given the circuit at the right in which the following values are used: R1 = 20 kΩ, R2 = 12 kΩ, C = 10 µ F, and ε = 25 V. You clo
    11·1 answer
  • What is the the force available at the roadway surface to perform work?
    6·1 answer
  • A pipe of inner radius R1, outer radius R2 and constant thermal conductivity K is maintained at an inner temperature T1 and oute
    14·1 answer
  • A 0.01 significance level is used for a hypothesis test of the claim that when parents use a particular method of gender? select
    13·1 answer
  • A detailed image of a brain scan with height, width, and depth is an example of a(n) 3D _________ model.
    15·1 answer
  • Some extremely hazardous materials used in welding operations include
    15·1 answer
  • a ball is subject to two forces F1 and F2. The magnitudes of the two forces are 45.0 N and 70.0 N respectively. In the figure be
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!