Removed more unused code

This commit is contained in:
TriantaTV 2022-09-30 15:46:39 -05:00
parent 38da411a83
commit 4ef4867d50
2 changed files with 10 additions and 39 deletions

View File

@ -8,7 +8,6 @@
#define THINKING 0 /*philosopher is thinking*/
#define HUNGRY 1 /*philosopher is trying to get forks*/
#define EATING 2 /*philosopher is eating*/
// pthread_mutex_t mutex; /*mutual exclusion for critical region*/
typedef struct PhilosopherData {
@ -32,16 +31,6 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition,
void Think(PhilosopherData* philosopherSelected);
void Up(sem_t* semaphore);
/*
* Todo:
* 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[])
{
if (argc < 2)
@ -57,9 +46,9 @@ int main(int argc, char* argv[])
return 0;
}
// Takes in array of times each philosopher has eaten
// If all philosophers have ate twice, then returns true
// Otherwise, if a philosopher has not eaten twice, returns false
// Takes in array of times each philosopher has eaten
// If all philosophers have ate twice, then returns true
// Otherwise, if a philosopher has not eaten twice, returns false
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
{
for (int i = 0; i < numPhilosophers; i++)
@ -68,18 +57,14 @@ 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
// Philosopher enters critical section.
void Down(sem_t* semaphore)
{
sem_wait(semaphore);
printf("A semaphore was put into waiting...\n");
}
// Sets philosopher to eating state and increments eating count.
// Sets philosopher to eating state and increments eating count.
void Eat(PhilosopherData* philosopherSelected)
{
printf("Philosopher %d is eating...\n", philosopherSelected->position);
@ -87,7 +72,7 @@ void Eat(PhilosopherData* philosopherSelected)
++philosopherSelected->eatingCount;
}
// Converts char* to number.
// Converts char* to number.
int GetPhilosopherCount(char* argv)
{
int philosopherTotal;
@ -105,7 +90,6 @@ void* Philosopher(void* philosopherPassed)
TakeForks(philosopherSelected); /*acquire two forks or block*/
Eat(philosopherSelected); /*yum-yum, spaghetti*/
PutForks(philosopherSelected); /*put both forks back on table*/
break;
}
}
@ -120,13 +104,13 @@ void PhilosopherInit(int philosopherTotal)
{
if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal))
{
Up(&(PhilosopherList[i-1].semaphore));
Up(&(PhilosopherList[i].semaphore));
}
}
}
}
// Initializes full list of philosophers.
void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal)
{
for (int i = 0; i < philosopherTotal; i++)
@ -139,15 +123,10 @@ void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal
}
}
// Philosopher puts forks down.
void PutForks(PhilosopherData* philosopherSelected)
{
printf("Philosopher %d is putting forks down...\n", philosopherSelected->position);
// Down(&mutex); /*enter critical region*/
// philosopherState[selectedPhilosopher] = THINKING; /*record fact that philosopher i is hungry*/
// Test(selectedPhilosopher, numPhilosophers);
// Test(LEFT); /*try to acquire 2 forks*/
// Test(RIGHT); /*exit critical region*/
// Up(&mutex); /*block if forks were not acquired*/
}
// Philosopher takes the forks on the left and right.
@ -155,11 +134,6 @@ void TakeForks(PhilosopherData* philosopherSelected)
{
printf("Philosopher %d is taking forks...\n", philosopherSelected->position);
Down(&philosopherSelected->semaphore);
// Down(&mutex); /*enter critical region*/
// 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*/
}
// If philosopher is hungry and forks are free, then returns true.
@ -178,17 +152,14 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition,
return true;
}
// Philosopher begins thinking
void Think(PhilosopherData* philosopherSelected)
{
// printf("%d is now thinking.\n", PhilosopherList.position);
// PhilosopherList.state = THINKING; /*philosopher has finished eating*/
philosopherSelected->state = THINKING;
printf("Philosopher %d is thinking...\n", philosopherSelected->position);
}
// Todo:
// Make function release hold on semaphore
// Use 'sem_post'
// Philosopher exits critical section
void Up(sem_t* semaphore)
{
sem_post(semaphore);

Binary file not shown.