Removed useless comments and old code

This commit is contained in:
TriantaTV 2022-09-30 15:39:21 -05:00
parent e3bac2aeba
commit 38da411a83
3 changed files with 21 additions and 37 deletions

View File

@ -5,9 +5,6 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
// #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 THINKING 0 /*philosopher is thinking*/
#define HUNGRY 1 /*philosopher is trying to get forks*/ #define HUNGRY 1 /*philosopher is trying to get forks*/
#define EATING 2 /*philosopher is eating*/ #define EATING 2 /*philosopher is eating*/
@ -23,7 +20,7 @@ typedef struct PhilosopherData {
} PhilosopherData; } PhilosopherData;
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers); bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers);
void Down(); void Down(sem_t* semaphore);
void Eat(PhilosopherData* philosopherSelected); void Eat(PhilosopherData* philosopherSelected);
int GetPhilosopherCount(char* argv); int GetPhilosopherCount(char* argv);
void* Philosopher(void* philosopherPassed); void* Philosopher(void* philosopherPassed);
@ -33,18 +30,16 @@ void PutForks(PhilosopherData* philosopherSelected);
void TakeForks(PhilosopherData* philosopherSelected); void TakeForks(PhilosopherData* philosopherSelected);
bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal); bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal);
void Think(PhilosopherData* philosopherSelected); void Think(PhilosopherData* philosopherSelected);
void Up(); void Up(sem_t* semaphore);
/* /*
* Todo: * Todo:
* Add prints to each state change
* Fix each function's requirements * Fix each function's requirements
* Theories: * Theories:
* Threads should be put to sleep if can't eat * Threads should be put to sleep if can't eat
* Sleep has to be awoken by something else, process can't wake own thread * Sleep has to be awoken by something else, process can't wake own thread
* Place semaphores so states can be checked by all * Place semaphores so states can be checked by all
* Total fork count = N / 2 * Total fork count = N / 2
*
*/ */
int main(int argc, char* argv[]) int main(int argc, char* argv[])
@ -73,21 +68,18 @@ bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
return true; return true;
} }
// // Takes a pointer to a philosopher and puts semaphore into waiting state.
// TODO: // TODO:
// 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() void Down(sem_t* semaphore)
{ {
printf("Down() was called...\n"); sem_wait(semaphore);
printf("A semaphore was put into waiting...\n");
} }
// // Sets philosopher to eating state and increments eating count.
// TODO:
// Add changing state to function
// Add increasing eating count functionality
//
void Eat(PhilosopherData* philosopherSelected) void Eat(PhilosopherData* philosopherSelected)
{ {
printf("Philosopher %d is eating...\n", philosopherSelected->position); printf("Philosopher %d is eating...\n", philosopherSelected->position);
@ -95,8 +87,7 @@ void Eat(PhilosopherData* philosopherSelected)
++philosopherSelected->eatingCount; ++philosopherSelected->eatingCount;
} }
// Takes in char* and converts into a number // Converts char* to number.
// Returns a number
int GetPhilosopherCount(char* argv) int GetPhilosopherCount(char* argv)
{ {
int philosopherTotal; int philosopherTotal;
@ -104,18 +95,10 @@ int GetPhilosopherCount(char* argv)
return philosopherTotal; return philosopherTotal;
} }
// Takes in the number of philosophers, from 0 to N-1 // Main function that each thread runs.
// Finishes when all philosophers have eaten twice
// TODO:
// Add splitting function into multiple pthreads
void* Philosopher(void* philosopherPassed) void* Philosopher(void* philosopherPassed)
{ {
PhilosopherData *philosopherSelected = (PhilosopherData*) 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) while (true)
{ {
Think(philosopherSelected); /*philosopher is thinking*/ 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) void PhilosopherInit(int philosopherTotal)
{ {
PhilosopherData PhilosopherList[philosopherTotal]; PhilosopherData PhilosopherList[philosopherTotal];
@ -136,11 +120,10 @@ void PhilosopherInit(int philosopherTotal)
{ {
if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal)) if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal))
{ {
Down(PhilosopherList[i-1].semaphore); Up(&(PhilosopherList[i-1].semaphore));
Down(PhilosopherList[i].semaphore); Up(&(PhilosopherList[i].semaphore));
} }
} }
break;
} }
} }
@ -167,11 +150,11 @@ void PutForks(PhilosopherData* philosopherSelected)
// Up(&mutex); /*block if forks were not acquired*/ // 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) void TakeForks(PhilosopherData* philosopherSelected)
{ {
printf("Philosopher %d is taking forks...\n", philosopherSelected->position); printf("Philosopher %d is taking forks...\n", philosopherSelected->position);
sem_wait(&philosopherSelected->semaphore); Down(&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*/
@ -179,9 +162,9 @@ void TakeForks(PhilosopherData* philosopherSelected)
// Down(&philosopherSemaphore[selectedPhilosopher]); /*exit critical region*/ // Down(&philosopherSemaphore[selectedPhilosopher]); /*exit critical region*/
} }
// Takes in the number of philosophers, from 0 to N-1 // If philosopher is hungry and forks are free, then returns true.
// Checks if left and right philosopher is not eating, // Else, philosopher returns false.
// and philosopher is hungry, then philosopher will eat // Left philosopher math: (i+N-1)%N Right philosopher math: (i+1)%N
bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal) bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal)
{ {
if (PhilosopherList[selectedPosition].state != HUNGRY) if (PhilosopherList[selectedPosition].state != HUNGRY)
@ -206,7 +189,8 @@ void Think(PhilosopherData* philosopherSelected)
// Todo: // Todo:
// Make function release hold on semaphore // Make function release hold on semaphore
// Use 'sem_post' // Use 'sem_post'
void Up() void Up(sem_t* semaphore)
{ {
printf("Up() was called...\n"); sem_post(semaphore);
printf("A semaphore was released...\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 2> crashCompile.log gcc DiningPhilosophers.c -o DiningPhilosophers.out 2> crashCompile.log
./DiningPhilosophers.out 5 > output.log 2> crashLaunch.log ./DiningPhilosophers.out 5