Philosophers eat, but lacks sem_post order
This commit is contained in:
parent
a4bcbc250f
commit
70bc58707a
@ -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");
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user