Fixed using selectedPhilosopher
This commit is contained in:
parent
f202f7f403
commit
9522bce20b
@ -19,20 +19,20 @@ typedef struct PhilosopherData {
|
||||
int position;
|
||||
sem_t semaphore;
|
||||
int state;
|
||||
pthread_t thread;
|
||||
} PhilosopherData;
|
||||
|
||||
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers);
|
||||
void Down();
|
||||
void Eat(void* philosopherSelected);
|
||||
void Eat(PhilosopherData* philosopherSelected);
|
||||
int GetPhilosopherCount(char* argv);
|
||||
int GetAbsolutePosition(PhilosopherData PhilosopherSelected);
|
||||
void* Philosopher(void* philosopherSelected);
|
||||
void* Philosopher(void* philosopherPassed);
|
||||
void PhilosopherInit(int philosopherTotal);
|
||||
void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal);
|
||||
void PutForks(void* philosopherSelected);
|
||||
void TakeForks(void* philosopherSelected);
|
||||
void PutForks(PhilosopherData* philosopherSelected);
|
||||
void TakeForks(PhilosopherData* philosopherSelected);
|
||||
bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal);
|
||||
void Think(void* philosopherSelected);
|
||||
void Think(PhilosopherData* philosopherSelected);
|
||||
void Up();
|
||||
|
||||
/*
|
||||
@ -91,9 +91,8 @@ void Down()
|
||||
// Add changing state to function
|
||||
// Add increasing eating count functionality
|
||||
//
|
||||
void Eat(void* philosopherSelected)
|
||||
void Eat(PhilosopherData* philosopherSelected)
|
||||
{
|
||||
philosopherSelected.
|
||||
printf("Eat() was called...\n");
|
||||
}
|
||||
|
||||
@ -106,18 +105,13 @@ int GetPhilosopherCount(char* argv)
|
||||
return philosopherTotal;
|
||||
}
|
||||
|
||||
// Unused function to be removed
|
||||
int GetAbsolutePosition(PhilosopherData philosopherSelected)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
// Takes in the number of philosophers, from 0 to N-1
|
||||
// Finishes when all philosophers have eaten twice
|
||||
// TODO:
|
||||
// Add splitting function into multiple pthreads
|
||||
void* Philosopher(void* philosopherSelected)
|
||||
void* Philosopher(void* philosopherPassed)
|
||||
{
|
||||
PhilosopherData *philosopherSelected = (PhilosopherData*) philosopherPassed;
|
||||
// Below replaced by PhilosopherData
|
||||
// int philosopherEatingCount[numPhilosophers];
|
||||
// sem_t philosopherSemaphore[numPhilosophers]; /*one semaphore per philospher*/
|
||||
@ -136,22 +130,18 @@ void* Philosopher(void* philosopherSelected)
|
||||
void PhilosopherInit(int philosopherTotal)
|
||||
{
|
||||
PhilosopherData PhilosopherList[philosopherTotal];
|
||||
pthread_t PhilosopherThreads[philosopherTotal];
|
||||
for (int i = 0; i < philosopherTotal; i++)
|
||||
pthread_create(&PhilosopherThreads[i], NULL, &Philosopher, &PhilosopherList[i]);
|
||||
for (int i = 0; i < philosopherTotal; i++)
|
||||
pthread_join(PhilosopherThreads[i], NULL);
|
||||
PhilosopherListInit(PhilosopherList, philosopherTotal);
|
||||
while (!AllPhilosophersFull(PhilosopherList, philosopherTotal))
|
||||
{
|
||||
for (int i = 0; i < philosopherTotal; i++)
|
||||
{
|
||||
|
||||
if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal))
|
||||
{
|
||||
Down(PhilosopherList[i-1].semaphore);
|
||||
Down(PhilosopherList[i].semaphore);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,10 +151,13 @@ void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal
|
||||
{
|
||||
PhilosopherList[i].position = i;
|
||||
PhilosopherList[i].eatingCount = 0;
|
||||
sem_init(&PhilosopherList[i].semaphore, 0, 1);
|
||||
pthread_create(&PhilosopherList[i].thread, NULL, &Philosopher, &PhilosopherList[i]);
|
||||
pthread_join(PhilosopherList[i].thread, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void PutForks(void* philosopherSelected)
|
||||
void PutForks(PhilosopherData* philosopherSelected)
|
||||
{
|
||||
printf("PutForks() was called...\n");
|
||||
// Down(&mutex); /*enter critical region*/
|
||||
@ -176,9 +169,10 @@ void PutForks(void* philosopherSelected)
|
||||
}
|
||||
|
||||
// Takes in a number for a selected philosopher, from 0 to N-1
|
||||
void TakeForks(void* philosopherSelected)
|
||||
void TakeForks(PhilosopherData* philosopherSelected)
|
||||
{
|
||||
printf("TakeForks() was called...\n");
|
||||
sem_wait(&philosopherSelected->semaphore);
|
||||
// Down(&mutex); /*enter critical region*/
|
||||
// PhilosopherList.state = HUNGRY; /*philosopher has finished eating*/
|
||||
// Test(selectedPhilosopher, ); /*see if left neighbor can now eat*/
|
||||
@ -202,11 +196,12 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition,
|
||||
return true;
|
||||
}
|
||||
|
||||
void Think(void* philosopherSelected)
|
||||
void Think(PhilosopherData* philosopherSelected)
|
||||
{
|
||||
// printf("%d is now thinking.\n", PhilosopherList.position);
|
||||
// PhilosopherList.state = THINKING; /*philosopher has finished eating*/
|
||||
printf("Think() was called...\n");
|
||||
philosopherSelected->state = THINKING;
|
||||
printf("Philosopher %d is thinking...\n", philosopherSelected->position);
|
||||
}
|
||||
|
||||
// Todo:
|
||||
|
Binary file not shown.
@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
rm DiningPhilosophers.out
|
||||
gcc DiningPhilosophers.c -o DiningPhilosophers.out 2> crashCompile.log
|
||||
./DiningPhilosophers.out 5 2> crashLaunch.log
|
||||
./DiningPhilosophers.out 5 > output.log 2> crashLaunch.log
|
||||
|
20
output.log
Normal file
20
output.log
Normal file
@ -0,0 +1,20 @@
|
||||
Philosopher 0 is thinking...
|
||||
TakeForks() was called...
|
||||
Eat() was called...
|
||||
PutForks() was called...
|
||||
Philosopher 1 is thinking...
|
||||
TakeForks() was called...
|
||||
Eat() was called...
|
||||
PutForks() was called...
|
||||
Philosopher 2 is thinking...
|
||||
TakeForks() was called...
|
||||
Eat() was called...
|
||||
PutForks() was called...
|
||||
Philosopher 3 is thinking...
|
||||
TakeForks() was called...
|
||||
Eat() was called...
|
||||
PutForks() was called...
|
||||
Philosopher 4 is thinking...
|
||||
TakeForks() was called...
|
||||
Eat() was called...
|
||||
PutForks() was called...
|
Loading…
Reference in New Issue
Block a user