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); } }
void fork_manager_entry(void) { int i; #ifdef CONFIG_NANOKERNEL /* externs */ extern struct nano_sem forks[N_PHILOSOPHERS]; #else /* ! CONFIG_NANOKERNEL */ kmutex_t forks[] = {forkMutex0, forkMutex1, forkMutex2, forkMutex3, forkMutex4, forkMutex5}; #endif /* CONFIG_NANOKERNEL */ SLEEP(2000); while (1) { if (forks_available) { /* take all forks */ for (i = 0; i < N_PHILOSOPHERS; i++) { TAKE(forks[i]); } /* Philosophers won't be able to take any fork for 2000 ticks */ forks_available = 0; SLEEP(2000); } else { /* give back all forks */ for (i = 0; i < N_PHILOSOPHERS; i++) { GIVE(forks[i]); } /* Philosophers will be able to take forks for 2000 ticks */ forks_available = 1; SLEEP(2000); } } }
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); } }