ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU class(2019-2020-1) > student directories > >
Homework4 Version 0
👤 Author: by 1787576766qqcom 2019-10-09 03:08:09
Reader-writer problem 1. Problem descr iption: a data file or record can be Shared by more than one process. The process that only requires to read the file is called the reader and the other process is called the writer. Multiple reader processes and multiple writer processes operate asynchronously on this file resource over a period of time. The restrictions are as follows: write-write mutex, read-write mutex, read-read allowed

2. Solving the Reader-Writer Problem by Using Recorded Signals

int rmutex = 1, wmutex = 1;
int readcount = 0;
void reader() {
do {
wait(rmutex);//各读者互斥的通过门禁,进入读书室
//第一个读者进入读书室时需要开启禁止写者进入的信号
if (readcount == 0) {
wait(wmutex);
}
readcount++;
signal(rmutex);
perform read operation;//读者阅读,此时进行的读操作无需互斥
wait(rmutex);//互斥的通过门禁,离开图书室
readcount--;
//最后一个读者离开时需要关闭禁止写者进入的信号
if (readcount == 0) {
signal(wmutex);
}
signal(rmutex);
} while (true);
}
void writer() {
do {
wait(wmutex);
perform write operation;
signal(wmutex);
} while (true);
}

Please login to reply. Login

Reversion History

Loading...
No reversions found.