使用信号量的一车道桥梁

时间:2017-12-02 20:40:43

标签: multithreading concurrency parallel-processing semaphore

在大学里,我从“Gregory R. Andrews-Foundations of Multithreaded ....编程”中得到了这个规范的并行编程问题:(虽然我有一本较新的俄语版本的书,我发现了一个古老的英语版本并尝试正确传达一切)
One Lane Bridge。来自北方和南方的汽车到达一个 - 车道桥。朝同一方向行驶的汽车可以同时穿过桥梁 时间,但相反方向的汽车不能。 指定全局不变量制定合理解决方案,以便汽车可以在不超过m 的汽车组中移动....使用信号量来解决这个任务
(是的,我知道有single-lane bridge problem但是)讲师告诉我使用书中描述的方法来实现它具体:使用“读者作家”问题的解决方案,我应该有两种过程(north_car和south_car)它应该像“读者”一样,所以我试图模仿解决方案:
读者/作家:

//RW: (nr == 0 or nw == 0) and nw <= 1 "global invariant" (=A "good" state)
int nr = 0,nw = 0;
sem e = 1, //used to perform atomic actions
r = 0, //used to pause readers
w = 0; //used to pause writers
//always 0<=(e+r+w)<=1, (e,r,w) form a "shared binary semaphore"
int dr = 0, //number of paused readers
dw = 0; //number of paused writers
process Reader[i = 1 to M] { 
    while (true) {  
        P(e);
        if (nw >0) { dr = dr+1; V(e); P(r); } //(1) 
        nr = nr+1;
        if(dr>0){dr=dr-1;V(r);} //"passing baton" 
        else V(e);
        read resource;
        P(e);
        nr = nr-1;
        if(nr==0 and dw>0){dw=dw-1;V(w);} //"passing baton"
        else V(e);
    }
}
process Writer[i = 1 to N] { 
    while (true) {
        P(e);
        if (nr > 0 or nw >0)  { dw = dw+1; V(e); P(w); }
        nw = nw+1;
        V(e)
        write resource;
        P(e);
        nw = nw-1;
        if(dr>0){dr=dr-1;V(r);} //(2) continue reader-process
        elseif (dw>0){dw=dw-1;V(w);} //(3) continue writer-process
        else V(e); //release entry lock
        }
}

所以这是我错误的尝试来解决我的问题:

int nn = 0,ns = 0;
int m;//consider initialised
sem e = 1, 
n = 0, 
s = 0; //lecturer's comment: "l+n+s=1 ??"
int dn = 0, ds = 0; 
## RW: (nn == 0 and ns < m) or (ns == 0 and nn < m) "global invariant"
process North_car[i = 1 to N] { 
    while (true) {  
        P(e);
        if (ns >0) { dn = dn+1; V(e); P(n); } 
        if(nn<mm){ //I tried to add this when was asked
            //"WHERE IS THE "move in groups of no more than m cars" CONDITION??"
            //but it seems no luck(
            nn = nn+1;
            if(dn>0){dn=dn-1;V(n);} //"passing baton" 
            else V(e);
            cross bridge going from north;
            P(e);
            nn = nn-1;
            if(nn==0 and ds>0){ds=ds-1;V(s);} 
            else V(s);
        } //I tried to add this
    }
}
process South_car[i = 1 to S] { 
    while (true) {  
        P(e);
        if (nn >0 or ns > m) { //my another try is adding "or ns > m"
            ds = ds+1; V(e); P(s);
        } 
        if(ns<m){ //I tried to add this 
            ns = ns+1;
            if(ds>0){ds=ds-1;V(s);} //"passing baton" 
            else V(e);
            cross bridge going from south;
            P(e);
            ns = ns-1;
            if(ns==0 and dn>0){dn=dn-1;V(n);} 
            else V(s);
        } //I tried to add this
    }
}

但当然我的解决方案只是尝试随机模仿书中的代码......而且这不是解决问题的正确方法。 请使用此提议的语法帮助我正确解决。 我的下一个任务是solve analogous task using "Monitor"我决定将其作为另一个问题发布

0 个答案:

没有答案