Operating System - The most detailed explanation of classic synchronization issues (Series 2)

reader-writer problem

This article will analyze the reader-writer problem in detail and strive to master it in one article.

Problem Description:

      A data query or record can be shared by multiple processes. We call the process that only reads the file a "reader process" and other processes as "writer processes". Multiple processes are allowed to read a shared object at the same time, but a writer process and other writer processes or reader processes are not allowed to access the shared object at the same time. That is: the synchronization problem of ensuring that a writer process must be mutually exclusive with other processes to access shared objects:

  1. Allows multiple readers to perform read operations simultaneously.
  2. Readers and writers are not allowed to operate at the same time.
  3. Multiple writers are not allowed to operate at the same time.

problem analysis:

       There are two processes in total , namely the writer() process and the read() process. Two mutually exclusive semaphores are also needed to realize mutually exclusive access to the reading process and the writing process respectively. That is, rw represents the mutual exclusion of the reading and writing processes and only Allows one writing process to access the critical section and the re semaphore implementation allows multiple readers to access the critical section.

       However, when the reading process continuously accesses the critical section for operations, it must be ensured that the writing process cannot jump in the queue. Because the reading process can be continuous, in order to prevent the writing process from jumping in the queue before the continuous access of the reading process ends, it is necessary to lock the writing process when the first reading process comes in, until the last reading process leaves the critical section. Release access to the writing process. To implement this step, you need the count semaphore to ensure which reader the current reader is.

     Initialize count=0, which means that when the first reader comes in, count=0; when the second reader comes in, count becomes 1, which does not meet the judgment in the if, and directly skips the if operation, thereby realizing the first The reader performs the latched write in the if operation. The same goes for unlocking write operations.

pseudocode:

//定义信号量
semaphore rw = 1;  //用于实现对缓冲区的互斥访问,只允许一个写进程访问
semaphore rea = 1      //对读进程上锁,即允许其他读进程访问,但不允许写进程访问
int  count = 0;  // 记录当前有几个读进程在访问文件,0代表只有1个读进程
 //写者进程
writer(){
   while(1){
       
       P(rw); //写之前"加锁",实现写操作单一执行。
       写文件;
       V(rw);  //写之后"解锁"
    
          }
      }  
 
//读者进程
reader(){
  while(1){
     p(rea);//进入读进程访问区
 //判断该读进程是否为第一个,是的话则开启对写进程的上锁
       if(count==0){
           P(rw);  //第一个读进程对写进程进行加锁
                    }
          count++;   //读进程的连续进入的计数
      V(rea); //释放对读进程的count++也就是读进程的访问
   
        读文件;
 //接下来进行判断count数量,判断是否读进程是否为最后一个
      P(rea); //  进入读进程访问区     
 //各进程互斥访问count,判断count数量,通过--,判断是否为最后一个数,是的话则释放对写进程的锁
      count--;          // 访问文件的读进程数-1
         if(count == 0) {    
            V(rw); //最后一个读进程负责对写进程解锁
                         }      
      V(rea);//释放读进程
         }
     }

Guess you like

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