Question

In: Computer Science

There is a single-plank bridge. Only one person can cross the bridge without any stop. The...

There is a single-plank bridge. Only one person can cross the bridge without any stop. The pedestrian can cross the bridge from west to east or from east to west. If there is more than 0 pedestrian crossing the bridge from west to east, no pedestrian can try to cross the bridge from east to west, but more than 1 pedestrian can cross the bridge from west to east, and vice versa. The pedestrian can cross the bridge immediately if there is no pedestrian on the bridge. Use Semaphore to solve this problem.

Solutions

Expert Solution

To the above problem one solution can be to block one way and let a limited number cars through:

1. It uses a simple variable max to allow maximum number of pedestrain to cross from WtoE or EtoW.

Below is the pseudo code for such a system being implemented using semaphore :


max = 10 // setting car limits
function main():
    while(true):
        if(westToEast):
            let_west_to_east()
        if(eastToWest):
            let_east_to_west()


function let_east_to_west():
    sem_wait(westToEast) // Waiting for west to east direction
    for(i = 0; i < max; i++):
        if(east):
            cross_bridge()
        else:
            break; // limit cross max reached

    sem_post(westToEast) // let west to east go

function let_west_to_east()
    sem_wait(eastToWest) // Waiting for east to west direction
    for(i = 0; i < max; i++):
        if(west):
            cross_bridge()
        else:
            break;

    sem_post(eastToWest) // let east to west go

Related Solutions

Question3 public goods are things that: A)can be enjoyed by more then one person without any...
Question3 public goods are things that: A)can be enjoyed by more then one person without any reduction in the goods B) people cannot be excluded from their consumption C) are unlikely to be provided by individual rational actors D) all of the above
1. A gene can have many alleles. Why is it that any given person only carries...
1. A gene can have many alleles. Why is it that any given person only carries two copies of each allele? Pick one below This is a function of co-dominance, or incomplete dominance. During fertilization a gamete only contributes one allele from each parent. Chromosomes lose the extra alleles during cross over or recombination. Having more than one allele results in cell death. 2. The X-linked recessive trait of color-blindness is present in 5% of males. If a mother who...
Consider an ATM of a bank located at a metro station. Only one person can use...
Consider an ATM of a bank located at a metro station. Only one person can use the machine, so a queue forms when two or more customers are present. The metro station is limited in size and can hold only five people. When there are more than five people, arriving customers will not enter the queue. Statistics indicates that the average time between arrivals is 30 seconds, whereas the time for service averages 24 seconds. Both times follow exponential distribution....
Do any equipotential lines cross one another?
physicsQuestions Do any equipotential lines cross one another?Do any field lines cross one another? Estimate the electric field strength at a point :(a) half way between the terminals and(b) near one of the terminals by using the relation: where AV is the potential difference between two points (for example, two points on successive equipotential lines) and AL is the distance measured along a field line. Where is the field the strongest? Where is the field the weakest?
Hello there , can any one please find an answer for this question Q. a single...
Hello there , can any one please find an answer for this question Q. a single lossless dielectric slab of a thickness “d” and permittivity “ε2” is inserted between two regions. the first region is the free space and the second one is a lossless dielectric material of permittivity “ε3”. a uniform plane wave propagating in y- direction and polarized in z-direction is normally incident on the first region. derive an analytical expression for the total reflection coefficient and the...
Without any changes in their diet a person has been loosing weight and their feces have...
Without any changes in their diet a person has been loosing weight and their feces have become fatty. The medical diagnosis is "a condition limiting the production of bile". Why do you think inadequate bile leads to the symptoms of weight loss and fatty feces? Your answer must discuss the role of bile in fat digestion.
Write a regular expression that can represent any digit, without more than one digit or non...
Write a regular expression that can represent any digit, without more than one digit or non digit characters.
Hi, I want to programm a c++ gamr without the using of any array. only by...
Hi, I want to programm a c++ gamr without the using of any array. only by using FUNCTIONS, WHILE LOOPS, FOR LOOPS, IF ELSE, SRADN() and RAND(), BREAK STATEMENT, CIN and COUT. Please help me out.
“If a person owns 80% of the shares in a company then they can make any...
“If a person owns 80% of the shares in a company then they can make any changes they like to the company’s constitution.” REQUIRED: Critically discuss, stating whether you agree or disagree with the above statement. Refer to relevant case law and/or statutory law in your answer as appropriate.
“If a person owns 80% of the shares in a company then they can make any...
“If a person owns 80% of the shares in a company then they can make any changes they like to the company’s constitution.” REQUIRED: Critically discuss, stating whether you agree or disagree with the above
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT