Question: C programming. Please don't use any previous chegg solutions, if you do I will be forced to down vote the solution so please provide an

C programming. Please don't use any previous chegg solutions, if you do I will be forced to down vote the solution so please provide an original answer. Please provide the code for the program file (.c), and provide screenshots of sample runs of the program. Please include commenting with your code.

A single-lane bridge connects the two Vermont villages of North Tunbridge and South Tunbridge. Farmers in the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become deadlocked if a northbound and a southbound farmer get on the bridge at the same time. (Vermont farmers are stubborn and are unable to back up.) Using semaphores and/or mutex locks, design an algorithm in pseudocode that prevents deadlock. Initially, do not be concerned about starvation (the situation in which northbound farmers prevent southbound farmers from using the bridge, or vice versa).

  1. a) Write the pseudocode algorithm.
  2. b) Modify the solution so that it is starvation-free.
  3. c) Implement the solution using POSIX synchronization. In particular, represent northbound and southbound farmers as separate threads. Once a farmer is on the bridge, the associated thread will sleep for a random period of time, representing traveling across the bridge. Design the program so that you can create several threads representing the northbound and southbound farmers.

The program will take 3 arguments. The first is the maximum time in seconds that a farmer will spend on the bridge. The second is the number of northbound farmers, and the third is the number of southbound farmers. The second and third parameters represent the number of threads to create.

The output will be the order that the farmers access the bridge.

An example run would be:

./a.out 5 5 8

5 northbound farmers.

8 southbound farmers.

Farmer 1 south crossed in 3 time.

Farmer 2 south crossed in 1 time.

Farmer 1 north crossed in 3 time.

Farmer 3 south crossed in 3 time.

Farmer 4 south crossed in 3 time.

Farmer 5 south crossed in 4 time.

Farmer 2 north crossed in 3 time.

Farmer 3 north crossed in 0 time.

Farmer 4 north crossed in 1 time.

Farmer 5 north crossed in 4 time.

Farmer 6 north crossed in 2 time.

Farmer 8 north crossed in 4 time.

Farmer 7 north crossed in 1 time.

Note: The program solution will be starvation free, because there are a finite number of farmers crossing in each direction. Therefore it is not necessary to account for starvation in the program.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!