Fixed errors and program runs

This commit is contained in:
TriantaTV 2022-09-27 22:58:47 -05:00
parent ba0363c2c0
commit 31000d2116
2 changed files with 69 additions and 39 deletions

BIN
DiningPhilosophers Executable file

Binary file not shown.

View File

@ -1,28 +1,35 @@
#include <pthread.h> #include <pthread.h>
#include <semaphore.h> #include <semaphore.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <unistd.h>
#define N 5 /*number of philosophers*/ // #define N 5 /*number of philosophers*/
#define LEFT (i+N-1)%N /*number of i's left neighbor*/ // #define LEFT (i+N-1)%N /*number of i's left neighbor*/
#define RIGHT (i+1)%N /*number of i's right 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*/
int philosopherState[N]; /*array to keep track of everyone's state*/ pthread_mutex_t mutex; /*mutual exclusion for critical region*/
sem_t mutex = 1; /*mutual exclusion for critical region*/
sem_t philosopherSemaphore[N]; /*one semaphore per philospher*/
void Down(sem_t* selectedSemaphore); typedef struct PhilosopherData {
int eatingCount;
int position;
sem_t semaphore;
int state;
} PhilosopherData;
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers);
void Down(pthread_mutex_t selectedSemaphore);
void Eat(int selectedPhilosopher); void Eat(int selectedPhilosopher);
int GetNumberPhilosophers(char* argv); int GetNumberPhilosophers(char* argv);
void Philosopher(int selectedPhilosopher); void Philosopher(int selectedPhilosopher);
void PutForks(int selectedPhilosopher); void PutForks(PhilosopherData PhilosopherList);
void TakeForks(int selectedPhilosopher); void TakeForks(PhilosopherData PhilosopherList);
void Test(int selectedPhilosopher); void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers);
void Think(int selectedPhilosopher); void Think(int selectedPhilosopher);
void Up(sem_t* selectedSemaphore); void Up(pthread_mutex_t selectedSemaphore);
/* /*
* Todo: * Todo:
@ -50,11 +57,22 @@ int main(int argc, char* argv[])
return 0; 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
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
{
for (int i = 0; i < numPhilosophers; i++)
if (PhilosopherList[i].eatingCount != 2)
return false;
return true;
}
// //
// TODO: // TODO:
// Turn function into putting down forks (releasing waits) // Turn function into putting down forks (releasing waits)
// // sem_wait
void Down(sem_t* selectedSemaphore) void Down(pthread_mutex_t selectedSemaphore)
{ {
printf("Function Down() was called.\n"); printf("Function Down() was called.\n");
} }
@ -82,49 +100,60 @@ int GetNumberPhilosophers(char* argv)
// Finishes when all philosophers have eaten twice // Finishes when all philosophers have eaten twice
void Philosopher(int numPhilosophers) void Philosopher(int numPhilosophers)
{ {
int stateEatingCount[numPhilosophers]; // 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) while (true)
{ {
Think(); /*philosopher is thinking*/ break;
TakeForks(numPhilosophers); /*acquire two forks or block*/ // Think(0); /*philosopher is thinking*/
Eat(); /*yum-yum, spaghetti*/ // TakeForks(0, numPhilosophers); /*acquire two forks or block*/
PutForks(numPhilosophers); /*put both forks back on table*/ // Eat(0); /*yum-yum, spaghetti*/
// PutForks(0, numPhilosophers); /*put both forks back on table*/
} }
} }
void PutForks(int selectedPhilosopher) /*i: philosopher number, from 0 to N-1*/ void PutForks(PhilosopherData PhilosopherList)
{ {
Down(&mutex); /*enter critical region*/ // Down(&mutex); /*enter critical region*/
printf("%d is now thinking.\n", selectedPhilosopher); printf("%d is now thinking.\n", PhilosopherList.position);
philosopherState[selectedPhilosopher] = THINKING; /*record fact that philosopher i is hungry*/ PhilosopherList.state = THINKING; /*philosopher has finished eating*/
Test((i+N-1)%N); /*try to acquire 2 forks*/ // philosopherState[selectedPhilosopher] = THINKING; /*record fact that philosopher i is hungry*/
Test(RIGHT); /*exit critical region*/ // Test(selectedPhilosopher, numPhilosophers);
Up(&mutex); /*block if forks were not acquired*/ // Test(LEFT); /*try to acquire 2 forks*/
// Test(RIGHT); /*exit critical region*/
// Up(&mutex); /*block if forks were not acquired*/
} }
// 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(int selectedPhilosopher) void TakeForks(PhilosopherData PhilosopherList)
{ {
Down(&mutex); /*enter critical region*/ // Down(&mutex); /*enter critical region*/
philosopherState[selectedPhilosopher] = 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*/
} }
// 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(int selectedPhilosopher) void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers)
{ {
if (philosopherState[selectedPhilosopher] != HUNGRY) if (PhilosopherList[positionSelected].state != HUNGRY)
return; return;
if (philosopherState[(selectedPhilosopher+N-1)%N LEFT] == EATING) int leftPhilosopher = (positionSelected + (numPhilosophers-1)) % numPhilosophers;
int rightPhilosopher = (positionSelected + 1) % numPhilosophers;
if (PhilosopherList[leftPhilosopher].state == EATING)
return; return;
if (philosopherState[RIGHT] == EATING) if (PhilosopherList[rightPhilosopher].state == EATING)
return; return;
philosopherState[selectedPhilosopher] = EATING; PhilosopherList[positionSelected].state = EATING;
Up(&philosopherSemaphore[selectedPhilosopher]); // Up(&philosopherSemaphore[selectedPhilosopher]);
} }
void Think(int selectedPhilosopher) void Think(int selectedPhilosopher)
@ -134,7 +163,8 @@ void Think(int selectedPhilosopher)
// Pick up for on left and right // Pick up for on left and right
// (Right fork is owned by selectedPhilosopher number for consistency) // (Right fork is owned by selectedPhilosopher number for consistency)
void Up(sem_t* selectedSemaphore) // sem_post
void Up(pthread_mutex_t selectedSemaphore)
{ {
printf("Function Up() was called.\n"); printf("Function Up() was called.\n");
} }