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*/
pthread_mutex_t mutex; /*mutual exclusion for critical region*/
typedef struct PhilosopherData {
int eatingCount;
int position;
@ -21,15 +22,18 @@ typedef struct PhilosopherData {
} PhilosopherData;
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers);
void Down(pthread_mutex_t selectedSemaphore);
void Eat(int selectedPhilosopher);
int GetNumberPhilosophers(char* argv);
void Philosopher(int selectedPhilosopher);
void PutForks(PhilosopherData PhilosopherList);
void TakeForks(PhilosopherData PhilosopherList);
void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers);
void Think(int selectedPhilosopher);
void Up(pthread_mutex_t selectedSemaphore);
void Down();
void Eat(void* philosopherSelected);
int GetPhilosopherCount(char* argv);
int GetAbsolutePosition(PhilosopherData PhilosopherSelected);
void* Philosopher(void* philosopherSelected);
void PhilosopherInit(int philosopherTotal);
void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal);
void PutForks(void* philosopherSelected);
void TakeForks(void* philosopherSelected);
bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal);
void Think(void* philosopherSelected);
void Up();
/*
* Todo:
@ -45,12 +49,19 @@ void Up(pthread_mutex_t selectedSemaphore);
int main(int argc, char* argv[])
{
if (argc == 2)
if (argc < 2)
{
int philosopherCount;
philosopherCount = GetNumberPhilosophers(argv[1]);
Philosopher(philosopherCount);
printf("Too few arguments, exiting...\n");
return 0;
}
if (argc > 2)
{
printf("Too many arguments, exiting...\n");
return 0;
}
int philosopherTotal;
philosopherTotal = GetPhilosopherCount(argv[1]);
PhilosopherInit(philosopherTotal);
return 0;
}
@ -70,9 +81,9 @@ bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
// Turn function into picking up forks (starting waits)
// (Right fork is owned by selectedPhilosopher number for consistency)
// 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 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
// Returns a number
int GetNumberPhilosophers(char* argv)
int GetPhilosopherCount(char* argv)
{
int numPhilosophers;
sscanf(argv, "%d", &numPhilosophers);
return numPhilosophers;
int philosopherTotal;
sscanf(argv, "%d", &philosopherTotal);
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(int numPhilosophers)
void* Philosopher(void* philosopherSelected)
{
// Below replaced by PhilosopherData
// int philosopherEatingCount[numPhilosophers];
// sem_t philosopherSemaphore[numPhilosophers]; /*one semaphore per philospher*/
// // array to keep track of everyone's state
// int philosopherState[numPhilosophers];
PhilosopherData PhilosopherList[numPhilosophers];
int forkCount = numPhilosophers / 2;
while (true)
{
Think(0); /*philosopher is thinking*/
// TakeForks(0, numPhilosophers); /*acquire two forks or block*/
// Eat(0); /*yum-yum, spaghetti*/
// PutForks(0, numPhilosophers); /*put both forks back on table*/
Think(philosopherSelected); /*philosopher is thinking*/
TakeForks(philosopherSelected); /*acquire two forks or block*/
Eat(philosopherSelected); /*yum-yum, spaghetti*/
PutForks(philosopherSelected); /*put both forks back on table*/
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*/
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*/
// Test(selectedPhilosopher, numPhilosophers);
// 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
void TakeForks(PhilosopherData PhilosopherList)
void TakeForks(void* philosopherSelected)
{
printf("TakeForks() was called...\n");
// 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*/
// Up(&mutex); /*see if right neighbor can now eat*/
// 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
// Checks if left and right philosopher is not eating,
// 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)
return;
int leftPhilosopher = (positionSelected + (numPhilosophers-1)) % numPhilosophers;
int rightPhilosopher = (positionSelected + 1) % numPhilosophers;
if (PhilosopherList[selectedPosition].state != HUNGRY)
return false;
int leftPhilosopher = (selectedPosition + (philosopherTotal-1)) % philosopherTotal;
int rightPhilosopher = (selectedPosition + 1) % philosopherTotal;
if (PhilosopherList[leftPhilosopher].state == EATING)
return;
return false;
if (PhilosopherList[rightPhilosopher].state == EATING)
return;
PhilosopherList[positionSelected].state = EATING;
// Up(&philosopherSemaphore[selectedPhilosopher]);
return false;
return true;
}
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:
// Make function release hold on semaphore
// 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
rm DiningPhilosophers.out
gcc DiningPhilosophers.c -o DiningPhilosophers.out
./DiningPhilosophers.out 5
gcc DiningPhilosophers.c -o DiningPhilosophers.out 2> crashCompile.log
./DiningPhilosophers.out 5 2> crashLaunch.log

0
crashCompile.log Normal file
View File

0
crashLaunch.log Normal file
View File