信号量与PV操作

1. 独木桥问题

1.1 n辆汽车,单向通行

汽车从东西两个方向通过独木桥,为了保证安全,一次只允许其中一个方向的汽车过桥;

待其中一个方向所有汽车通过之后,另一个方向的汽车才允许过桥;

桥面上的汽车数量没有限制。

semaphore wait, mutex1, mutex2; wait=mutex1=mutex2=1;
int count1, count2; count1=count2=0;

cobegin
    process West() {                process East() {
        while(true) {                   while(true) {
            P(mutex1);                      P(mutex2);
            count1++;                       count2++;
            if(count1 == 1)                 if(count2 == 1)
                P(wait);                        P(wait);
            V(mutex1);                      V(mutex2);
            /* 过独木桥 */                  /* 过独木桥 */
            P(mutex1);                      P(mutex2);
            count1--;                       count2--;
            if(count1 == 0)                 if(count2 == 0)
                V(wait);                        V(wait);
            V(mutex1);                      V(mutex2);
        }                               }
    }                               }
coend

1.2 k辆汽车,单向通行

在问题一的基础上,桥面上的汽车数量最多为k。

semaphore wait, bridge, mutex1, mutex2; wait=bridge=mutex1=mutex2=1;
int count1, count2; count1=count2=0;

cobegin
    process West() {                process East() {
        P(bridge);                      P(bridge);       /* (#) */
        P(mutex1);                      P(mutex2);
        count1++;                       count2++;
        if(count1 == 1)                 if(count2 == 1)
            P(wait);                        P(wait);
        V(mutex1);                      V(mutex2);
    //  P(bridge);                      P(bridge);       /* (#)也可以在此处执行 */
        /* 过独木桥 */                   /* 过独木桥 */
        V(bridge);                      V(bridge);
        P(mutex1);                      P(mutex2);
        count1--;                       count2--;
        if(count1 == 0)                 if(count2 == 0)
            V(wait);                        V(wait);
        V(mutex1);                      V(mutex2);
    }                               }
coend

1.3 3辆汽车为一组,单向交替通行

在问题一的基础上,东西方向以三辆汽车为一组,交替通过独木桥。

semaphore wait, mutex1, mutex2;
wait=mutex1=mutex2=1;
int countu1, countu2, countd1, countd2;    /* countu1&2是指当前桥面上的汽车数量,countd1&2是指当前方向上已经通过的汽车数量(最大为3) */
countu1=countu2=countd1=countd2=0;
semaphore S1, S2; S1=3; S2=0;              /* S1&2是指当前方向上还可以上桥的汽车数量 */

cobegin
    process West() {                                process East() {
        while(true) {                                   while(true) {
            P(S1);                                          P(S2);
            P(mutex1);                                      P(mutex2);
            count1++;                                       count2++;
            if(count1 == 1)                                 if(count2 == 1)
                P(wait);                                        P(wait);
            V(mutex1);                                      V(mutex2);
            /* 过独木桥 */                                  /* 过独木桥 */
            V(S1);                                          V(S2);
            P(mutex1);                                      P(mutex2);
            countu1--;                                      countu2--;
            countd1++;                                      countd2++;
            if((countu1==0) && (countd1==3)) {              if((countu2==0) && (countd2==3)) {
                countd1=0;                                      countd2=0;
                V(wait);                                        V(wait);
            }                                               }
            V(mutex1);                                      V(mutex2);
        }                                               }
    }                                               }
coend

1.4 相应对方请求,单向通行

在问题一的基础上,要求各方向的汽车串行过桥,但当另一方提出过桥时,应能阻止对方未上桥的后继车辆,待桥面上的汔车过完桥后,另一方的汽车开始过桥。

semaphore stop,wait,mutex1,mutex2;
stop=mutex1=mutex2=wait=1;  
int count1,count2; 
count1=count2=0;

cobegin
    process West() {                    process East() {
        P(stop);                            P(stop);
        P(mutex1);                          P(mutex2);
        count1++;                           count2++;
        if(count1 == 1)                     if(count2 == 1)
            P(wait);                            P(wait);
        V(mutex1);                          V(mutex2);
        V(stop);                            V(stop);
        /* 过独木桥 */                       /* 过独木桥 */
        P(mutex1);                          P(mutex2);
        count1--;                           count2--;
        if(count1 == 0)                     if(count2 == 0)
            V(wait);                            V(wait);
        V(mutex1);                          V(mutex2);
    }                                   }
coend

2. 信箱链收发

2.1 信箱链收发


四个进程 Pi (i=0, 1, 2, 3) 和四个信箱 Mj (j=0, 1, 2, 3),进程间借助相邻信箱传递消息,即 Pi 每次从 Mi 中取一条消息,经加工后送入 M(i+1)mod4,其中 M0, M1, M2, M3分别可存放 3、3、2、2个消息。

初始状态下, M0 装了三条消息,其余为空,求写出 Pi(i=0, 1, 2, 3)的同步工作算法。

semaphore mutex0, mutex1, mutex2, mutex3;
mutex0=mutex1=mutex2=mutex3=1;
semaphore empty0, empty1, empty2, empty3;
empty0=0; empty1=3; empty2=empty3=2;
semaphore full0,full1,full2,full3;
full0=3; full1=full2=full3=0;
int in0,in1,in2,in3,out0,out1,out2,out3;
in0=in1=in2=in3=out0=out1=out2=out3=0;

cobegin
    /* 以P0为例 */
    process P0() {
        while(true) {
            P(full0);
            P(mutex0);
            /*从 M0[out0] 取一条消息*/;
            out0=(out0+1) % 3;
            V(mutex0);
            V(empty0);    /* 读取结束,该被读信箱可存消息量+1 */
            /* 加工消息 */;
            P(empty1);    /* 发送信件前先向所要发往的信箱预留空间 */
            P(mutex1);
            /* 消息存 M1[in1] */;
            in1=(in1+1) % 3;
            V(mutex1);
            V(full1);    /* 由当前进程P0开始往后循环进行 */
        }
    }
coend

3. 未完待续

3.1 未完待续

未完待续

THX!

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值