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

cars coming from the north and the south arrive at a one lanebridge. cars headin

ID: 3610505 • Letter: C

Question

cars coming from the north and the south arrive at a one lanebridge. cars heading in the same direction can cross thebridge at the same time, but cars heading in opposite directioncannot.

a) develop a solution to this problem. first specify a globalinvariant then develop a solution using semaphores forsynchronization. do not worry about fairness.

b) modify your answer to ensure that any car that is waiting tocross the bridge eventually gets to do so, you may want to solvethe problem differently

c) implement you solution using java semaphore

Explanation / Answer

Dear...        a)
   Here we use works for both SC and SWsemantics Invariant:{ns=0 or ns=0 ) and (non (!empty(ns_c)=> sn > 0 ) and (!empty(sn_c) => ns > 0)}    monitor One_lane_bridge    {       int ns =0;       int sn =0;       cond ns_c:       cond sn_c;    private procedure startNorth()      {         while (sn >0) wait(ns_c);         ns++     }    private procedure endSouth()    {        ns--;        if (ns == 0)signal_all(sn_c)); //signals possible waiting sn cars    }    public cross_from_North() {     startNorth();         endSouth(); }     endSouth(); } c)     Java solution without fairness
class One_lane_bridge {    private int ns =0    private int sn =0    private int wns =0    private int wsn =0    private char p= “ “    private synchronized void startNorth() {     while (sn>0) or ((wsn>0) and(ns>0) and !(p==“n”))     {      wns++;      try     {        wait();        catch (interrupter executionex    }    {return;}     wns--; };    if (wns == 0) p = ” ”;    ns++; } private synchronized void endSouth() { ns--; if (ns == 0)   {     p = “s”;     notifyAll();   } } public void cross_from_North() { startNorth(); endSouth(); } } Try for remaining ( b ) option.
I hope it is useful to you.