diff --git a/DiningPhilosophers.c b/DiningPhilosophers.c index 9f7551f..ec8b3f6 100644 --- a/DiningPhilosophers.c +++ b/DiningPhilosophers.c @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) { for (int i = 0; i < numPhilosophers; i++) - if (PhilosopherList[i].eatingCount < 1) + if (PhilosopherList[i].eatingCount < 2) return false; return true; } @@ -69,6 +69,7 @@ void PhilosopherWait() void Eat(PhilosopherData* philosopherSelected) { printf("Philosopher %d is eating...\n", philosopherSelected->position); + philosopherSelected->state = EATING; ++philosopherSelected->eatingCount; } @@ -87,6 +88,11 @@ void* Philosopher(void* philosopherPassed) while (true) { Think(philosopherSelected); /*philosopher is thinking*/ + if (philosopherSelected->eatingCount == 2) + { + printf("Philosopher %d ate twice...\n", philosopherSelected->position); + return; + } TakeForks(philosopherSelected); /*acquire two forks or block*/ Eat(philosopherSelected); /*yum-yum, spaghetti*/ PutForks(philosopherSelected); /*put both forks back on table*/ @@ -101,16 +107,16 @@ void PhilosopherInit(int philosopherTotal) sem_init(&semaphore, 0, 1); while (!AllPhilosophersFull(PhilosopherList, philosopherTotal)) { - sleep(2); for (int i = 0; i < philosopherTotal; i++) { - if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal)) - { - printf("Philosopher %d was allowed to eat...\n", i); - PhilosopherList[i].state = EATING; - AllowPhilosopher(); - } + printf("Philosopher %d state: %d\n", PhilosopherList[i].position, PhilosopherList[i].state); } + AllowPhilosopher(); + sleep(1); + } + for (int i = 0; i < philosopherTotal; i++) + { + printf("Philosopher %d eating count: %d\n", PhilosopherList[i].position, PhilosopherList[i].eatingCount); } } @@ -121,7 +127,9 @@ void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal { PhilosopherList[i].position = i; PhilosopherList[i].eatingCount = 0; + PhilosopherList[i].state = THINKING; pthread_create(&PhilosopherList[i].thread, NULL, &Philosopher, &PhilosopherList[i]); + // pthread_join(&PhilosopherList[i].thread, NULL); } } @@ -146,12 +154,15 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, { if (PhilosopherList[selectedPosition].state != HUNGRY) return false; - int leftPhilosopher = (selectedPosition + (philosopherTotal-1)) % philosopherTotal; - int rightPhilosopher = (selectedPosition + 1) % philosopherTotal; - if (PhilosopherList[leftPhilosopher].state == EATING) - return false; - if (PhilosopherList[rightPhilosopher].state == EATING) - return false; + // int leftPhilosopher = (selectedPosition + (philosopherTotal-1)) % philosopherTotal; + // int rightPhilosopher = (selectedPosition + 1) % philosopherTotal; + // if (PhilosopherList[leftPhilosopher].state == EATING) + // return false; + // if (PhilosopherList[rightPhilosopher].state == EATING) + // return false; + for (int i = 0; i < philosopherTotal; i++) + if (PhilosopherList[i].state == EATING) + return false; return true; } diff --git a/DiningPhilosophers.out b/DiningPhilosophers.out index 2926701..9fd69ad 100755 Binary files a/DiningPhilosophers.out and b/DiningPhilosophers.out differ diff --git a/TestDining.sh b/TestDining.sh index e91dfe0..2e6980e 100755 --- a/TestDining.sh +++ b/TestDining.sh @@ -1,4 +1,4 @@ #!/bin/bash rm DiningPhilosophers.out gcc DiningPhilosophers.c -o DiningPhilosophers.out 2> crashCompile.log -./DiningPhilosophers.out 5 +./DiningPhilosophers.out 5