From c31b3b632c6f59e02b2dbb472aa1cd2769eda52f Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Mon, 3 Oct 2022 03:31:33 -0500 Subject: [PATCH] Fixed issues with semaphores. Program fully functional --- DiningPhilosophers.c | 10 ++++++---- DiningPhilosophers.out | Bin 16808 -> 16808 bytes 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/DiningPhilosophers.c b/DiningPhilosophers.c index 9735572..63be6a8 100644 --- a/DiningPhilosophers.c +++ b/DiningPhilosophers.c @@ -86,15 +86,15 @@ void* Philosopher(void* philosopherPassed) PhilosopherData *philosopherSelected = (PhilosopherData*) philosopherPassed; while (true) { - Think(philosopherSelected); /*philosopher is thinking*/ - if (philosopherSelected->eatingCount == 2) + if (philosopherSelected->eatingCount > 2) { - printf("Philosopher %d ate twice...\n", philosopherSelected->position); + // 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*/ + Think(philosopherSelected); /*philosopher is thinking*/ } } @@ -106,9 +106,10 @@ void PhilosopherInit(int philosopherTotal) sem_init(&semaphore, 0, 1); while (!AllPhilosophersFull(PhilosopherList, philosopherTotal)) { - AllowPhilosopher(); sleep(1); + AllowPhilosopher(); } + sleep(5); for (int i = 0; i < philosopherTotal; i++) { printf("Philosopher %d eating count: %d\n", PhilosopherList[i].position, PhilosopherList[i].eatingCount); @@ -123,6 +124,7 @@ void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal PhilosopherList[i].position = i; PhilosopherList[i].eatingCount = 0; PhilosopherList[i].state = THINKING; + printf("Philosopher %d is thinking...\n", i); pthread_create(&PhilosopherList[i].thread, NULL, &Philosopher, &PhilosopherList[i]); } } diff --git a/DiningPhilosophers.out b/DiningPhilosophers.out index af582e668d96240039c529a00b4a28b955f0cd69..21e4ff26629b38d1c9430a35291f43d84b9208d8 100755 GIT binary patch delta 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