Added pthread to program and adjusted philosopher methods

This commit is contained in:
TriantaTV 2022-09-30 00:03:37 -05:00
parent 33ec05e766
commit f202f7f403
5 changed files with 96 additions and 48 deletions

View File

@ -13,6 +13,7 @@
#define EATING 2 /*philosopher is eating*/ #define EATING 2 /*philosopher is eating*/
pthread_mutex_t mutex; /*mutual exclusion for critical region*/ pthread_mutex_t mutex; /*mutual exclusion for critical region*/
typedef struct PhilosopherData { typedef struct PhilosopherData {
int eatingCount; int eatingCount;
int position; int position;
@ -21,15 +22,18 @@ typedef struct PhilosopherData {
} PhilosopherData; } PhilosopherData;
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers); bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers);
void Down(pthread_mutex_t selectedSemaphore); void Down();
void Eat(int selectedPhilosopher); void Eat(void* philosopherSelected);
int GetNumberPhilosophers(char* argv); int GetPhilosopherCount(char* argv);
void Philosopher(int selectedPhilosopher); int GetAbsolutePosition(PhilosopherData PhilosopherSelected);
void PutForks(PhilosopherData PhilosopherList); void* Philosopher(void* philosopherSelected);
void TakeForks(PhilosopherData PhilosopherList); void PhilosopherInit(int philosopherTotal);
void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers); void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal);
void Think(int selectedPhilosopher); void PutForks(void* philosopherSelected);
void Up(pthread_mutex_t selectedSemaphore); void TakeForks(void* philosopherSelected);
bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal);
void Think(void* philosopherSelected);
void Up();
/* /*
* Todo: * Todo:
@ -45,12 +49,19 @@ void Up(pthread_mutex_t selectedSemaphore);
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
if (argc == 2) if (argc < 2)
{ {
int philosopherCount; printf("Too few arguments, exiting...\n");
philosopherCount = GetNumberPhilosophers(argv[1]); return 0;
Philosopher(philosopherCount);
} }
if (argc > 2)
{
printf("Too many arguments, exiting...\n");
return 0;
}
int philosopherTotal;
philosopherTotal = GetPhilosopherCount(argv[1]);
PhilosopherInit(philosopherTotal);
return 0; return 0;
} }
@ -70,9 +81,9 @@ bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
// Turn function into picking up forks (starting waits) // Turn function into picking up forks (starting waits)
// (Right fork is owned by selectedPhilosopher number for consistency) // (Right fork is owned by selectedPhilosopher number for consistency)
// Add sem_wait // Add sem_wait
void Down(pthread_mutex_t selectedSemaphore) void Down()
{ {
printf("Function Down() was called.\n"); printf("Down() was called...\n");
} }
// //
@ -80,48 +91,83 @@ void Down(pthread_mutex_t selectedSemaphore)
// Add changing state to function // Add changing state to function
// Add increasing eating count functionality // Add increasing eating count functionality
// //
void Eat(int selectedPhilosopher) void Eat(void* philosopherSelected)
{ {
printf("Function Eat() was called.\n"); philosopherSelected.
printf("Eat() was called...\n");
} }
// Takes in char* and converts into a number // Takes in char* and converts into a number
// Returns a number // Returns a number
int GetNumberPhilosophers(char* argv) int GetPhilosopherCount(char* argv)
{ {
int numPhilosophers; int philosopherTotal;
sscanf(argv, "%d", &numPhilosophers); sscanf(argv, "%d", &philosopherTotal);
return numPhilosophers; 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(int numPhilosophers) void* Philosopher(void* philosopherSelected)
{ {
// 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*/
// // array to keep track of everyone's state // // array to keep track of everyone's state
// int philosopherState[numPhilosophers]; // int philosopherState[numPhilosophers];
PhilosopherData PhilosopherList[numPhilosophers];
int forkCount = numPhilosophers / 2;
while (true) while (true)
{ {
Think(0); /*philosopher is thinking*/ Think(philosopherSelected); /*philosopher is thinking*/
// TakeForks(0, numPhilosophers); /*acquire two forks or block*/ TakeForks(philosopherSelected); /*acquire two forks or block*/
// Eat(0); /*yum-yum, spaghetti*/ Eat(philosopherSelected); /*yum-yum, spaghetti*/
// PutForks(0, numPhilosophers); /*put both forks back on table*/ PutForks(philosopherSelected); /*put both forks back on table*/
break; break;
} }
} }
void PutForks(PhilosopherData PhilosopherList) 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);
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);
}
}
}
}
void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal)
{
for (int i = 0; i < philosopherTotal; i++)
{
PhilosopherList[i].position = i;
PhilosopherList[i].eatingCount = 0;
}
}
void PutForks(void* philosopherSelected)
{
printf("PutForks() was called...\n");
// Down(&mutex); /*enter critical region*/ // Down(&mutex); /*enter critical region*/
printf("%d is now thinking.\n", PhilosopherList.position);
PhilosopherList.state = THINKING; /*philosopher has finished eating*/
// philosopherState[selectedPhilosopher] = THINKING; /*record fact that philosopher i is hungry*/ // philosopherState[selectedPhilosopher] = THINKING; /*record fact that philosopher i is hungry*/
// Test(selectedPhilosopher, numPhilosophers); // Test(selectedPhilosopher, numPhilosophers);
// Test(LEFT); /*try to acquire 2 forks*/ // Test(LEFT); /*try to acquire 2 forks*/
@ -130,10 +176,11 @@ void PutForks(PhilosopherData PhilosopherList)
} }
// 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(PhilosopherData PhilosopherList) void TakeForks(void* philosopherSelected)
{ {
printf("TakeForks() was called...\n");
// 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*/
// Up(&mutex); /*see if right neighbor can now eat*/ // Up(&mutex); /*see if right neighbor can now eat*/
// Down(&philosopherSemaphore[selectedPhilosopher]); /*exit critical region*/ // Down(&philosopherSemaphore[selectedPhilosopher]); /*exit critical region*/
@ -142,29 +189,30 @@ void TakeForks(PhilosopherData PhilosopherList)
// Takes in the number of philosophers, from 0 to N-1 // Takes in the number of philosophers, from 0 to N-1
// Checks if left and right philosopher is not eating, // Checks if left and right philosopher is not eating,
// and philosopher is hungry, then philosopher will eat // and philosopher is hungry, then philosopher will eat
void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers) bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal)
{ {
if (PhilosopherList[positionSelected].state != HUNGRY) if (PhilosopherList[selectedPosition].state != HUNGRY)
return; return false;
int leftPhilosopher = (positionSelected + (numPhilosophers-1)) % numPhilosophers; int leftPhilosopher = (selectedPosition + (philosopherTotal-1)) % philosopherTotal;
int rightPhilosopher = (positionSelected + 1) % numPhilosophers; int rightPhilosopher = (selectedPosition + 1) % philosopherTotal;
if (PhilosopherList[leftPhilosopher].state == EATING) if (PhilosopherList[leftPhilosopher].state == EATING)
return; return false;
if (PhilosopherList[rightPhilosopher].state == EATING) if (PhilosopherList[rightPhilosopher].state == EATING)
return; return false;
PhilosopherList[positionSelected].state = EATING; return true;
// Up(&philosopherSemaphore[selectedPhilosopher]);
} }
void Think(int selectedPhilosopher) void Think(void* philosopherSelected)
{ {
printf("Function Think() was called.\n"); // printf("%d is now thinking.\n", PhilosopherList.position);
// PhilosopherList.state = THINKING; /*philosopher has finished eating*/
printf("Think() was called...\n");
} }
// Todo: // Todo:
// Make function release hold on semaphore // Make function release hold on semaphore
// Use 'sem_post' // Use 'sem_post'
void Up(pthread_mutex_t selectedSemaphore) void Up()
{ {
printf("Function Up() was called.\n"); printf("Up() was called...\n");
} }

Binary file not shown.

View File

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

0
crashCompile.log Normal file
View File

0
crashLaunch.log Normal file
View File