From 4ef4867d50bf911454157ea92fb8e8ae77ab4486 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 30 Sep 2022 15:46:39 -0500 Subject: [PATCH] Removed more unused code --- DiningPhilosophers.c | 49 +++++++++-------------------------------- DiningPhilosophers.out | Bin 16768 -> 16768 bytes 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/DiningPhilosophers.c b/DiningPhilosophers.c index 9022677..efab6a2 100644 --- a/DiningPhilosophers.c +++ b/DiningPhilosophers.c @@ -8,7 +8,6 @@ #define THINKING 0 /*philosopher is thinking*/ #define HUNGRY 1 /*philosopher is trying to get forks*/ #define EATING 2 /*philosopher is eating*/ -// pthread_mutex_t mutex; /*mutual exclusion for critical region*/ typedef struct PhilosopherData { @@ -32,16 +31,6 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, void Think(PhilosopherData* philosopherSelected); 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[]) { if (argc < 2) @@ -57,9 +46,9 @@ int main(int argc, char* argv[]) 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 +// 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++) @@ -68,18 +57,14 @@ bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) return true; } -// Takes a pointer to a philosopher and puts semaphore into waiting state. -// TODO: -// Turn function into picking up forks (starting waits) -// (Right fork is owned by selectedPhilosopher number for consistency) -// Add sem_wait +// Philosopher enters critical section. void Down(sem_t* semaphore) { sem_wait(semaphore); printf("A semaphore was put into waiting...\n"); } -// Sets philosopher to eating state and increments eating count. +// Sets philosopher to eating state and increments eating count. void Eat(PhilosopherData* philosopherSelected) { printf("Philosopher %d is eating...\n", philosopherSelected->position); @@ -87,7 +72,7 @@ void Eat(PhilosopherData* philosopherSelected) ++philosopherSelected->eatingCount; } -// Converts char* to number. +// Converts char* to number. int GetPhilosopherCount(char* argv) { int philosopherTotal; @@ -105,7 +90,6 @@ void* Philosopher(void* philosopherPassed) TakeForks(philosopherSelected); /*acquire two forks or block*/ Eat(philosopherSelected); /*yum-yum, spaghetti*/ PutForks(philosopherSelected); /*put both forks back on table*/ - break; } } @@ -120,13 +104,13 @@ void PhilosopherInit(int philosopherTotal) { if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal)) { - Up(&(PhilosopherList[i-1].semaphore)); Up(&(PhilosopherList[i].semaphore)); } } } } +// Initializes full list of philosophers. void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal) { 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) { 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. @@ -155,11 +134,6 @@ void TakeForks(PhilosopherData* philosopherSelected) { printf("Philosopher %d is taking forks...\n", philosopherSelected->position); 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. @@ -178,17 +152,14 @@ bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, return true; } +// Philosopher begins thinking void Think(PhilosopherData* philosopherSelected) { - // printf("%d is now thinking.\n", PhilosopherList.position); - // PhilosopherList.state = THINKING; /*philosopher has finished eating*/ philosopherSelected->state = THINKING; printf("Philosopher %d is thinking...\n", philosopherSelected->position); } -// Todo: -// Make function release hold on semaphore -// Use 'sem_post' +// Philosopher exits critical section void Up(sem_t* semaphore) { sem_post(semaphore); diff --git a/DiningPhilosophers.out b/DiningPhilosophers.out index 36d8c49c261b2706e0c2e3acd484f8378ecf4df7..59a07e9cefb66c0e8047cffd622dddd04735860f 100755 GIT binary patch delta 700 zcmY*VUuaTc6u-x-&KqWP=Qev#uRQdMVEJjTr#_s|B8AgixMpsFygTt64kwRt^%`V1B O6N^6!-O9TEiRvGZhBf>tof}%%xdd(WvBO8b%A&U9bf)C$Fro`9h=>MWd^7{sB&P+Z_fr;hZl33Tp7(j*b52Zxm;?)Y zFx2#15yarj_tYjJnugWqReK{6=pk0Zw@ykP_=YFu(nMX9Ob{b)t&UcJLe?r4? zp_I54O6KGl%x-gmF}BV9sAn?y6)ISU!1D$dtl!Fs1!d9tmDJhQ)TsiteoMYnh6Gg{ z7tc?tERN_=Q=4`pTITp)#fI1w%+y+m|0Tpp$zb%ybnyIjrW_KhKBZ9@#Nbz z#z;5s+WDV)8r8-NrL10whsiy@WIl|)iB&TwZZ(3uRq+pE5xT40-E5GnRr<>AXnF3A zK>#)x`vM&IyTTZUZyfl=m>Y2d@fhMP;soM&m9ZG&L&RD7qPq`BY?mh7kI{`XK3Tb~ z*ltRwf^9lgu(btOxlxLszhi^3I^sWnB(0V->hFMEPL!{rw@G@1E?owhzCun4ow^02 z_6oTqbZXxt9VAJiOIt?viOeH2;Z=UpyztaOZg>WvL>E0dH5BRe{zCu~S@iKxB6*(| z;-uyqfhBU>?}7+9@84tSvEWWTb$Z{=gTrNOmI0itoM$+>gEMh&$?}0Y7^4>h*Y%Jl WYe&6?lD*kdB97r44A8aV$Ev^Yr~+jG