Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

course: operating system 1. Assume there are cars driving on one side of the int

ID: 3590296 • Letter: C

Question

course: operating system

1. Assume there are cars driving on one side of the interstate which has two lanes. At one point along the road there is construction that causes only one lane to continue. We want to be able to merge the cars in the two lanes into one lane without having any accidents. Assume that if a car reaches the merging point, and there is no car in the other lane at the same spot, then the car can continue along the road. If a car reaches the merging point and finds one car already there in the other lane, then it must wait its turn. Whenever there are multiple cars waiting in both lanes, the cars should take turns moving forward. Write pseudocode algorithms using semaphores/counters/etc. for cars in the left and right lanes that will allow the merging to occur as described.

Explanation / Answer

Answer:

Pseudocode:

Set lane to one

If

car reaches merging point and no car in other lanes

print “continue along the road”

else if

car reaches at merging point and one car in another lane

print “wait for turn"

else if

multiple cars waiting in both lane

print “car should take turns moving forward"