This could be an exercise on Application-Orientation, but indeed it's only an invitation for students to compare the implementation of Dijkstra's classic process coordination problem "The Dining-Philosophers" for some operating systems.
1. |
#include <iostream> |
Line 8 creates five Semaphore objects, which are initialized by the default constructor to "1".
Lines 10-23 describe the philosophers' live: thinking and eating. In order to eat, each philosopher must take both nearby chopsticks. Philosophers 0 through 3 take first the chopstick to their left and then the one to their right. Philosopher 4 does do opposite, thus avoiding deadlocks.
Line 29 declares an array of pointers to Threads objects, each of which corresponds to a philosopher. Lines 30-31 create a thread for each philosopher, setting function philosopher as entry point and passing the creation order as argument.
1. |
#include <unistd.h> |
1. |
#include <unistd.h> |