Operating system process management - readers to write the problem

Readers write the problem

Mutual exclusion synchronization of access to limited resources

Question: allows multiple processes to read files at once, but if there is a process to write a file, then it does not allow other processes to write or read the file

Readers priority

Readers priority is only when all the readers to read the file operation is completed, in order to allow access to the file write process

Implementation:
  • readcount: Readers counter, the initial value of 0
  • rmutex: initial value of 1, because the reader counter for multiple readers shared resources, is a critical resource
  • wmutex: initial value of 1, mutex, on behalf of a shared file
Fake code:
void reader(void)
{
	while(true)
    {
        p(rmutex); //抢占读者计算器这个资源
        readercount++; //读者数量加1
        //如果是第一个读者,要执行p操作抢占文件,为什么要抢占文件,就要先抢占,防止有写者进行写(读者优先),如果不是第一个读者,则表示已经有读者在安全地读这个文件
        if (readercount == 1)
            p(wmutex);  //抢占共享文件资源
        v(rmutex); //释放读者计算器
        
        read_file(); //读文件
        
        p(rmutex); //读完文件后,读者数量减少1,所以需要再次抢占读者计算器这个资源,然后进行修改
        readcount--;//读者数量减1
        if (readcount == 0) 
            //如果是最后一个读者,需要释放“共享文件”这个资源,这样才能让写者进程进行写
            v(wmutex); //释放文件资源
        v(rmutex);//释放读者计数器
            
    }
}


void write(void)
{
    while(true)
    {
        p(wmutex); //抢占文件资源
        
        write_file(); //执行写文件操作
        
        v(wmutex); //释放文件资源
    }
}

Write a plus

Only operate when the reading process until all the write process is completed

  • Readers and writers, writers and writers can not access the buffer
  • When there is no writer process, multiple processes can access the buffer simultaneously readers
  • When the reader and writer processes while waiting for the process, the process priority to readers who write process, that is, all readers should be established in the process of carrying out activities under the premise of no writer process
Implementation:
  • wcount: represents the number of writers, the initial value 0
  • rcount: represents the number of readers, the initial value 0
  • rmutex: exclusive modified readership, the initial value of 1
  • wmutex: exclusive writer to modify the number, the initial value of 1
  • file: exclusive access to the file, the initial value of 1
  • read: Readers blocking the process, write a plus, at least when there is a write file operation process when the process is necessary to read blocking
Attention to the problem:

When a reader queue is not 0, the writer process will wait for all of these processes the reader to read the file and then released after the resource file, write to file before the operation, the reader is assumed that when the process is performed for a long time, then there are multiple queue readers, that is rcount> = 1, then it's time to write the application process to write a file resource, then all the readers in before this time he had to wait for readers to queue when you're done for the job , as for preferred writing operation is reflected in writing when the latter process to apply a file resource, a reader process 1 after he files the application resources, then this process can only be routed behind the readers who write process, and unfortunately this time again a writer process 2, then the reader process 1 can only be squeezed out, came back in 2 writer process, which is at this time for the process queue writer process 1, process 2 writers, readers process 1 .. . . Readers can have multiple processes are, that is, after the reader application process may be slower than the release of his readers apply for another process, so the process can generate more readers

Fake code:
void reader()
{
    while(true)
    {
        p(read);//当前写者队列为空的时候,就可以申请读文件资源
        p(rmutex); //抢占读者计数器
        if(rcount == 0)  //如果是第一个读者,那么就抢占文件资源,这样就可以保证读文件与写文件互斥
            p(file);  //抢占文件资源
        rcount++; //读者数量+1
        v(rmutex); //释放读者计数器
        v(read);  //这一步比较关键,如果不释放的话,那么你每次只有一个读者进程,而且写进程也不能抢占这个资源,就会发生死锁
        //而且这也是写进程抢占写文件的时机,如果写进程抢占到这个read,他就会等到之前所有的读进程完成后释放文件,然后抢占文件,进而就可以开始写进程,这也就是写者优先
          
        read_file(); //读取文件
        
        p(rmutex);//抢占读者计数器
        rcount--;
        if (rcount == 0)  //如果是最后一个读者进程,释放文件资源
            v(file);
        v(rmutex); //释放读者计数器              
    }
}

void writer(void)
{
    while(true)
    {
        p(wmutex); //抢占写者计数器
        if (wcount == 0) //如果当前写者队列为空,申请优先资源
            p(read)
         wcount++; //写者数量加1
        v(wmutex); //释放写者计数器
        
        p(file); //申请文件资源,这时候写者会等待在他进程之前的所有读者进程完成后释放文件
        
        write_file(); //写文件
        
        v(file); //释放文件
        
        p(wmutex); //抢占写者计数器
        wcount--; // 写者数量减1
        if (wcount == 0) //如果是最后一个写者
            v(read); //释放优先资源
        v(wmutex); //释放写者计数器  
    }
}

Readers who write (read and write fair) - first come, first served

Implementation
  • The initial value of the semaphore file 1, for implementing a mutex // file operation
  • readcount = 0; // record the number of readers
  • Semaphore readmutex initial value 1, exclusive access to readcount
  • Fair amount of read and write signals s
Fake code
void reader()
{
    while (1)
    {
        p(s);
        p(readmutex);
        if (readcount == 0)
            p(file);
        readcount++;
        v(readmutex);
        v(s);
        读文件
        p(readmutex);
        if (readcount==0)
            v(file);
        v(readmutex);
    }
}

void writer()
{
    while (1)
    {
        p(s);
        p(file);
        写文件;
        v(file);
        v(s);
    }
}
Testing concurrency
  • Audience 1 - Reader 2
  • Write 1 - Write 2
  • Reading 1 - Write 1
  • Write 1 - Read 1
  • 1 Read - Write 1- Reading 2
Published 29 original articles · won praise 6 · views 461

Guess you like

Origin blog.csdn.net/weixin_42100456/article/details/103727807
Recommended