18cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
28cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include <pthread.h>
38cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include <stdlib.h>
48cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com#include <unistd.h>
58cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
68cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com/* Naive dining philosophers with inconsistent lock acquisition
78cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   ordering. */
88cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
98cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comstatic pthread_t phil[5];
108cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comstatic pthread_mutex_t chop[5];
118cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
128cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comvoid* dine ( void* arg )
138cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com{
148cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   int i;
158cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   long left = (long)arg;
168cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   long right = (left + 1) % 5;
178cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   for (i = 0; i < 1000/*arbitrary*/; i++) {
188cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pthread_mutex_lock(&chop[left]);
198cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pthread_mutex_lock(&chop[right]);
208cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      /* eating */
218cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pthread_mutex_unlock(&chop[left]);
228cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pthread_mutex_unlock(&chop[right]);
238cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   }
248cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   return NULL;
258cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
268cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
278cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.comint main ( void )
288cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com{
298cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   long i;
308cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   for (i = 0; i < 5; i++)
318cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pthread_mutex_init( &chop[i], NULL);
328cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
338cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   for (i = 0; i < 5; i++)
348cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pthread_create(&phil[i], NULL, dine, (void*)i );
358cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
368cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   sleep(1);
378cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
388cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   for (i = 0; i < 5; i++)
398cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com      pthread_join(phil[i], NULL);
408cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com
418cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com   return 0;
428cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com}
438cee797901763ab0922eb9ef484cfdcbc94bee54edisonn@google.com