// EPOS Semaphore Abstraction Test Program #include #include #include #include #include #include __USING_SYS const int iterations = 10; Mutex table; Thread * phil[5]; Semaphore * chopstick[5]; OStream cout; int philosopher(int n, int l, int c) { int first = (n < 4)? n : 0; int second = (n < 4)? n + 1 : 4; for(int i = iterations; i > 0; i--) { table.lock(); Display::position(l, c); cout << "thinking"; table.unlock(); Delay thinking(2000000); chopstick[first]->p(); // get first chopstick chopstick[second]->p(); // get second chopstick table.lock(); Display::position(l, c); cout << " eating "; table.unlock(); Delay eating(1000000); chopstick[first]->v(); // release first chopstick chopstick[second]->v(); // release second chopstick } table.lock(); Display::position(l, c); cout << " done "; table.unlock(); return(iterations); } int main() { table.lock(); Display::clear(); Display::position(0, 0); cout << "The Philosopher's Dinner:\n"; for(int i = 0; i < 5; i++) chopstick[i] = new Semaphore; phil[0] = new Thread(&philosopher, 0, 5, 32); phil[1] = new Thread(&philosopher, 1, 10, 44); phil[2] = new Thread(&philosopher, 2, 16, 39); phil[3] = new Thread(&philosopher, 3, 16, 24); phil[4] = new Thread(&philosopher, 4, 10, 20); cout << "Philosophers are alive and hungry!\n"; Display::position(7, 44); cout << '/'; Display::position(13, 44); cout << '\\'; Display::position(16, 35); cout << '|'; Display::position(13, 27); cout << '/'; Display::position(7, 27); cout << '\\'; Display::position(19, 0); cout << "The dinner is served ...\n"; table.unlock(); for(int i = 0; i < 5; i++) { int ret = phil[i]->join(); table.lock(); Display::position(20 + i, 0); cout << "Philosopher " << i << " ate " << ret << " times \n"; table.unlock(); } for(int i = 0; i < 5; i++) delete chopstick[i]; for(int i = 0; i < 5; i++) delete phil[i]; cout << "The end!\n"; return 0; }