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)
{
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;
}

Binary file not shown.

View File

@ -1,4 +1,4 @@
#!/bin/bash
rm DiningPhilosophers.out
gcc DiningPhilosophers.c -o DiningPhilosophers.out 2> crashCompile.log
./DiningPhilosophers.out 5
./DiningPhilosophers.out 5