Use the recorded semaphore mechanism: wait(s), signal(s) to solve process synchronization problems

wait(s) is equivalent to P operation, signal(s) is equivalent to V operation

producer-consumer problem

int in = 0, out = 0;
item buffer[n];
semaphore mutex = 1, empty = n, full = 0;
void procuder() {
  do {
      procuder an item nextp;
      ...
      wait(empty);//表示空缓冲区-1;
      wait(mutex);//数据缓冲区互斥访问,关闭访问;
      buffer[in] = nextp;
      in = (in+1) % n;
      signal(mutex);//打开访问
      signal(full);//满缓冲区加+1
  } while (true);
}

void consumer() {
   do {
       wait(full);//判断是否有满缓冲区,满缓冲区-1
       wait(mutex);
       nextc = buffer[out];
       out = (out+1) % n;
       signal(mutex);
       siganl(empty);//空缓冲区+1
       consumer the item in nextc;
       ....
   } while (true);
}

void main() {
  cobegin
    producer(); consumer();
  coend
}

Mom and Dad wash fruit, son and daughter eat fruit problem

Problem description: Dad puts apples on the plate, mother puts oranges on the plate, son waits to eat the oranges on the plate, and daughter waits to eat the apples on the plate. As long as the plate is empty, parents can put fruit on the plate, and only when the plate has the fruit they need, the son or daughter can take it out. Please give the synchronization relationship between the four of them, and use the PV operation to achieve the correctness of the four program of activity. . Use p, v operations to complete the synchronization behavior simulation of father, mother, son, daughter.

semaphore putmutex = 1, getmutex = 0;
semaphore empty = 1, apple = 0, orange = 0;
void father() {
  do {
    wait(putmutex);
    apple++;
    empty--;
    signal(putmutex);
  } while (true);
}
void mother() {
  do {
    wait(putmutex);
    orange++;
    empty--;
    signal(putmutex);
  } while (true);
}
void son() {
   do {
     wait(getmutex);
     orange--;
     empty++;
     signal(getmutex);
   } while (true);
}
void daughter() {
  do {
    wait(getmutex);
    apple--;
    empty++;
    signal(getmutex);
  } while (true);
}
void main() {
  cobegin
    father(); mother(); son(); daughter();
  coend
}

In addition, you can refer to the implementation of java: http://www.cnblogs.com/zyp4614/p/6555530.html

Driver, conductor question

Problem:
The driver drives the car and the conductor sells the ticket. When the conductor closes the door, the driver can drive, and when the driver stops the car, the conductor can open the door.

For S1 (door): The door has two states, open and closed. After the conductor closes the door, he should give the driver the right to operate the door (because the driver can only stop when he arrives). So 0 is the door open state.
For S2 (vehicle): two states, driving and stopping at the station. When the car is driving, you need to apply for the resources of the car, that is, use wait, the state of the car is 1 when it is parked, and the state is 0 when it is driving.
Driver: drive, drive normally, stop at the station
Conductor : close the door, sell tickets, open the door

**S1 indicates whether the driver is allowed to start the car; S1 = 1 indicates that it can be started, and S2 = 0 indicates that it cannot be started.
S2 indicates whether to allow the conductor to close the door; S2 = 1 indicates that the conductor is allowed to close the door, and S2 = 0 indicates that the conductor is not allowed to close the door. **

semaphore s1 = 1, s2 = 0;
void driver() {
  do {
    wait(s1);// P(S1)
    开车;
    正常行驶;
    到站停车;
    signal(s2);//V(S2)
  } while (true);
}
void seller() {
  do {
    关门;
    signal(s1);// V(S1)
    售票;
    wait(S2);//P(S2)
    开车门;
    上下乘客;
  } while (true);
}
void main() {
  cobegin
    driver(); seller();
  coend
}

Reference blog: http://m.blog.csdn.net/Y_215/article/details/53467517

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325597330&siteId=291194637