diff --git a/DiningPhilosophers.c b/DiningPhilosophers.c index efab6a2..9f7551f 100644 --- a/DiningPhilosophers.c +++ b/DiningPhilosophers.c @@ -13,13 +13,14 @@ typedef struct PhilosopherData { int eatingCount; int position; - sem_t semaphore; int state; pthread_t thread; } PhilosopherData; +sem_t semaphore; + bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers); -void Down(sem_t* semaphore); +void PhilosopherWait(); void Eat(PhilosopherData* philosopherSelected); int GetPhilosopherCount(char* argv); void* Philosopher(void* philosopherPassed); @@ -29,7 +30,7 @@ void PutForks(PhilosopherData* philosopherSelected); void TakeForks(PhilosopherData* philosopherSelected); bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal); void Think(PhilosopherData* philosopherSelected); -void Up(sem_t* semaphore); +void AllowPhilosopher(); int main(int argc, char* argv[]) { @@ -52,23 +53,22 @@ int main(int argc, char* argv[]) bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) { for (int i = 0; i < numPhilosophers; i++) - if (PhilosopherList[i].eatingCount < 2) + if (PhilosopherList[i].eatingCount < 1) return false; return true; } // Philosopher enters critical section. -void Down(sem_t* semaphore) +void PhilosopherWait() { - sem_wait(semaphore); - printf("A semaphore was put into waiting...\n"); + printf("A thread was put into waiting...\n"); + sem_wait(&semaphore); } // Sets philosopher to eating state and increments eating count. void Eat(PhilosopherData* philosopherSelected) { printf("Philosopher %d is eating...\n", philosopherSelected->position); - philosopherSelected->state = EATING; ++philosopherSelected->eatingCount; } @@ -98,13 +98,17 @@ void PhilosopherInit(int philosopherTotal) { PhilosopherData PhilosopherList[philosopherTotal]; PhilosopherListInit(PhilosopherList, philosopherTotal); + sem_init(&semaphore, 0, 1); while (!AllPhilosophersFull(PhilosopherList, philosopherTotal)) { + sleep(2); for (int i = 0; i < philosopherTotal; i++) { 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].eatingCount = 0; - sem_init(&PhilosopherList[i].semaphore, 0, 1); 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. void TakeForks(PhilosopherData* philosopherSelected) { - printf("Philosopher %d is taking forks...\n", philosopherSelected->position); - Down(&philosopherSelected->semaphore); + philosopherSelected->state = HUNGRY; + printf("Philosopher %d is hungry...\n", philosopherSelected->position); + PhilosopherWait(); } // If philosopher is hungry and forks are free, then returns true. @@ -155,13 +158,13 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, // Philosopher begins thinking void Think(PhilosopherData* philosopherSelected) { - philosopherSelected->state = THINKING; printf("Philosopher %d is thinking...\n", philosopherSelected->position); + philosopherSelected->state = THINKING; } // Philosopher exits critical section -void Up(sem_t* semaphore) +void AllowPhilosopher() { - sem_post(semaphore); - printf("A semaphore was released...\n"); + sem_post(&semaphore); + printf("A thread was released...\n"); } diff --git a/DiningPhilosophers.out b/DiningPhilosophers.out index 59a07e9..2926701 100755 Binary files a/DiningPhilosophers.out and b/DiningPhilosophers.out differ