Model a common roadway occurrence, where a lane is closed and a flag person is directing traffic. There is a two-lane road with one lane closed, and vehicles are approaching from the North and South directions. Due to the traffic lights, the cars arrive in bursts. When a car reaches the construction area, there is an 80% chance that another car will follow it. However, if no car comes, there will be a 20-second gap (utilizing the provided pthread_sleep function) before any new car arrives. During the intervals where no cars are at either end, the flag person will rest. However, when a car arrives at either end, the flag person will wake up and manage the traffic flow from that side, until there are no more cars from that side or until there are 10 or more cars waiting in the opposite direction. If there are 10 or more cars on the opposite side, the flag person must allow those cars to pass first. Each car takes one second to travel through the construction area. Your task is to build a simulation of these events, ensuring that a deadlock never occurs. Requirements You must design and implement a multi-threaded C/C++ program using the Pthread library that avoids deadlocks. You need to utilize Pthread synchronization structures, such as semaphores and mutex locks, to achieve synchronization. Solely using mutex locks for synchronization is not a valid solution for this problem. You are not allowed to use condition variables. Question: I am so confused on how to start this. Would you provide any pseduocode or some steps I have to follow or reply to this questions? My understanding is that I have to queues Norht and South. Echa car will be a tread and there would be 2 semaphores for the counting of how many cars are at each side? I will also need a lock(mutex) that that flag person will hold to control de traffic flow. I that correct?
Model a common roadway occurrence, where a lane is closed and a flag
person is directing traffic. There is a two-lane road with one lane closed, and vehicles are
approaching from the North and South directions. Due to the traffic lights, the cars arrive in bursts. When
a car reaches the construction area, there is an 80% chance that another car will follow it. However, if no
car comes, there will be a 20-second gap (utilizing the provided pthread_sleep function) before any new
car arrives. During the intervals where no cars are at either end, the flag person will rest. However, when a car arrives at either end, the flag person will wake up and manage the traffic flow from that side, until there are no more cars from that side or until there are 10 or more cars waiting in the opposite direction. If there are 10 or more cars on the opposite side, the flag person must allow those cars to pass first.
Each car takes one second to travel through the construction area. Your task is to build a simulation of these events, ensuring that a deadlock never occurs.
- Requirements
You must design and implement a multi-threaded C/C++ program using the Pthread library that avoids deadlocks. You need to utilize Pthread synchronization structures, such as semaphores and mutex locks, to achieve synchronization. Solely using mutex locks for synchronization is not a valid solution for this problem.
You are not allowed to use condition variables.
Question: I am so confused on how to start this. Would you provide any pseduocode or some steps I have to follow or reply to this questions? My understanding is that I have to queues Norht and South. Echa car will be a tread and there would be 2 semaphores for the counting of how many cars are at each side? I will also need a lock(mutex) that that flag person will hold to control de traffic flow. I that correct?
Step by step
Solved in 2 steps