Synchronization mutually exclusive issues such as pv primitives, producer-consumer, etc.

Posted by nebb on Sat, 18 Dec 2021 18:08:16 +0100

1. Description of the problem: A group of producer processes and a group of consumer processes share a buffer with an initial empty size of n. Only when the buffer is not full, the producer can put the message into the buffer, otherwise he must wait; Consumers can only pull messages from a buffer when it is not empty, or they must wait. Since a buffer is a critical resource, it only allows one producer to put a message or one consumer to pull a message from it.

When you see this problem, first analyze its related synchronization and mutually exclusive relationships.

Solution: (1): Since a buffer is a critical resource and only one process is allowed to access it at a time, the producer process and the consumer process are mutually exclusive for the buffer access operation. Therefore, a semaphore mutex = 1 can be obtained.

The pv operations for access to the buffer alone, regardless of other information, are as follows:

          

semaphore mutex = 1;  //Mutually exclusive semaphore, allowing only one process access at a time

producer()  //Producer Process
{
      p(mutex);
      ...
      v(mutex);
      ...
}

consumer() //Consumer Process
{
    p(mutex);
    ...
    v(mutex);
    ...
}

(2): Since only when the buffer is not full (that is, there is still space) can the producer use the buffer and put data into it. From this statement, the concept of sequence is derived:

First: when the buffer is not full, second: the producer can operate the buffer. Thus, this is a synchronization relationship, and only when the current behavior condition is satisfied can the next operation be carried out.

Set an empty semaphore to indicate the free area of the buffer, regardless of the initial value.

      

(3): Since the consumer process can only pull messages from the buffer if it is not empty, this statement tells you that the condition for the buffer to be not empty is met before the next consumer process cancels the message. This is also a synchronization relationship.

You can set a semaphore to full first to indicate the number of buffers used.

(4): Now analyze which process is related to the semaphore empty in the above synchronization relationship?

The first thing to understand is that producers can only perform operations when the buffer is not full, and it can be found that when a consumer process executes an operation to pull data out of the buffer, it will result in an increase in the free area of the buffer, so you know that there should be a V(empty) operation by the consumer to increase the free area.

With more free space, the producer can operate.

(5): Similarly, the consumer's signal full is determined by the producer.

(6): Finally, its general pv operation is as follows.

semaphore mutex = 1;
semaphore empty = n;  //Free space itself has n
semaphore full = 0;   //Whether there is data or not, it needs to be added slowly

producer()
{
   p(empty);  //Determine if there is free space
   p(mutex);   //Mutually exclusive access buffer and lock it
   
   Add data;
   
   v(mutex);   //Release Buffer
   v(full);   // Buffer Data Increase
}

consumer()
{

   p(full);  //Check buffer for data
   p(mutex);  
  
   Take out the data;
   
   v(mutex);
   v(empty);  //Resources increase in idle zone
}

 

2. Description of the problem: There is a plate on the table and only one fruit can be put into it at a time. Dad put apples on the plate, Mom put oranges on the plate, son ate oranges on the plate, and daughter ate apples on the plate. Only when the plate is empty can Mom or Dad put a fruit on it. A son or daughter can only take the fruit out of the plate if it contains the fruit he or she needs.

(1): Mutual exclusion --->Only one person can put a fruit on the plate at a time. mutex = 1;

(2): Synchronization relationship --->When the plate is empty, father or mother can put a fruit on the plate. empty = n;

(3): Synchronization --->When an orange appears, the son can take action. morange = 0;

(4): Synchronization relationship - > When an apple appears, the daughter can take action. fapple = 0;

      

semaphore mutex = 1;
semaphore empty = n;
semaphore fapple = 0;  //When an apple appears, the daughter can operate it
semaphore morange = 0; //When an orange appears, the son can operate it

father() //Dad Process
{
   p(empty);  //Determine if the plate is empty
   p(mutex);   //Mutually exclusive use plate
   
   Put in the apples;
   
   v(mutex);
   v(fapple);  //Indicates that there is an apple on the plate and the daughter's action can be activated

}

mother()  //Mom Progress
{
   p(empty);  
   p(mutex);
   
   Put in the oranges;
  
   v(mutex);
   v(morange);  //Indicates that an orange already exists on the plate and the son's action can be activated

}

son()   //Son Process
{
    p(morange);  //Check for oranges
    
    Take oranges;
   
    v(empty);  //The orange was taken and the plate became more free
    
    Eat oranges;
 }

daughter() //Daughter Progress
{
   p(fapple);
   
   Take apples;
  
   v(empty);  //The Apple was taken and the plate was idle
   
   Eat apples;
}


For the empty semaphore above, i.e. the plate is empty, I set it to n to indicate that a plate can hold more than one fruit.

And the above round-robin operation has not been written for the purpose of simplicity and intuition. You need to add while(1) loops yourself.

 

Topics: Operating System