Program with 5 philosophers typically works. Currently unable to find cause of variation.

This commit is contained in:
TriantaTV 2022-10-03 02:28:57 -05:00
parent 70bc58707a
commit c8322811a2
3 changed files with 26 additions and 15 deletions

View File

@ -53,7 +53,7 @@ int main(int argc, char* argv[])
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
{ {
for (int i = 0; i < numPhilosophers; i++) for (int i = 0; i < numPhilosophers; i++)
if (PhilosopherList[i].eatingCount < 1) if (PhilosopherList[i].eatingCount < 2)
return false; return false;
return true; return true;
} }
@ -69,6 +69,7 @@ void PhilosopherWait()
void Eat(PhilosopherData* philosopherSelected) void Eat(PhilosopherData* philosopherSelected)
{ {
printf("Philosopher %d is eating...\n", philosopherSelected->position); printf("Philosopher %d is eating...\n", philosopherSelected->position);
philosopherSelected->state = EATING;
++philosopherSelected->eatingCount; ++philosopherSelected->eatingCount;
} }
@ -87,6 +88,11 @@ void* Philosopher(void* philosopherPassed)
while (true) while (true)
{ {
Think(philosopherSelected); /*philosopher is thinking*/ 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*/ TakeForks(philosopherSelected); /*acquire two forks or block*/
Eat(philosopherSelected); /*yum-yum, spaghetti*/ Eat(philosopherSelected); /*yum-yum, spaghetti*/
PutForks(philosopherSelected); /*put both forks back on table*/ PutForks(philosopherSelected); /*put both forks back on table*/
@ -101,16 +107,16 @@ void PhilosopherInit(int philosopherTotal)
sem_init(&semaphore, 0, 1); sem_init(&semaphore, 0, 1);
while (!AllPhilosophersFull(PhilosopherList, philosopherTotal)) while (!AllPhilosophersFull(PhilosopherList, philosopherTotal))
{ {
sleep(2);
for (int i = 0; i < philosopherTotal; i++) for (int i = 0; i < philosopherTotal; i++)
{ {
if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal)) printf("Philosopher %d state: %d\n", PhilosopherList[i].position, PhilosopherList[i].state);
{
printf("Philosopher %d was allowed to eat...\n", i);
PhilosopherList[i].state = EATING;
AllowPhilosopher();
}
} }
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].position = i;
PhilosopherList[i].eatingCount = 0; PhilosopherList[i].eatingCount = 0;
PhilosopherList[i].state = THINKING;
pthread_create(&PhilosopherList[i].thread, NULL, &Philosopher, &PhilosopherList[i]); 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) if (PhilosopherList[selectedPosition].state != HUNGRY)
return false; return false;
int leftPhilosopher = (selectedPosition + (philosopherTotal-1)) % philosopherTotal; // int leftPhilosopher = (selectedPosition + (philosopherTotal-1)) % philosopherTotal;
int rightPhilosopher = (selectedPosition + 1) % philosopherTotal; // int rightPhilosopher = (selectedPosition + 1) % philosopherTotal;
if (PhilosopherList[leftPhilosopher].state == EATING) // if (PhilosopherList[leftPhilosopher].state == EATING)
return false; // return false;
if (PhilosopherList[rightPhilosopher].state == EATING) // if (PhilosopherList[rightPhilosopher].state == EATING)
return false; // return false;
for (int i = 0; i < philosopherTotal; i++)
if (PhilosopherList[i].state == EATING)
return false;
return true; return true;
} }

Binary file not shown.