Beispiel #1
0
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);
}
Beispiel #2
0
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);
}