void * philosopher (void *num) { int id; int i, left_chopstick, right_chopstick, f; id = (intptr_t)num; printf ("Philosopher %d is done thinking and now ready to eat.\n", id); right_chopstick = id; left_chopstick = id + 1; /* 箸が一巡した */ if (left_chopstick == PHILOS) left_chopstick = 0; while (f = food_on_table ()) { get_token (); grab_chopstick (id, right_chopstick, "right "); grab_chopstick (id, left_chopstick, "left"); printf ("Philosopher %d: eating.\n", id); usleep (DELAY * (FOOD - f + 1)); down_chopsticks (left_chopstick, right_chopstick); return_token (); } printf ("Philosopher %d is done eating.\n", id); return (NULL); }
void * philosopher (void *num) { int id; int left_fork, right_fork, f; id = (int)num; printf ("Philosopher %d sitting down to dinner.\n", id); right_fork = id; left_fork = id + 1; /* Wrap around the forks - somewhat more complicated */ if (left_fork == PHILO) { left_fork = right_fork; right_fork=0; } while (f = food_on_table ()) { printf ("Philosopher %d: get dish %d.\n", id, f); get_fork (id, right_fork, "right"); get_fork (id, left_fork, "left"); printf ("Philosopher %d: eating.\n", id); usleep (DELAY * (FOOD - f + 1)); down_forks (left_fork, right_fork); } printf ("Philosopher %d is done eating.\n", id); return (NULL); }
void * philosopher (void *num) { int id; int i, left_chopstick, right_chopstick, f; id = (intptr_t)num; printf ("Philosopher %d is done thinking and now ready to eat.\n", id); right_chopstick = id; left_chopstick = id + 1; /* Wrap around the chopsticks. */ if (left_chopstick == PHILOS) left_chopstick = 0; while (f = food_on_table ()) { f = food_on_table (); if (id == 1) sleep (sleep_seconds); grab_chopstick (id, right_chopstick, "right "); grab_chopstick (id, left_chopstick, "left"); printf ("Philosopher %d: eating.\n", id); usleep (DELAY * (FOOD - f + 1)); /*i = rand()%9999; while (--i);*/ //gtthread_yield(); //usleep (DELAY); down_chopsticks (left_chopstick, right_chopstick); } printf ("Philosopher %d is done eating.\n", id); return (NULL); }