One lane bridge using monitor

一曲冷凌霜 提交于 2019-12-08 00:03:27

问题


In the University I'm given this canonical parallel programming problem from "Gregory R. Andrews-Foundations of Multithreaded .... programming": (though I have a newer and Russian edition of the book I found an old English variant and try to convey everything properly)
I was also given task to solve this problem but with m consequently moving cars possible using semaphores To solve that task I was told by the tutor to mimic Reader's behavior from readers-writers task
The One-Lane Bridge. Cars coming from the north and the south arrive at a one- lane bridge. Cars heading in the same direction can cross the bridge at the same time, but cars heading in opposite directions cannot. Develop a solution to this problem. Model the cars as processes, and use a monitor for synchronization. First specify the monitor invariant, then develop the body of the monitor.Ensure fairness. (Have cars take tums)
I googled and found solution for analogous task (http://www.cs.cornell.edu/courses/cs4410/2008fa/homework/hw3_soln.pdf) but lecturer said most of it is useless and incorrect I ended up with the following solution:

monitor onelanebridge{
    int nb=0,sb=0; //Invar:(nb==0 and sb<=1)or(sb=0 and nb<=1)
    cond nbfreetogo,sbfreetogo; //conditional variables
    procedure enter_n(){
        if(sb!=0andnb==0) wait(nbfreetogo);
        nb++;
    }
    procedure enter_s(){
        if(nb!=0andsb==0) wait(sbfreetogo);
        sb++;
    }
    procedure leave_n(){
        nb--;
        if(nb==0) signal(sbfreetogo);
    }
    procedure leave_s(){
        sb--;
        if(sb==0) signal(nbfreetogo);
    }
}

I was asked the question "What ensures that no more than one car at a time can cross the bridge?".. And am not even sure whether it's even so... Please help me solve the task correctly. I must use only constructions from the above mentioned book... Example of readers-writers problem solution from the book:

monitor RW_Controller {
    int nr = 0, nw =0;  //Invar:  (nr == 0 or nw ==  0) and nw  <= 1
    cond oktoread; # recieves signal, when  nw == 0
    cond oktowrite; # recieves signal, when  nr == 0  и nw  == 0
    procedure request_read() {
        while (nw > 0) wait(oktoread); 
        nr = nr + 1;
    }
    procedure release_read() { 
    nr = nr - 1;
    if (nr == 0) signal(oktowrite);
        # run one writer-process
    }
    procedure request_write()  {
        while (nr > 0 || nw > 0) wait(oktowrite); 
            nw = nw + 1 ;
        }
    procedure release_ write() { 
        nw = nw - 1;
        signal(oktowrite); # run one writer-process and
        signal_all(oktoread); # all reader-processes
    }
}

Of course my solution is just a random try. Halp me please to solve the task properly
Note: A variable of "conditional variable" type according to the book is a "wait queue" which has these methods:

wait(cv)//wait at end of queue  
wait(cv,rank)//wait in order of increasing value of rank  
signal(cv)//awaken process at end of queue then continue
signal_all(cv)//awaken all processes at end of queue then continue  
empty(cv) //true if wait queue is  empty; false otherwise  
minrank(cv) //value of rank of process at front of wait queue  

And so I should solve the task probably using some of these


回答1:


Your monitor onelanebridge is not far off the mark, but it has no notion of fairness. If there was a steady stream of northbound traffic, nothing would trigger a switch to southbound. You need to separate the count of waiting and ‘active’.

A simple fairness would be to alternate, so you could limit the ‘active’ counter at 1; and check whether to switch when it becomes zero. To avoid inciting road rage, you might choose a limit based on the transit time of the single lane section.

You would now have vehicles waiting in enter_[ns] which had the right direction, but have to wait because of the limit, so your if (cond) wait needs to become while (more complex cond) wait.

Concurrent programming is not natural, but with practise can become ingrained. Try and think of the problem at hand rather than how can I employ these mechanisms.



来源:https://stackoverflow.com/questions/47613297/one-lane-bridge-using-monitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!