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
The Ethernet (CSMA/CD) alternates between contention intervals and successful transmissions. Assume a 100 Mbps Ethernet over 1 k
Vesnalui [34]
<h3><u>CSMA/CD Protocol: </u></h3>

Carrier sensing can transmit the data at anytime only the condition is before sending the data sense carrier if the carrier is free then send the data.

But the problem is the standing at one end of channel, we can’t send the entire carrier. Because of this 2 stations can transmit the data (use the channel) at the same time resulting in collisions.

There are no acknowledgement to detect collisions, It's stations responsibility to detect whether its data is falling into collisions or not.

<u>Example: </u>

T_{P}=1 H r, at time t = 10.00 AM, A starts, 10:59:59 AM B starts at time 11:00 AM collision starts.

12:00 AM A will see collisions

Pocket Size to detect the collision.

\begin{aligned}&T_{t} \geq 2 T_{P}\\&\frac{L}{B} \geq 2 T_{P}\\&L \geq 2 \times T_{P} \times B\end{aligned}

CSMA/CD is widely used in Ethernet.

<u>Efficiency of CSMA/CD:</u>

  • In the previous example we have seen that in worst case 2 T_{P} time require to detect a collision.
  • There could be many collisions may happen before a successful completion of transmission of a packet.

We are given number of collisions (contentions slots)=4.

\text { Propagation day }=\frac{\text {distance}}{\text {speed}}

Distance = 1km = 1000m

\begin{aligned}&\text { Speed }=2 \times 10^{8} \mathrm{m} / \mathrm{sec}\\ &T_{P}=\frac{1000}{2 \times 10^{8}}=(0.5) \times 10^{-5}=5 \times 10^{-6}\\ &T_{t}=5 \mu \mathrm{sec}\end{aligned}

7 0
3 years ago
This search compares the target value with the middle element in the collection, then ignores the half of the collection in whic
love history [14]

Answer:

d) Binary

Explanation:

Binary search is a search algorithm that finds the position of a value in an ordered array. It compares the value with the element in the middle of the array, if they are not equal, half in which the value cannot be eliminated and the search continues in the remaining half until the value is found.

7 0
2 years ago
3) Explain how dc machines Can work as motor and generator​
weeeeeb [17]

The working principle of a DC machine is when electric current flows through a coil within a magnetic field, and then the magnetic force generates a torque that rotates the dc motor. The DC machines are classified into two types such as DC generator as well as DC motor.

5 0
1 year ago
________ are written to “maximize” or “minimize” a specific value associated with the product needs in order to define the goal
Anna35 [415]

Answer:

Objective statements.

Explanation:

An objective statement can be defined as a short statement that explicitly states or describes what a person wants exactly or is looking out for in a particular item.

Objective statements are written to “maximize” or “minimize” a specific value associated with the product needs in order to define the goal or aim of the design process.

This ultimately implies that, objective statements are used by various manufacturing industries or companies to explicitly define the minimum or maximum requirements for the production of its goods.

4 0
3 years ago
Read 2 more answers
A steel rule can be used to check for
MAXImum [283]
I THINK THE ANSWER IS B BUT IM NOT SURE OK BYE
3 0
3 years ago
Other questions:
  • Identify each statement as referring to a series or parallel circuit.
    15·1 answer
  • HELP!
    8·1 answer
  • To 3 significant digits, what is the change of entropy of air in kJ/kgk if the pressure is decreased from 400 to 300 kPa and the
    15·1 answer
  • List fabrication methods of composite Materials.
    12·1 answer
  • Consider a 1.2-m-high and 2-m-wide glass window with a thickness of 6 mm, thermal conductivity k = 0.78 W/m·K, and emissivity ε
    5·1 answer
  • In a paragraph of 125 words, explain at least three ways that engineers explore possible solutions in their
    8·1 answer
  • A fair die is thrown, What is the probability gained if you are told that 4 will
    12·1 answer
  • Recall the steps of the engineering design process. Compare and contrast the
    9·1 answer
  • True or false for the 4 questions?
    8·1 answer
  • What person at the construction worksite keeps workers safe from asbestos exposure?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!