作業系統›Ch6 行程同步(Process Synchronization)第 20 題/共 32 題
20. Readers-Writers Problem、Semaphore
#OS-06-020易Readers-Writers ProblemSemaphore
題組題幹(本題:第21題,共 4 小題)點擊展開
Consider using semaphores for the first readers-writers problem, which allows either one writer or at most readers in the critical session at the same time (no reader be kept waiting unless a writer has already obtained permission to use the shared object). We use an integer read_count to count the number of readers that are currently reading the data. We use two binary semaphores: rw_mutex and mutex. The former is common to both reader and writer processes. The mutex semaphore is used to ensure mutual exclusion when the variable read_count is updated. The reader's code is shown below.
sem_wait(&①);
read_count++;
if (read_count == ②)
sem_wait(&③);
sem_post(&④);
(read the data)
sem_wait(&⑤);
read_count--;
if (read_count == ⑥)
sem_post(&⑦);
sem_post(&⑧);
What is the initial value of mutex?
📄 交大111
▤完整推導請見《WH 資工筆記 · 作業系統》Ch6 行程同步(Process Synchronization)