Week 6 - Practical Exercise

Aims

Tasks

Exercise 1

Copy the code for the program that uses semaphores based on pipes into your directory. criticalsection2.c Build and run the program.

Exercise 2

Is this implementation of semaphores sufficient to solve the Dining Philosophers problem? Write a program for the dining philosophers using semaphores based on pipe operations. You might use: philosophers.c Build and test it.

Exercise 3

Copy Rochkind’s implementation of semaphores based on UNIX System V semaphore sets sem_ops.h Use these semaphores in the critical section problem of question 1.

Exercise 4

Now change the program so that the critical section is to increment an integer counter, and report the thread id and new counter value.

Exercise 5

Now change the program so that there are two types of threads: Writers, who increment the counter, and Readers who just read it.

Exercise 6

To increase concurrency, we could allow any number of reader threads access at the same time. Writers will still need exclusive access. Write the program - a semaphore solution to the Readers and Writers problem.

Exercise 7

Check that you haven’t left any semaphores around on the system. Remove them if you have.