读者-写者问题1--读者优先

读者优先是最简单的情况,因此不做过多阐述

//读者优先
int rcount = 0;
semaphore file;   //用于读者和写者互斥访问
semaphore rmutex; //用于读者进程互斥修改rcount 

writer() 
{
    P(file);
    do_writing;
    V(file);
}

reader()
{
    P(rmutex);
    if(rcount == 0)
        P(file);
    rcount++;
    V(rmutex);
    
    do_reading;
    
    P(rmutex);
    rcount--;
    if(rcount == 0)
        V(file);
    V(rmutex);
}

猜你喜欢

转载自blog.csdn.net/mch2869253130/article/details/84319048