Philosophers eat, but lacks sem_post order

This commit is contained in:
TriantaTV 2022-10-01 23:35:31 -05:00
parent a4bcbc250f
commit 70bc58707a
2 changed files with 20 additions and 17 deletions

View File

@ -13,13 +13,14 @@
typedef struct PhilosopherData { typedef struct PhilosopherData {
int eatingCount; int eatingCount;
int position; int position;
sem_t semaphore;
int state; int state;
pthread_t thread; pthread_t thread;
} PhilosopherData; } PhilosopherData;
sem_t semaphore;
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers); bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers);
void Down(sem_t* semaphore); void PhilosopherWait();
void Eat(PhilosopherData* philosopherSelected); void Eat(PhilosopherData* philosopherSelected);
int GetPhilosopherCount(char* argv); int GetPhilosopherCount(char* argv);
void* Philosopher(void* philosopherPassed); void* Philosopher(void* philosopherPassed);
@ -29,7 +30,7 @@ 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(sem_t* semaphore); void AllowPhilosopher();
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@ -52,23 +53,22 @@ int main(int argc, char* argv[])
bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
{ {
for (int i = 0; i < numPhilosophers; i++) for (int i = 0; i < numPhilosophers; i++)
if (PhilosopherList[i].eatingCount < 2) if (PhilosopherList[i].eatingCount < 1)
return false; return false;
return true; return true;
} }
// Philosopher enters critical section. // Philosopher enters critical section.
void Down(sem_t* semaphore) void PhilosopherWait()
{ {
sem_wait(semaphore); printf("A thread was put into waiting...\n");
printf("A semaphore was put into waiting...\n"); sem_wait(&semaphore);
} }
// Sets philosopher to eating state and increments eating count. // Sets philosopher to eating state and increments eating count.
void Eat(PhilosopherData* philosopherSelected) void Eat(PhilosopherData* philosopherSelected)
{ {
printf("Philosopher %d is eating...\n", philosopherSelected->position); printf("Philosopher %d is eating...\n", philosopherSelected->position);
philosopherSelected->state = EATING;
++philosopherSelected->eatingCount; ++philosopherSelected->eatingCount;
} }
@ -98,13 +98,17 @@ void PhilosopherInit(int philosopherTotal)
{ {
PhilosopherData PhilosopherList[philosopherTotal]; PhilosopherData PhilosopherList[philosopherTotal];
PhilosopherListInit(PhilosopherList, philosopherTotal); PhilosopherListInit(PhilosopherList, philosopherTotal);
sem_init(&semaphore, 0, 1);
while (!AllPhilosophersFull(PhilosopherList, philosopherTotal)) while (!AllPhilosophersFull(PhilosopherList, philosopherTotal))
{ {
sleep(2);
for (int i = 0; i < philosopherTotal; i++) for (int i = 0; i < philosopherTotal; i++)
{ {
if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal)) if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal))
{ {
Up(&(PhilosopherList[i].semaphore)); printf("Philosopher %d was allowed to eat...\n", i);
PhilosopherList[i].state = EATING;
AllowPhilosopher();
} }
} }
} }
@ -117,9 +121,7 @@ void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal
{ {
PhilosopherList[i].position = i; PhilosopherList[i].position = i;
PhilosopherList[i].eatingCount = 0; PhilosopherList[i].eatingCount = 0;
sem_init(&PhilosopherList[i].semaphore, 0, 1);
pthread_create(&PhilosopherList[i].thread, NULL, &Philosopher, &PhilosopherList[i]); pthread_create(&PhilosopherList[i].thread, NULL, &Philosopher, &PhilosopherList[i]);
pthread_join(PhilosopherList[i].thread, NULL);
} }
} }
@ -132,8 +134,9 @@ void PutForks(PhilosopherData* philosopherSelected)
// Philosopher takes the forks on the left and right. // 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); philosopherSelected->state = HUNGRY;
Down(&philosopherSelected->semaphore); printf("Philosopher %d is hungry...\n", philosopherSelected->position);
PhilosopherWait();
} }
// If philosopher is hungry and forks are free, then returns true. // If philosopher is hungry and forks are free, then returns true.
@ -155,13 +158,13 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition,
// Philosopher begins thinking // Philosopher begins thinking
void Think(PhilosopherData* philosopherSelected) void Think(PhilosopherData* philosopherSelected)
{ {
philosopherSelected->state = THINKING;
printf("Philosopher %d is thinking...\n", philosopherSelected->position); printf("Philosopher %d is thinking...\n", philosopherSelected->position);
philosopherSelected->state = THINKING;
} }
// Philosopher exits critical section // Philosopher exits critical section
void Up(sem_t* semaphore) void AllowPhilosopher()
{ {
sem_post(semaphore); sem_post(&semaphore);
printf("A semaphore was released...\n"); printf("A thread was released...\n");
} }

Binary file not shown.