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
Define waves as it applies to electromagnetic fields
julsineya [31]

Waves in the electric and magnetic fields are known as electromagnetic waves. You must first understand what a field is, which is just a technique of giving each square inch of space a numerical value. You may see that as a temperature field, for instance, when you look at the weather predictions and they mention the temperature in several locations. Every location on Earth has a unique temperature that can be quantified. Everywhere on Earth has its own wind velocity, which is another form of field. This field differs somewhat from the temperature field in that the wind velocity has both a direction and a magnitude, whereas the temperature just has a magnitude (how hot it is). A vector is a quantity that has both magnitude and direction, hence a field that contains vectors at every location is referred to as a vector field. Vector fields include the magnetic and electric fields. We may examine what would happen if we placed a charged particle at any given position in space. If the charged particle were to accelerate, we would state that the electric field there is the direction in which the particle is moving. In general, positively charged particles will move in the electric field's direction, whereas negatively charged particles will move in the opposite way. Because it is a vector field, the magnetic field exhibits comparable behavior. We discovered in the 19th century that the same interaction, electromagnetism, really produces both electric and magnetic fields. Like an electromagnet, a changing electric field will produce a magnetic field, and a changing magnetic field will induce an electric field (like in a generator). If your system is configured properly, you may have an electric field that fluctuates, which in turn produces a magnetic field, which in turn induces another electric field, which in turn generates another magnetic field, and so on indefinitely. At the speed of light, this oscillation between a strong magnetic field and strong electric field spreads out indefinitely. In reality, light is an electromagnetic wave—an oscillation in the electromagnetic fields. An electric or magnetic field may exist without a medium since they exist in a vacuum, which implies that waves in these fields don't require a medium like sound to flow through.

5 0
2 years ago
Showing or hiding records in a database is called “filtering.”<br> True<br> False
agasfer [191]

Answer:

TRUE

Explanation:

4 0
2 years ago
Read 2 more answers
Input Energy ---&gt; Output Energy
uranmaximum [27]

Answer:

motion ------> electrical. winds push the turbines which generate a magnetic fields which in turn, generates electricity

4 0
3 years ago
9. How is the Air Delivery temperature controlled during A/C operation?
Rzqust [24]

Answer:

i believe the answer is a but i could be wrong

Explanation:

i hope it helps

6 0
3 years ago
What should be your strongest tool be for gulding your ethical decisions making process
valkas [14]

Answer:

Recognize that there is a moral dilemma.

Determine the actor. ...

Gather the relevant facts. ...

Test for right versus wrong issues. ...

Test for right versus right paradigms. ...

Apply the resolution principles. ...

Investigate the trilemma options. ...

Make the decision.

7 0
2 years ago
Other questions:
  • A pitfall cited in Section 1.10 is expecting to improve the overall performance of a computer by improving only one aspect of th
    6·1 answer
  • What are the challenges posed by strategic information systems, and how should they be addressed?
    10·1 answer
  • Fatigue failure occurs under the condition of (a) High elastic stress (b) High corrosivity (c) High stress fluctuations (d) High
    9·1 answer
  • What are the two safety precautions taken before driving a car​
    12·1 answer
  • HELP PLEASE<br> this is for drivers ed btw
    5·1 answer
  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers.Call a method named FillArray to intera
    12·1 answer
  • An ice hockey player is skating on an ice rink. The rink has a coefficient of kinetic friction of roughly 0.1. If the normal for
    6·1 answer
  • What is the function of a regulator?
    8·1 answer
  • What are the important things to remember when arriving for an interview?
    15·1 answer
  • Draw the free-body diagram of the beam which supports the 80-kg load and is supported by the
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!