Removed more unused code
This commit is contained in:
parent
38da411a83
commit
4ef4867d50
@ -8,7 +8,6 @@
|
|||||||
#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*/
|
||||||
// pthread_mutex_t mutex; /*mutual exclusion for critical region*/
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct PhilosopherData {
|
typedef struct PhilosopherData {
|
||||||
@ -32,16 +31,6 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition,
|
|||||||
void Think(PhilosopherData* philosopherSelected);
|
void Think(PhilosopherData* philosopherSelected);
|
||||||
void Up(sem_t* semaphore);
|
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[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
@ -68,11 +57,7 @@ bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a pointer to a philosopher and puts semaphore into waiting state.
|
// Philosopher enters critical section.
|
||||||
// TODO:
|
|
||||||
// Turn function into picking up forks (starting waits)
|
|
||||||
// (Right fork is owned by selectedPhilosopher number for consistency)
|
|
||||||
// Add sem_wait
|
|
||||||
void Down(sem_t* semaphore)
|
void Down(sem_t* semaphore)
|
||||||
{
|
{
|
||||||
sem_wait(semaphore);
|
sem_wait(semaphore);
|
||||||
@ -105,7 +90,6 @@ void* Philosopher(void* philosopherPassed)
|
|||||||
TakeForks(philosopherSelected); /*acquire two forks or block*/
|
TakeForks(philosopherSelected); /*acquire two forks or block*/
|
||||||
Eat(philosopherSelected); /*yum-yum, spaghetti*/
|
Eat(philosopherSelected); /*yum-yum, spaghetti*/
|
||||||
PutForks(philosopherSelected); /*put both forks back on table*/
|
PutForks(philosopherSelected); /*put both forks back on table*/
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,13 +104,13 @@ void PhilosopherInit(int philosopherTotal)
|
|||||||
{
|
{
|
||||||
if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal))
|
if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal))
|
||||||
{
|
{
|
||||||
Up(&(PhilosopherList[i-1].semaphore));
|
|
||||||
Up(&(PhilosopherList[i].semaphore));
|
Up(&(PhilosopherList[i].semaphore));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initializes full list of philosophers.
|
||||||
void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal)
|
void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < philosopherTotal; i++)
|
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)
|
void PutForks(PhilosopherData* philosopherSelected)
|
||||||
{
|
{
|
||||||
printf("Philosopher %d is putting forks down...\n", philosopherSelected->position);
|
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.
|
// 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);
|
printf("Philosopher %d is taking forks...\n", philosopherSelected->position);
|
||||||
Down(&philosopherSelected->semaphore);
|
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.
|
// If philosopher is hungry and forks are free, then returns true.
|
||||||
@ -178,17 +152,14 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Philosopher begins thinking
|
||||||
void Think(PhilosopherData* philosopherSelected)
|
void Think(PhilosopherData* philosopherSelected)
|
||||||
{
|
{
|
||||||
// printf("%d is now thinking.\n", PhilosopherList.position);
|
|
||||||
// PhilosopherList.state = THINKING; /*philosopher has finished eating*/
|
|
||||||
philosopherSelected->state = THINKING;
|
philosopherSelected->state = THINKING;
|
||||||
printf("Philosopher %d is thinking...\n", philosopherSelected->position);
|
printf("Philosopher %d is thinking...\n", philosopherSelected->position);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Todo:
|
// Philosopher exits critical section
|
||||||
// Make function release hold on semaphore
|
|
||||||
// Use 'sem_post'
|
|
||||||
void Up(sem_t* semaphore)
|
void Up(sem_t* semaphore)
|
||||||
{
|
{
|
||||||
sem_post(semaphore);
|
sem_post(semaphore);
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user