CMPS-3600 Exam 1
Readers and writer programming with semaphores. The C program below is from our textbook. 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/exam1.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 at one time? 6. How many readers can read at one time? 7. What does it mean that semaphore x starts with a value of 1?
Tips: Write brief answers. Check your spelling. A semaphore can enforce mutual exclusion on a resource or a section of code. Please be more specific with your answers. Thanks.