From f202f7f40347f0e7f747697ef5cfb2b09d372b76 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 30 Sep 2022 00:03:37 -0500 Subject: [PATCH] Added pthread to program and adjusted philosopher methods --- DiningPhilosophers.c | 140 +++++++++++++++++++++++++++-------------- DiningPhilosophers.out | Bin 16472 -> 16656 bytes TestDining.sh | 4 +- crashCompile.log | 0 crashLaunch.log | 0 5 files changed, 96 insertions(+), 48 deletions(-) create mode 100644 crashCompile.log create mode 100644 crashLaunch.log diff --git a/DiningPhilosophers.c b/DiningPhilosophers.c index 0483df1..a8757fe 100644 --- a/DiningPhilosophers.c +++ b/DiningPhilosophers.c @@ -13,6 +13,7 @@ #define EATING 2 /*philosopher is eating*/ pthread_mutex_t mutex; /*mutual exclusion for critical region*/ + typedef struct PhilosopherData { int eatingCount; int position; @@ -21,15 +22,18 @@ typedef struct PhilosopherData { } PhilosopherData; bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers); -void Down(pthread_mutex_t selectedSemaphore); -void Eat(int selectedPhilosopher); -int GetNumberPhilosophers(char* argv); -void Philosopher(int selectedPhilosopher); -void PutForks(PhilosopherData PhilosopherList); -void TakeForks(PhilosopherData PhilosopherList); -void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers); -void Think(int selectedPhilosopher); -void Up(pthread_mutex_t selectedSemaphore); +void Down(); +void Eat(void* philosopherSelected); +int GetPhilosopherCount(char* argv); +int GetAbsolutePosition(PhilosopherData PhilosopherSelected); +void* Philosopher(void* philosopherSelected); +void PhilosopherInit(int philosopherTotal); +void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal); +void PutForks(void* philosopherSelected); +void TakeForks(void* philosopherSelected); +bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal); +void Think(void* philosopherSelected); +void Up(); /* * Todo: @@ -45,12 +49,19 @@ void Up(pthread_mutex_t selectedSemaphore); int main(int argc, char* argv[]) { - if (argc == 2) + if (argc < 2) { - int philosopherCount; - philosopherCount = GetNumberPhilosophers(argv[1]); - Philosopher(philosopherCount); + printf("Too few arguments, exiting...\n"); + return 0; } + if (argc > 2) + { + printf("Too many arguments, exiting...\n"); + return 0; + } + int philosopherTotal; + philosopherTotal = GetPhilosopherCount(argv[1]); + PhilosopherInit(philosopherTotal); return 0; } @@ -70,9 +81,9 @@ bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) // Turn function into picking up forks (starting waits) // (Right fork is owned by selectedPhilosopher number for consistency) // Add sem_wait -void Down(pthread_mutex_t selectedSemaphore) +void Down() { - printf("Function Down() was called.\n"); + printf("Down() was called...\n"); } // @@ -80,48 +91,83 @@ void Down(pthread_mutex_t selectedSemaphore) // Add changing state to function // Add increasing eating count functionality // -void Eat(int selectedPhilosopher) +void Eat(void* philosopherSelected) { - printf("Function Eat() was called.\n"); + philosopherSelected. + printf("Eat() was called...\n"); } // Takes in char* and converts into a number // Returns a number -int GetNumberPhilosophers(char* argv) +int GetPhilosopherCount(char* argv) { - int numPhilosophers; - sscanf(argv, "%d", &numPhilosophers); - return numPhilosophers; + int philosopherTotal; + sscanf(argv, "%d", &philosopherTotal); + return philosopherTotal; +} + +// Unused function to be removed +int GetAbsolutePosition(PhilosopherData philosopherSelected) +{ + ; } // Takes in the number of philosophers, from 0 to N-1 // Finishes when all philosophers have eaten twice // TODO: // Add splitting function into multiple pthreads -void Philosopher(int numPhilosophers) +void* Philosopher(void* philosopherSelected) { // Below replaced by PhilosopherData // int philosopherEatingCount[numPhilosophers]; // sem_t philosopherSemaphore[numPhilosophers]; /*one semaphore per philospher*/ // // array to keep track of everyone's state // int philosopherState[numPhilosophers]; - PhilosopherData PhilosopherList[numPhilosophers]; - int forkCount = numPhilosophers / 2; while (true) { - Think(0); /*philosopher is thinking*/ - // TakeForks(0, numPhilosophers); /*acquire two forks or block*/ - // Eat(0); /*yum-yum, spaghetti*/ - // PutForks(0, numPhilosophers); /*put both forks back on table*/ + Think(philosopherSelected); /*philosopher is thinking*/ + TakeForks(philosopherSelected); /*acquire two forks or block*/ + Eat(philosopherSelected); /*yum-yum, spaghetti*/ + PutForks(philosopherSelected); /*put both forks back on table*/ break; } } -void PutForks(PhilosopherData PhilosopherList) +void PhilosopherInit(int philosopherTotal) { + PhilosopherData PhilosopherList[philosopherTotal]; + pthread_t PhilosopherThreads[philosopherTotal]; + for (int i = 0; i < philosopherTotal; i++) + pthread_create(&PhilosopherThreads[i], NULL, &Philosopher, &PhilosopherList[i]); + for (int i = 0; i < philosopherTotal; i++) + pthread_join(PhilosopherThreads[i], NULL); + while (!AllPhilosophersFull(PhilosopherList, philosopherTotal)) + { + for (int i = 0; i < philosopherTotal; i++) + { + + if (PhilosopherCanEat(PhilosopherList, i, philosopherTotal)) + { + Down(PhilosopherList[i-1].semaphore); + Down(PhilosopherList[i].semaphore); + } + } + } +} + +void PhilosopherListInit(PhilosopherData PhilosopherList[], int philosopherTotal) +{ + for (int i = 0; i < philosopherTotal; i++) + { + PhilosopherList[i].position = i; + PhilosopherList[i].eatingCount = 0; + } +} + +void PutForks(void* philosopherSelected) +{ + printf("PutForks() was called...\n"); // Down(&mutex); /*enter critical region*/ - printf("%d is now thinking.\n", PhilosopherList.position); - PhilosopherList.state = THINKING; /*philosopher has finished eating*/ // philosopherState[selectedPhilosopher] = THINKING; /*record fact that philosopher i is hungry*/ // Test(selectedPhilosopher, numPhilosophers); // Test(LEFT); /*try to acquire 2 forks*/ @@ -130,10 +176,11 @@ void PutForks(PhilosopherData PhilosopherList) } // Takes in a number for a selected philosopher, from 0 to N-1 -void TakeForks(PhilosopherData PhilosopherList) +void TakeForks(void* philosopherSelected) { + printf("TakeForks() was called...\n"); // Down(&mutex); /*enter critical region*/ - PhilosopherList.state = HUNGRY; /*philosopher has finished eating*/ + // 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*/ @@ -142,29 +189,30 @@ void TakeForks(PhilosopherData PhilosopherList) // Takes in the number of philosophers, from 0 to N-1 // Checks if left and right philosopher is not eating, // and philosopher is hungry, then philosopher will eat -void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers) +bool PhilosopherCanEat(PhilosopherData PhilosopherList[], int selectedPosition, int philosopherTotal) { - if (PhilosopherList[positionSelected].state != HUNGRY) - return; - int leftPhilosopher = (positionSelected + (numPhilosophers-1)) % numPhilosophers; - int rightPhilosopher = (positionSelected + 1) % numPhilosophers; + if (PhilosopherList[selectedPosition].state != HUNGRY) + return false; + int leftPhilosopher = (selectedPosition + (philosopherTotal-1)) % philosopherTotal; + int rightPhilosopher = (selectedPosition + 1) % philosopherTotal; if (PhilosopherList[leftPhilosopher].state == EATING) - return; + return false; if (PhilosopherList[rightPhilosopher].state == EATING) - return; - PhilosopherList[positionSelected].state = EATING; - // Up(&philosopherSemaphore[selectedPhilosopher]); + return false; + return true; } -void Think(int selectedPhilosopher) +void Think(void* philosopherSelected) { - printf("Function Think() was called.\n"); + // printf("%d is now thinking.\n", PhilosopherList.position); + // PhilosopherList.state = THINKING; /*philosopher has finished eating*/ + printf("Think() was called...\n"); } // Todo: // Make function release hold on semaphore // Use 'sem_post' -void Up(pthread_mutex_t selectedSemaphore) +void Up() { - printf("Function Up() was called.\n"); + printf("Up() was called...\n"); } diff --git a/DiningPhilosophers.out b/DiningPhilosophers.out index b61488bd582a0c8ef8c12496b96aeffb9aed76ff..43d9529eb2bba69f8385e569525976fb76bb2dde 100755 GIT binary patch delta 4020 zcmaJ^4Nz3q6~1qOfF&rq3%a7{?xH5E;w~V+0?KMOJ`7eI#l*1DR>ugTDW;g$bKkpf#lPH{ z_r7z_`OdlL{=WML+PTi>xz1_&K`jwWg#@I!ZSviZOKQIq{`7G&~sM% zS*28!k+29HsuS?(S}57O%J0TDg;bBi9?qcaVXPj;>VaQ#7tK)<{nW*Bx5i+Swi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8ir9_(7s#7e@_SPJDDs~n|GSmE z@CiI$s9zUD?j-J}Vb5`|5>E|y@q~)<6CN|qlL%E7(<<>E{ zvDrq)fjV7u2HZ{?orOlH%QVK{ll-GY{1A28%oo`w5C{K&k|iBncJ9OFCYOz#0k^AN zhHm~vZ^ZK&+B{Drf^i7gg74em0FwF39YL>?qwuj0dA zR%}!QEwi}yB5TI7>g1Cn=%EDPjY)ZL~&J+v%C0aJ2)F+cELo{L*Qm|jODVv z#8(|=_c;z8n!F)b@;+zVc}%sE54rsscb9KdTs;Mi*yE?HQ|lM#V-l<+!^`5P#x0mY z_CGwr0zZ-;aEC0ovBfg59`67n_TAx4tz*&^zUmS`;B}nd#Qlw4Zs4#L?QAE{@+>Ot z;r3qMwQ_!hY)3oR4DXhkbo)@Y<88IZ+QGdK@@^eGdMD5dOYzScD;QEgA)FoIt15Yp zlF9lC4TYyFn{s!zh4%G5d!((Wp(D`Q)!WtG(bm%o=PS3Fl^-7O?!9}q?`SS_mN_e6 zsWKmmszUna<^zxIX!h;f32*B1Cv{s-)5+0oeo7Z%qS^u()p@2Ft&&>BS#VdoOlSDN zriwZ6T(wI-&aLBcwz?{FiEH?1-KwrMsZX4WesI?e&<2>Oap}AH)_21~O%=@77^xTB z9#`_OS3R03E%bPtnd)6PlnduPyXY^#{e2r4YmKI1yHc!*L*TA;nT>pJRa~UdQ|pBJ z+ETb$Yt)bPU)yn5sIAhgPhkv->#CAZ-5CDsu;GX2>LekCWq2#~@p8rZGE8`@gr8Nx IE$_Pj0Tv_2MgRZ+ diff --git a/TestDining.sh b/TestDining.sh index 0e60c98..f4d2018 100755 --- a/TestDining.sh +++ b/TestDining.sh @@ -1,4 +1,4 @@ #!/bin/bash rm DiningPhilosophers.out -gcc DiningPhilosophers.c -o DiningPhilosophers.out -./DiningPhilosophers.out 5 +gcc DiningPhilosophers.c -o DiningPhilosophers.out 2> crashCompile.log +./DiningPhilosophers.out 5 2> crashLaunch.log diff --git a/crashCompile.log b/crashCompile.log new file mode 100644 index 0000000..e69de29 diff --git a/crashLaunch.log b/crashLaunch.log new file mode 100644 index 0000000..e69de29