Operating system - the most detailed explanation of classic synchronization problems (series 4) real test exercises

After reading my first three classic question types about process synchronization, let’s start practicing some real questions!

1. Problems with storing and retrieving parts in the warehouse

Problem Description:

       A factory has two production workshops and one assembly workshop. The two production workshops produce two types of parts, A and B respectively. The task of the assembly workshop is to assemble A and B. Parts are assembled into products. After each part is produced in the two production workshops, they must be sent to shelves F and F2 in the dedicated workshop. F1 stores part A, and F2 stores part B. Both F1 and F2 have the capacity to store 10 parts. The assembly worker takes one part A and one part B from the shelf at a time and assembles it into a product. Please use P and V operations for correct management.

problem analysis:

       There are two production workshops and one assembly workshop, so there are three processes in total. Because it is said that 10 or (n) parts can be stored, a specific number is given, and two semaphores, empty and full, are used. Only one shelf F1 and F2 can be taken at a time, so mutual exclusion semaphores mutex1 and mutex2 are required. But during the assembly workshop, parts are also taken from the F1 and F2 warehouses, so there is no need to write a mutually exclusive semaphore. When entering the F1 and F2 warehouses, only the warehouse's own mutex1 and mutex2 are needed.

pseudocode:

//定义信号量
semaphore  mutex_1=mutex_2=1;
semaphore  empty_1=empty_2=10;
semaphore  full_1=full_2=0;
//A车间的工作过程可描述为:
       product_A(){
           while(1){
                  生产一个产品 A;
                  P(empty_l);     //判断货架F1是否有空 
                  P(mutex_l);     //互斥访问货架F1 
                  将产品A存放到货架F1上;
                  V(mutex_l);     //释放货架F1 
                  V(full_l);      //货架F1上的零件A的个数加1 
                }  
                   }
//B车间的工作过程可描述为:
        product_B(){
             while(1){
                  生产一个产品 B;
                  P(empty_2);     //判断货架F2是否有空
                  P(mutex_2);     //互斥访问货架F2 
                   将产品B存放到货架F2上;
                  V(mutex_2);     //释放货架F2 
                  V(full_2);      //货架F2上的零件B的个数加1 
                 }
                    }
//装配车间的工作过程可描述为:
         assemble(){
              while(1){
                  P(full_1);      //判断货架F1上是否有产品A 
                  P(mutex_1);     //互斥访问货架F1 
                   从货架F1上取一个A产品;
                  V(mutex_l);     //释放货架F1 
                  V(empty_l);     //货架F1上的空闲空间数加 
                  P(ful1_2);      //判断货架F2上是否有品 
                  P(mutex_2);     //互斥访问货架F2 
                   从货架F2上取一个B产品;
                  V(mutex_2);     //释放货架F2 
                  V(empty_2);     //货架F2上的空闲空间数加1 
                    将取得的A产品和B产品组装成产品;
                       }
                    }

2. The problem of water collection by old monks and young monks

Problem Description:

      There are several young monks and old monks in a certain temple. There is a water tank. The young monk brings water into the tank for the old monk to drink. The water tank can hold
10 buckets of water, and the water is taken from the same well. The diameter of the well is narrow and can only accommodate one bucket at a time. The total number of buckets is 3. Only 1 bucket of water can be taken into the tank each time, and cannot be done at the same time. Try to give a description of the algorithm for getting water from the tank and entering the water.

problem analysis:

   Based on the meaning of the question, we learned that a total of water tanks (vat) and wells ( are needed) well), bucket (pail) three information quantities are the little monk fetching water (the premise is that the water in the tank is less than 10 buckets, and the bucket is fetched into the well water) and drink water with the old monk (prerequisite: there is water in the tank, you can use a bucket to lift water from the tank to drink). Two processes,

So first the code definition:

 Vat=1;//The old monks mutually exclude access to the vat, which is equivalent to the m. (In response to the question "Only 1 bucket of water is taken into the tank each time")

Well=1;//The young monks are mutually exclusive from visiting the well (corresponding to the question "The diameter of the well is narrow and only one bucket is allowed to draw water at a time")

Pail=3;//Indicates the number of buckets(The total number of buckets in the anaphora question is 3)

    So what is the critical space resource? From the question, we can find that both the old monk and the young monk will use the water tank, and the water in the bucket will eventually be poured into the tank, so the critical resource is the tank. In fact, there is another place where you can confirm that the tank is a critical resource. The question states that the water tank can hold 10 barrels of water (usually the space capacity given is our critical resource). After confirming the critical resources, you need to confirm how much water is in the tank and whether it is full or not. Then you need to use empty and full Two semaphores for confirmation.

pseudocode:

//定义信号量
semaphore well=1;	//用于互斥地访问水井	
semaphore vat=1;	//用于互斥地访问水缸	
semaphore empty=10;	//用于表示水缸中剩余空间能容纳的水的桶数	
semaphore full=0;	//表示水缸中的水的桶数	
semaphore pail=3;	//表示有多少个水桶可以用,初值为3	

//小和尚 
        while(1){
             P(empty);   //先判断水缸是否可以再添水
             P(pail);    //可以添水则拿起桶,互斥访问桶
             P(well);    //互斥访问井
            从井中打一桶水; 
            V(well);     //打完水释放井
            P(vat);      //将水倒进缸中,互斥访问缸
            将水倒入水缸中; 
            V(vat);      //倒完睡,离开缸
            V(full);     //此时缸中水加一桶
            V(pail);     //释放桶
               }
//老和尚 
       while(l){
             P(full);    //先判断缸中是否有水可以饮用
             P(pail);    //拿桶,互斥访问桶
             P(vat);     //使用水桶往缸中取水
             从水缸中打一桶水; 
             V(vat);     //打完水后,释放对缸互斥
             v(empty);   //此时缸中的水减少一桶的量
             喝水;
             V(pail);    //喝完水后释放桶
                }

3. Museum visit issues

Problem Description:

     A certain museum can accommodate up to 500 people visiting at the same time. It has one entrance and exit, and only one person is allowed to pass through this entrance and exit at a time
. The visitor's activities are described as follows: cobegin
visitor process i:{ ... Enter the door. visit;... go out;



coend
Please add the necessary semaphores and P, V [or wait(), signal()] operations to achieve mutual exclusion and synchronization in the above process. It is required to write out the complete process, explain the meaning of the semaphore and assign an initial value.

problem analysis:

     Only one person is allowed to pass through the entrance and exit at a time, and the mutex semaphore mutex is set with an initial value of 1. The museum can accommodate up to 500 people at the same time, so set the semaphore empty with an initial value of 500.

   Think of the door as a critical resource, then you need to access this critical resource when entering and leaving, so two pairs of P and VCC operations are required.

pseudocode:

//定义信号量
Semaphore empty=500; //博物馆可以容纳的最多人数 
Semaphore mutex=l; //用于出入口资源的控制 
cobegin
参观者进程i:
    {
***
           P(empty); //判断是否还可以进人 
           P(mutex); //可以则进入,互斥使用门 
             进门;
           V(mutex);//进入以后则释放对门的访问
              参观;
           P(mutex); //参观完离开,互斥使用门 
             出门;
           V(mutex);//离开博物馆,释放对门的访问
           V(empty);' //可容纳人数增1 
...
       }
coend

I hope this exercise can give you a deeper understanding and understanding of process synchronization issues.

Guess you like

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