Operating system - the most detailed explanation of classic synchronization issues (Series 1)

Table of contents:

1. Producer and consumer issues

(1) Single producer and consumer

(2) Multiple producers and consumers

(3) Practice with real questions


       Producer and consumer issues are often the focus of exams, so mastering this issue is critical. This article will focus on typical producer and consumer problems from simple to difficult, from easy to deep. Finally, we provide several postgraduate entrance examination real questions to practice in order to fully master this type of questions.

(1) Single producer and consumer

Problem Description:

There is a set of producer processes and a set of consumer processes. The producer process produces one product and puts it into the buffer each time; the consumer process takes out one product from the bounded buffer and uses it each time. The producer and consumer share an initially empty buffer of size n.

problem analysis:

      This problem requires two processes , namely the producer and consumer processes, and a mutually exclusive semaphore mutex (buffer), whose mute=1 means that only one process is allowed to enter the buffer; two resource semaphores: not started Before production , empty=n , full=0 means that a total of n products can be stored, but currently there are only 0.

pseudocode:

    
semaphore mutex=1,empty=n,ful1=0; //初始化信号量 
//生产者进程
void producer(){
 do {
    生产者生产产品;
    P(empty)   //判断缓冲区是否为空 
    P(mutex);   //判断是否可以进入缓冲区,空则进入
    将产品放入指定区域;
    V(mutex);   //退出临界区,允许别的进程进入 
   V(full);     //缓冲池中非空的缓冲区数量加1,同时唤醒等待的消费者进程
   }while(true);
                }
//消费者进程
void consumer(){
 do{
    P(full);  //判断缓冲区是否非空(是否有产品)
    P(mutex);  //判断是否可以进入缓冲区(非空则进入)
    取走产品;
    V(mutex);   //退出临界区,允许别的进程进入
    V(empty);  //缓冲池中空缓冲区数量加1,可以唤醒等待的生产者进程
   }
   使用产品;
              }

       Through the above pseudocode, it can be found that the P (mutex) and V (mutex) operations used to achieve mutual exclusion in each process must appear in pairs and in the same process; note: each program The order of multiple P and V operations in the process cannot be reversed. For example, in the producer process, you should first check whether the resource semaphore is empty and perform the P operation –P (empty); then perform the P operation into the buffer –P (mutex), otherwise a deadlock may occur due to mutual exclusion, that is, entering the buffer pool but there is no free buffer (empty \neq0), the producer process will be blocked, and other processes will not be able to enter the critical section, thus Causes the process to deadlock. 

(2) Multi-producer-consumer problem
Problem description
     There is a plate on the table, and only one fruit can be put into it at a time. The father only puts apples on the plate, and the mother only puts oranges on the plate. The son is waiting to eat the oranges on the plate, and the daughter is waiting to eat the apples on the plate. Only when the plate is empty, parents can put a piece of fruit on the plate. Only when there is the fruit they need, the son or daughter can take out the fruit from the plate.
problem analysis

       The plate is a buffer and only one process is allowed to enter, so a mutex semaphore is needed . When the plate is empty, the father can enter the buffer zone and put apples on the plate, or the mother can enter the buffer zone and put oranges on it; if there is something in the plate, and the son finds that it is not empty and the plate is filled with oranges, he will enter the buffer zone. The district takes the orange and eats it. If it is an apple, the daughter picks it up and eats it. There are four processes in total . At this time, three resource semaphores, apple  and orange , and plate are needed to indicate whether there is something on the plate, what the thing is, and then determine whether the buffer can be accessed.

pseudocode:

//定义信号量
semaphore mutex = 1;  //实现互斥访问盘子(缓冲区)
semaphore apple = 0;  //盘子中有几个苹果
semaphore orange = 0;  //盘子中有几个橘子
semaphore plate = 1;   //盘子中还可以放多少个水果
  //进程一:父亲
dad(){
   while(1){
  准备一个苹果;
  P(plate);//看盘子里是否可以放水果
  P(mutex);//进入临界区
        把苹果放入盘子;
  V(mutex);//退出临界区同时唤醒女儿进行拿苹果
  V(apple);//苹果数量+1
     }
}
  //进程二:母亲
mom(){
   while(1){
  准备一个橘子;
  P(plate); //看盘子里是否可以放水果
  P(mutex); //进入临界区
        把橘子放入盘子;
  V(mutex); //退出临界区同时唤醒儿子进行拿橘子
  V(orange);//橘子数量+1
     }
}
 //进程三:女儿
daughter(){
   while(1){
 
  P(apple);//判断是否有苹果可以取
  P(mutex);//进入临界区,互斥访问
        从盘子中取出苹果;
  V(mutex); //退出临界区
  V(plate);//盘子里水果数量-1
  吃苹果;
     }
}
  //进程四:儿子

son(){
   while(1){
  
  P(orange); //判断是否有橘子可以取
  P(mutex); //进入临界区,互斥访问
        从盘子中取出橘子;
  V(mutex); //退出临界区
  V(plate); //盘子里水果数量-1
  吃掉橘子;
     }
}

(3) Practice questions

Problem Description

Two products, A and B, can be stored in a warehouse. The requirements are:
① Only one product can be stored at a time.
②The quantity of product A-the quantity of product B<M. ③The quantity of product B - the quantity of product A <N.
Among them, M and N are positive integers. Use P operation and V operation to describe the warehousing process of product A and product B.

problem analysis

     Use the semaphore mutex to control the mutual exclusive access of two processes to critical resources (warehouses), and use the synchronization semaphores Sa and Sb (respectively representing the difference between the quantities that can be accommodated between products A and B, and the quantities that can be accommodated between products B and A. Poor) satisfies condition 2 and condition 3.

       For ②, let the quantity of product B = 0, and the quantity of product is a positive integer, then the quantity of product A = M-1; similarly, let the quantity of product A = 0, then the quantity of product B = N-1.

      There are two processes in total , Sa stores the process (process_A) and Sb stores the process (process_B); three semaphores Sa , Sb and mutex .

code show as below:

//定义信号量
Semaphore Sa=M-1,Sb=N-1;
Semaphore mutex=l; //访问仓库的互斥信号量 
//进程一
process_A(){ 
while(l){
   P(Sa); //看Sa是否还可以存入
   P(mutex); //Sa数量小于M-1,则进入互斥缓冲区
    A产品入库;
    V(mutex);//退出缓冲区
    V(Sb);  //唤醒进程b
       }
           }
    
//进程二
process_B(){ 
 while(1){ 
   P(Sb); //看Sb是否还可以存入
   P(mutex); //Sb数量小于N-1,则进入互斥缓冲区
   B产品入库; 
   V(mutex); //退出缓冲区
   V(Sa);    //唤醒进程a
         }
           }

(4) Real question practice

Problem Description

      The baker has a lot of bread, which is sold by n salespeople. Each customer takes a number after entering the store and waits for the number to be called.
When a salesperson is free, the next number is called. Try to design an algorithm that synchronizes salespeople and customers.

problem analysis

    There are two processes in total , the customer's number-taking process (Consumer) and the salesperson's number-calling process (Seller). Four semaphores ,  i, j, mutex_i, mutex_j ; why choose two mutually exclusive semaphores instead of one? It is because the customer taking the number and the salesperson calling the number require two values ​​​​i and j, and the increase of the two values They cannot be in a buffer and are independent of each other. Otherwise, the corresponding value cannot be increased correctly. I understand it this way : taking a number and calling a number are two different procedures. For example, taking a number uses a machine to take a number, while calling a number requires a waiter to call the number through another machine or manually, using different machines or processes. Therefore, different mutually exclusive semaphores need to be distinguished.

code show as below:

//定义信号量
int i=0,j=0;//i表示顾客取号值,j表示销售者叫号值,都初始化为0
semaphore mutex_i=1,mutex_j=1;
//顾客进程
Consumer(){
首先顾客进入面包店;
P(mutex_i);//进入取号临界资源区,互斥访问i
进行取号i;
i++;//号加1
V(mutex_i);//取完号离开取号资源区
等待叫号i并购买面包;
}
//销售人员进程
Seller(){
(while(1){
P(mutex_j);//销售者进去交好资源区,互斥访问
if(j<i)
{
叫号j;
j++;//叫完号后j数量加1
V(mutex_j);//离开叫号资源区
销售面包;
else{
V(mutex_j);//暂时没有顾客,离开叫号区
休息片刻;
}
  }
}

(5) Real question practice

Problem Description

There are multiple producer processes and multiple consumer processes in the system, sharing a ring buffer (initially empty) that can store 100 products. When the buffer is not full, the producer process can put a product it produces, otherwise wait; when the buffer is not empty, the consumer process can take a product from the buffer, otherwise wait. A consumer process is required to take out 10 products from the buffer before other consumer processes can take the products. Please use semaphore P, V (wait(), signal()) operations to achieve mutual exclusion and synchronization between processes. It is required to write out the complete process and explain the meaning and initial value of the semaphore used.

problem analysis

Two processes , producer (Producer) and consumer (Costumer); two resource semaphores empty=100 , which represents a total of 100 product vacancies, full=0 , which represents whether there are products in the buffer, and if so, it is OK Take away consumption; two mutually exclusive semaphores mutex_1=1 (indicates that only one process can access the buffer), mutex_2=1 (indicates that the consumer can enter the buffer to get 10 products in a row, and no other products can be obtained during the continuous acquisition process. process interference),

code show as below:
 

//定义信号量 
   Semapher mutex=1,mutex_2=1;
   Semapher empty=100,full=0;
//生产者进程:
Producer()
{
  While(1)
  {
     P(empty);//判断缓冲区是否为空,空的话则生产产品
     P(mutex_1);//进入临界区生产
     生产产品;
     V(mutex_1);//离开临界区
     V(full);//非空数量+1
  }
}
//消费者进程
Costumer()
{
  While(1)
  {
     P(mutex_2);//阻止其他消费者进入
     for(i=0,i<10,i++)
    {
       P(full);//判断是否有产品可以取
       P(mutex_1);//先进入临界区
        取产品;
       V(mutex_1);//进入临界资源
       V(empty);//空数+1
    }
       V(mutex_2);

   }
}

This producer-consumer issue has been written so much for now, everyone must master it! If you have any questions, you can comment or message me privately.

For the other two classic synchronization process problems, you can read my other articles, and there is also a separate exercise on the real questions. You can take a look, and the editor will also give a detailed analysis.

Welcome everyone to correct me~~

Guess you like

Origin blog.csdn.net/m0_68403626/article/details/133492991