//*************************************************************** // From the book "Win32 System Services: The Heart of Windows 98 // and Windows 2000" // by Marshall Brain // Published by Prentice Hall // // Copyright 1995, by Prentice Hall. // // This code demonstrates the dining philosophers problem that // can fail due to deadlock. //*************************************************************** // dinphil.cpp #include #include #include const INT numPhilosophers=3; HANDLE chopsticks[numPhilosophers]; CHAR csStat[numPhilosophers+1]; CHAR pStat[numPhilosophers+1]; void PhilosopherThread(INT id) { while (1) { //thinking Sleep(GetTickCount() % 50); // hungry, so picks up chopsticks WaitForSingleObject(chopsticks[id], INFINITE); csStat[id]='1'; WaitForSingleObject( chopsticks[(id+1)%numPhilosophers], INFINITE); csStat[(id+1)%numPhilosophers]='1'; pStat[id]='1'; // eating cout << csStat << " " << pStat << endl; Sleep(GetTickCount() % 50); pStat[id]='0'; // done eating ReleaseMutex(chopsticks[id]); csStat[id]='0'; ReleaseMutex( chopsticks[(id+1)%numPhilosophers]); csStat[(id+1)%numPhilosophers]='0'; } } void main(void) { HANDLE handles[numPhilosophers]; DWORD threadID; INT i; csStat[numPhilosophers]='\0'; pStat[numPhilosophers]='\0'; // Create the chopstick mutexes for (i=0; i