作業系統›Ch6 行程同步(Process Synchronization)
第 22 題/共 32 題
◀ OS 22/32
22. Readers-Writers Problem、Semaphore
#OS-06-022易Readers-Writers ProblemSemaphore
題組題幹(本題:第23題,共 4 小題)點擊展開

Consider using semaphores for the first readers-writers problem, which allows either one writer or at most nn 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 should be in ②?

📄 交大111
跳轉到第題
▤完整推導請見《WH 資工筆記 · 作業系統》Ch6 行程同步(Process Synchronization)
本章題號 · 21–32 / 32