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