diff --git a/DiningPhilosophers.c b/DiningPhilosophers.c index 787ea1d..9022677 100644 --- a/DiningPhilosophers.c +++ b/DiningPhilosophers.c @@ -5,9 +5,6 @@ #include #include -// #define N 5 /*number of philosophers*/ -// #define LEFT (i+N-1)%N /*number of i's left neighbor*/ -// #define RIGHT (i+1)%N /*number of i's right neighbor*/ #define THINKING 0 /*philosopher is thinking*/ #define HUNGRY 1 /*philosopher is trying to get forks*/ #define EATING 2 /*philosopher is eating*/ @@ -23,7 +20,7 @@ typedef struct PhilosopherData { } PhilosopherData; bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers); -void Down(); +void Down(sem_t* semaphore); void Eat(PhilosopherData* philosopherSelected); int GetPhilosopherCount(char* argv); void* Philosopher(void* philosopherPassed); @@ -33,18 +30,16 @@ void PutForks(PhilosopherData* philosopherSelected); void TakeForks(PhilosopherData* philosopherSelected); bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal); void Think(PhilosopherData* philosopherSelected); -void Up(); +void Up(sem_t* semaphore); /* * Todo: - * Add prints to each state change * Fix each function's requirements * Theories: * Threads should be put to sleep if can't eat * Sleep has to be awoken by something else, process can't wake own thread * Place semaphores so states can be checked by all * Total fork count = N / 2 - * */ int main(int argc, char* argv[]) @@ -73,21 +68,18 @@ bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) return true; } -// +// Takes a pointer to a philosopher and puts semaphore into waiting state. // TODO: // Turn function into picking up forks (starting waits) // (Right fork is owned by selectedPhilosopher number for consistency) // Add sem_wait -void Down() +void Down(sem_t* semaphore) { - printf("Down() was called...\n"); + sem_wait(semaphore); + printf("A semaphore was put into waiting...\n"); } -// -// TODO: -// Add changing state to function -// Add increasing eating count functionality -// +// Sets philosopher to eating state and increments eating count. void Eat(PhilosopherData* philosopherSelected) { printf("Philosopher %d is eating...\n", philosopherSelected->position); @@ -95,8 +87,7 @@ void Eat(PhilosopherData* philosopherSelected) ++philosopherSelected->eatingCount; } -// Takes in char* and converts into a number -// Returns a number +// Converts char* to number. int GetPhilosopherCount(char* argv) { int philosopherTotal; @@ -104,18 +95,10 @@ int GetPhilosopherCount(char* argv) return philosopherTotal; } -// 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 +// Main function that each thread runs. void* Philosopher(void* philosopherPassed) { PhilosopherData *philosopherSelected = (PhilosopherData*) philosopherPassed; - // 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]; while (true) { Think(philosopherSelected); /*philosopher is thinking*/ @@ -126,6 +109,7 @@ void* Philosopher(void* philosopherPassed) } } +// Handles all philosopher threads, finishes when all philosophers are full. void PhilosopherInit(int philosopherTotal) { PhilosopherData PhilosopherList[philosopherTotal]; @@ -136,11 +120,10 @@ void PhilosopherInit(int philosopherTotal) { if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal)) { - Down(PhilosopherList[i-1].semaphore); - Down(PhilosopherList[i].semaphore); + Up(&(PhilosopherList[i-1].semaphore)); + Up(&(PhilosopherList[i].semaphore)); } } - break; } } @@ -167,11 +150,11 @@ void PutForks(PhilosopherData* philosopherSelected) // Up(&mutex); /*block if forks were not acquired*/ } -// Takes in a number for a selected philosopher, from 0 to N-1 +// Philosopher takes the forks on the left and right. void TakeForks(PhilosopherData* philosopherSelected) { printf("Philosopher %d is taking forks...\n", philosopherSelected->position); - sem_wait(&philosopherSelected->semaphore); + Down(&philosopherSelected->semaphore); // Down(&mutex); /*enter critical region*/ // PhilosopherList.state = HUNGRY; /*philosopher has finished eating*/ // Test(selectedPhilosopher, ); /*see if left neighbor can now eat*/ @@ -179,9 +162,9 @@ void TakeForks(PhilosopherData* philosopherSelected) // Down(&philosopherSemaphore[selectedPhilosopher]); /*exit critical region*/ } -// 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 +// If philosopher is hungry and forks are free, then returns true. +// Else, philosopher returns false. +// Left philosopher math: (i+N-1)%N Right philosopher math: (i+1)%N bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal) { if (PhilosopherList[selectedPosition].state != HUNGRY) @@ -206,7 +189,8 @@ void Think(PhilosopherData* philosopherSelected) // Todo: // Make function release hold on semaphore // Use 'sem_post' -void Up() +void Up(sem_t* semaphore) { - printf("Up() was called...\n"); + sem_post(semaphore); + printf("A semaphore was released...\n"); } diff --git a/DiningPhilosophers.out b/DiningPhilosophers.out index bab7381..36d8c49 100755 Binary files a/DiningPhilosophers.out and b/DiningPhilosophers.out differ diff --git a/TestDining.sh b/TestDining.sh index 3409c1f..e91dfe0 100755 --- a/TestDining.sh +++ b/TestDining.sh @@ -1,4 +1,4 @@ #!/bin/bash rm DiningPhilosophers.out gcc DiningPhilosophers.c -o DiningPhilosophers.out 2> crashCompile.log -./DiningPhilosophers.out 5 > output.log 2> crashLaunch.log +./DiningPhilosophers.out 5