Readers and writer programming with semaphores.
The C program below is from our textbook in a section called Readers Have Priority.
Chapter-5 pg. 271 in my textbook.
Page 243 in the 9th Edition hard-bound textbook.
Look at the program and answer the questions below.
The semaphores are similar to POSIX.
semWait() means to wait for sem to be greater than zero, then decrement.
semSignal() means increment the semaphore.
int readcount = 0;
semaphore x = 1;
semaphore wsem = 1;
void reader()
{
while (true) {
semWait(x);
readcount++;
if (readcount == 1)
semWait(wsem);
semSignal(x);
READUNIT();
semWait(x);
readcount--;
if (readcount == 0)
semSignal(wsem);
semSignal(x);
}
}
void writer()
{
while (true) {
semWait(wsem);
WRITEUNIT();
semSignal(wsem);
}
}
void main()
{
readcount = 0;
parbegin(reader, writer);
}
Assignment:
Create a file on Odin named:
3600/9/quiz9.txt
In your text file, answer the following questions.
1. What is the purpose of semaphore x?
2. What is the purpose of semaphore wsem?
3. Which reader waits for wsem to be available?
4. Which reader signals the writer to continue writing?
5. How many writers can write concurrently?
6. How many readers are likely reading concurrently?
7. What does it mean that semaphore x starts with a value of 1?
8. What problem can happen with this reader/writer strategy?
The textbook will help you answer this question.
Write brief answers.
Check your spelling.
Thanks.