Example #1
0
void phil_entry(void)
{
	int counter;
	struct k_sem *f1;       /* fork #1 */
	struct k_sem *f2;       /* fork #2 */
	static int myId;        /* next philosopher ID */
	int pri = irq_lock();   /* interrupt lock level */
	int id = myId++;        /* current philosopher ID */

	irq_unlock(pri);

	/* always take the lowest fork first */
	if ((id + 1) != N_PHILOSOPHERS) {
		f1 = FORK(id);
		f2 = FORK(id + 1);
	} else {
		f1 = FORK(0);
		f2 = FORK(id);
	}

	for (counter = 0; counter < 5; counter++) {
		TAKE(f1);
		TAKE(f2);

		RANDDELAY(id);

		GIVE(f2);
		GIVE(f1);

		RANDDELAY(id);
	}
}
Example #2
0
void philEntry(void)
{
#ifdef CONFIG_NANOKERNEL
    struct nano_sem *f1;	/* fork #1 */
    struct nano_sem *f2;	/* fork #2 */
#else
    kmutex_t f1;		/* fork #1 */
    kmutex_t f2;		/* fork #2 */
#endif
    static int myId;        /* next philosopher ID */
    int pri = irq_lock();   /* interrupt lock level */
    int id = myId++;        /* current philosopher ID */

    irq_unlock(pri);

    /* always take the lowest fork first */
    if ((id+1) != N_PHILOSOPHERS) {
        f1 = FORK(id);
        f2 = FORK(id + 1);
    } else {
        f1 = FORK(0);
        f2 = FORK(id);
    }

    while (1) {
        TAKE(f1);
        TAKE(f2);

        PRINT(id, "EATING  ");
        RANDDELAY(id);

        GIVE(f2);
        GIVE(f1);

        PRINT(id, "THINKING");
        RANDDELAY(id);
    }
}