"Operating System Guide" 5th Edition -Chapter3-2- synchronous communication with the deadlock - the core PV title

Producer - consumer issues

 

semaphore mutex=1;
semaphore empty=n;
semaphore full=0;

producer(){
    while(1){
        produce item;
        P(empty);
        P(mutex);
        add item to buffer;
        V(mutex);
        V(full);
    }
}

consumer(){
    while(1){
        P(full);
        P(mutex);
        remove item from buffer;
        V(mutex);
        V(empty);
    }
}

Apple oranges problem

semaphore plate=1,apple=0,orange=0;
dad(){
    prepare apple;
    P(plate);
    put apple;
    V(apple);
}


mom(){
    prepare orange;
    P(plate);
    put apple;
    V(orange);
}


son(){
    P(orange);
    take orange;
    V(plate);
}


daughter(){
    P(apple);
    take apple;
    V(plate);
}

Readers write the problem - writer priority

int count=0;//读者数量
semaphore mutex=1;//count互斥
semaphore rw=1;//读者写者互斥
semaphore w=1 //写优先

writer(){
P(w);
P(rw);
writing;
V(rw);
V(w);
}

reader(){
P(w);
P(mutex);
if(count==0) P(rw);
count++;
V(mutex);
V(w);
reading;
P(mutex);
count--;
if(count==0) V(rw);
V(mutex)
}

Barber sleep problems

semaphore mutex=1,customers=0,barber=0;
int waiting=0;

barber(){
    P(customers);
    P(mutex);
    waiting--;
    V(barber);
    V(mutex);
    haircut;
}

customers(){
    P(mutex);
    if(waiting>n){
        V(mutex);
        leave;
    }
    waiting++;
    V(customers);
    V(mutex);
    P(barber);
    haircut;
}

Smokers problem

 

int random;
semaphore offer1=0,offer2=0,offer3=0;
semaphore finish=0;

procedure P1{
    random=getRondom();
    random=random%3;
    if(random==0) V(offer1)
    if(random==1) V(offer2)
    if(random==2) V(offer3)
    P(finish);
}

procedure P2{
    P(offer3);
    V(finish);
}

procedure P3{
    P(offer2);
    V(finish);
}

procedure P3{
    P(offer1);
    V(finish);
}

In a box, mixed up an equal number of black and white Go son. Now automatic sorting system to separate sunspots, albino, provided the system has two sorting processes P1 and P2, wherein P1 picking albino; ​​P2 pick sunspots. Every process requires that each pick a child; when picking a process, not allow another process Qujian; When a process picked up a midnight, another process must be allowed to Qujian. Write the program two concurrent processes P1 and P2 can be performed correctly.

semaphore S1,S2;
S1=1;S2=0;
cobegin
process P1( ) {
    while(true) {
     P(S1);
     拣白子
     V(S2);
    }
  }
process P2( ) {
    while(true) {
     P(S2);
     拣黑子
     V(S1);
    }
  }

Four process Pi (i = 0 ... 3) and four mailbox Mj (j = 0 ... 3), interprocess transfer means adjacent mail message, i.e. Pi each taking a message from the Mi, M fed after processing (i + 1) mod4, where M0, M1, M2, M3 respectively stored 3,3,2,2 message. Initially, three messages M0 installed, the rest is empty. Illustrated with P, V for the tool operation, write Pi (i = 0 ... 3) of the synchronization algorithm.

semaphore mutex1,mutex2,mutex3,mutex0;
mutex1=mutex2=mutex3=mutex0=1;
semaphore empty0,empty1,empty2,empty3;
empty=0;empty1=3;empty=2;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

process P0( ) {
   while(true) {
    P(full0);
    P(mutex0);
    {从M0[out0]取一条消息};
    out0=(out0+1) % 3;
    V(mutex0);
    V(empty0);
    {加工消息};
    P(empty1);
    P(mutex1);
    {消息存M1[in1]};
    in1=(in1+1) % 3;
    V(mutex1);
    V(full1);
    }
  }

process P1( ) {
    while(true) {
    P(full1);
    P(mutex1);
    {从M1[out1]取一条消息};
    out1=(out1+1) % 3;
    V(mutex1);
    V(empty1);
    {加工消息};
    P(empty2);
    P(mutex2);
    {消息存M2[in2]};
    in2=(in2+1) % 2;
    V(mutex2);
    V(full2);
    }
  }

process P2( ) {
   while(true) {
    P(full2);
    P(mutex2);
    {从M2[out2]取一条消息};
    out2=(out2+1) % 2;
    V(mutex2);
    V(empty2);
    加工消息;
    P(empty3);
    P(mutex3);
    {消息存M3[in3]};
    in3=(in3+1) % 2;
    V(mutex3);
    V(full3);
    }
  }

process P3( ) {
   while(true) {
    P(full3);
    P(mutex3);
    {从M3[out3]取一条消息};
    out3=(out3+1) % 2;
    V(mutex3);
    V(empty3);
    {加工消息};
    P(empty0);
    P(mutex0);
    {消息存M0[in0]};
    in0=(in0+1) % 3;
    V(mutex0);
    V(full0);
    }
  }
coend

Today there are k process, their numbers were 1,2, ..., k, if they are allowed to read the file at the same time file, but the conditions must be met: the label at the same time to participate in the process of reading the file and must be less than M (k <M) use: 1) the amount of the signal P, V operation, 2) the tube, a program coordinated multiple write process reads the file.

 

1) 使用信号量与P、V操作
semaphore waits,mutex;
   int numbersum=0;
   wait=0;mutex=1;
cobegin
process readeri(int number) {    //i=1,2,…
      P(mutex);
while(numbersum+number>=M)
     {V(mutex);P(waits);}
       numbersum=numbersum+number;
      V(mutex);
      Read file;
      P(mutex);
      numbersum=numbersum-number;
      V(waits);
      V(mutex);
}

自写:
process readeri(int number) { //i=1,2,…
    P(mutex);
    while (numbersum + number >= M) {
        P(waits);
    }
    numbersum = numbersum + number;
    V(mutex);
    Read file;
    P(mutex);
    numbersum = numbersum - number;
    V(waits);
    V(mutex);
}

coend
A warehouse can be stored X, Y two products, warehouse storage space is large enough, provided that: (1) can only deposit a product X or Y, (2) satisfies -N <Quantity X - product number Y <M. Where, N and M are positive integers, and the amount of trial signal P, V during operation to achieve storage of X and Y products.
 
 
A: This question gives expression decomposed into constraints:
                 -N <X number of product -Y products
                  Quantity Quantity X -Y <M
That is, the number of products X can not be less than the number Y of the product more than N, the number of products X can not be larger than the number M of Y or more products. Two signals may be provided to control an amount of X, Y store the number of products:
sx表示当前允许X产品比Y产品多入库的数量,即在当前库存量和Y产品不入库的情况下,还可以允许sx个X产品入库;初始时,若不放Y而仅放X产品,则sx最多为M-1个。
sy表示当前允许Y产品比X产品多入库的数量,即在当前库存量和X产品不入库的情况下,还可以允许sy个Y产品入库。初始时,若不放X而仅放Y产品,则sy最多为N-1个。
当往库中存放入一个X产品时,则允许存入Y产品的数量也增加1,故信号量sy应加1;当往库中存放入一个Y产品时,则允许存入X产品的数量也增加1,故信号量sx应加1
 
semaphore mutex=1;         /*互斥信号量*/
     semaphore sx,sy;
     sx=M-1; sy=N-1;
cobegin
  process storeX( )  {
   while(true) {
        P(sx);
        P(mutex);
        {将X产品入库};
        V(mutex);
        V(sy);
        }
    }
  process  storeY( )  {
    while(true) {
        P(sy);
        P(mutex);
        {将Y产品入库};
        V(mutex);
        V(sx);
        }
}
coend

 

某银行有人民币储蓄业务,由n个储蓄员负责。每个顾客进入银行后先取一个号,并且等着叫号。当一个储蓄人员空闲下来,就叫下一个号。请用P,V操作正确编写储蓄人员和顾客进程的程序。

 

var customer_count,mutex:semaphore;
    customer_count:=0;
   mutex:=1;
cobegin
   process customer
     begin
      L1: take a number;
          P(mutex);
            进入队列;
          V(mutex);
          V(customer_count);
          Go to L1;
     End;
   Process serversi(I=1,2,3,…)
     Begin
      P(customer_count);
      P(mutex);
      从队列取号;
      V(muyex);
      为该号客人服务;
     end;
coend.

机房问题

 

semaphore c=2m;
int s=0;
semaphore mutex=1;
semaphore enter=1;
semaphore finish=0;
semaphore check=0;

student_i{
    P(c);
    P(mutex);
    s++;
    if(s==1){
        V(mutex);
        P(enter);
    }
    else{
        s=0;
        V(mutex);
        V(enter);
    }
    上机
    V(finish);
    P(check);
    V(c);
}

teacher(){
    while(1){
        P(finish);
        P(finish);
        V(check);
        V(check);
    }
}

工厂装配问题

semaphore empty1=10;
semaphore empty2=10;
semaphore mutex1=1;
semaphore mutex2=1;
semaphore full1=0;
semaphore full2=0;

A_factory{
    P(empty1);
    生产A
    P(mutex1);
    put A
    V(mutex1);
    V(full1);
}

B_factory{
    P(empty2);
    生产B
    P(mutex2);
    put B
    V(mutex2);
    V(full2);
}

装配工{
    P(full1);
    P(mutex1);
    取货品
    V(mutex1);
    V(empty1);
    
    P(full2);
    P(mutex2);
    取货品
    V(mutex2);
    V(empty2);
    组装
}

 

发布了15 篇原创文章 · 获赞 1 · 访问量 1万+

Guess you like

Origin blog.csdn.net/m0_37302219/article/details/104267894