From 31000d2116ebdd136ac1136a2899aac68dde2ace Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Tue, 27 Sep 2022 22:58:47 -0500 Subject: [PATCH] Fixed errors and program runs --- DiningPhilosophers | Bin 0 -> 16472 bytes DiningPhilosophers.c | 108 +++++++++++++++++++++++++++---------------- 2 files changed, 69 insertions(+), 39 deletions(-) create mode 100755 DiningPhilosophers diff --git a/DiningPhilosophers b/DiningPhilosophers new file mode 100755 index 0000000000000000000000000000000000000000..0768d3ec9a4390db2cd2a28405e435d00ef14ba8 GIT binary patch literal 16472 zcmeHOdu&tJ89#QC63QciLV0u))0GwqF@%O!JBEQc+)M!iBwLxrE{^RaRvbICug62B zgSm9`LbP;`uG1=w{ZX}3D~)#T#MVhmV1t4v3TxFdZIjZ9R!L!Pc&xZxiue1@J)eDZ z>(FhQrfKq#u7Bryo^!r)?)5qMp7S+tU5(S>5S)s{X9ZC`6FQXpEB8kk!wgucB7@~V@wI7Q0*YQ5)vue zj{Buh2u7A?%X5)2l1u*PP}N^BTFRr?9I7z&53L%B9V#v_E9CNsB)h}P?y$0B9HI&# zF>*X9CUmV(^|0e&GD2ds+qEh?J64-!f|1IgwDm0`@b8pYuj~%Gq#I$!hfTI%~lt6CHeRD?s3#IA~6E0$F(t8&Mq?j^DcR4*4U z8aC@UHVU}uAs!C{=RT8Wesn>X&%&0}UQBOR(Y*^mw; z^2bAjXv&YpMLK3Y_9JKV2+s!3v=fe&$p7R#!iT|g<>B7}UOxThqwuv+_=muE;j*W@ z!N}+TXW+Z+%Ag&%s7@bCy6)fQ)0;yPUpTZUD0Cei=-1HgL>z1bHEi;h?*Vl)dfrp{g_*4NdpU8gT`FVE(xvbiPhrP=(w zRYI?A*q{f3v0zImZUkcu8`g!Rkzj+bDNOZgX^%!!eRNYbuOb@eL@SGMHx~g*WS-hx zEH**L<9b>sG=WCnHpQDF>0;qmbYf-*!=y;iU?7_p9(@eM#VK|pPl=yL{2Vwm$>cw+ zo;S?jq4=d9Q_g&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F6?we))Q^@42|7h*{we&MZ49m9y?8U@j;+E|GdG_?S)SoW+eq#PGGWqsd&p<$2tYpDsl8>i{$HGK*Dfp8RPmkiy zQ{wx<&*#5R^2^0)CFJMLX2sv5`0^bCe6@gIE@p^w~59f2UI-GaVf-F@TpyPKO4b!nN=9^+p7F*Q}LXmq+#&y z7@=tr-ynS_{{3vvhc4ykZdD)o-ib`M`eR0=JBp3?zNV0F_*w)QaU;>(?DmV?rhVOL z*Zs6@pUw&dqIyd>+T;uC0V5iV>%PP;;g7a=go6g&jV{iwL1z#`x-S;enREm4(;2mQj0fdP_+ zI<%v*)7`;VHMm;?@Wzzs!yM(N1?N$euRR23ra6Kx+_=5kao6Oz}l@K_B_9598%>{9VyP-ex7HCz@w=i+w*#X z@g!u{L_VzPDk-Nbp|e14(mSzdz#BL&GUK2dR3Uc{#=JHWGJR=&+7}u z9;IlHAGiMp%6_fVpq<1cOYJxaj%ylud<9lQOF&7S9njJ!@} zeVaS+J7v%PFI-Af;kOjYV7Gq-0*XJ|A9R_Tj1QY+_SbIzCrGUJ<#S9`#vu|~5|1Bd zy@kv~T)Zyj^*^73wd*s@cnP*;7JGg_@N@;3NUUd4Ihy$&K|o`d%jfkApKIp&)BQ(@ z?YJMG0gsx<_Pm}sw1~8Y*dc#&KWxu<6O66)!lQH-o5raU4VYPA)0CO(&(EK+;BnneOY?W2 b%EZTP&beIHrD=XX`-hgB8Z&JMHd6cp<7TMu literal 0 HcmV?d00001 diff --git a/DiningPhilosophers.c b/DiningPhilosophers.c index 53756f8..60f938b 100644 --- a/DiningPhilosophers.c +++ b/DiningPhilosophers.c @@ -1,28 +1,35 @@ #include #include +#include #include #include -#include +#include -#define N 5 /*number of philosophers*/ -#define LEFT (i+N-1)%N /*number of i's left neighbor*/ -#define RIGHT (i+1)%N /*number of i's right neighbor*/ +// #define N 5 /*number of philosophers*/ +// #define LEFT (i+N-1)%N /*number of i's left neighbor*/ +// #define RIGHT (i+1)%N /*number of i's right neighbor*/ #define THINKING 0 /*philosopher is thinking*/ #define HUNGRY 1 /*philosopher is trying to get forks*/ #define EATING 2 /*philosopher is eating*/ -int philosopherState[N]; /*array to keep track of everyone's state*/ -sem_t mutex = 1; /*mutual exclusion for critical region*/ -sem_t philosopherSemaphore[N]; /*one semaphore per philospher*/ +pthread_mutex_t mutex; /*mutual exclusion for critical region*/ -void Down(sem_t* selectedSemaphore); +typedef struct PhilosopherData { + int eatingCount; + int position; + sem_t semaphore; + int state; +} 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(int selectedPhilosopher); -void TakeForks(int selectedPhilosopher); -void Test(int selectedPhilosopher); +void PutForks(PhilosopherData PhilosopherList); +void TakeForks(PhilosopherData PhilosopherList); +void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers); void Think(int selectedPhilosopher); -void Up(sem_t* selectedSemaphore); +void Up(pthread_mutex_t selectedSemaphore); /* * Todo: @@ -50,11 +57,22 @@ 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 +bool AllPhilosophersFull(PhilosopherData PhilosopherList[], int numPhilosophers) +{ + for (int i = 0; i < numPhilosophers; i++) + if (PhilosopherList[i].eatingCount != 2) + return false; + return true; +} + // // TODO: // Turn function into putting down forks (releasing waits) -// -void Down(sem_t* selectedSemaphore) +// sem_wait +void Down(pthread_mutex_t selectedSemaphore) { printf("Function Down() was called.\n"); } @@ -82,49 +100,60 @@ int GetNumberPhilosophers(char* argv) // Finishes when all philosophers have eaten twice void Philosopher(int numPhilosophers) { - int stateEatingCount[numPhilosophers]; + // 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(); /*philosopher is thinking*/ - TakeForks(numPhilosophers); /*acquire two forks or block*/ - Eat(); /*yum-yum, spaghetti*/ - PutForks(numPhilosophers); /*put both forks back on table*/ + break; + // 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*/ } } -void PutForks(int selectedPhilosopher) /*i: philosopher number, from 0 to N-1*/ +void PutForks(PhilosopherData PhilosopherList) { - Down(&mutex); /*enter critical region*/ - printf("%d is now thinking.\n", selectedPhilosopher); - philosopherState[selectedPhilosopher] = THINKING; /*record fact that philosopher i is hungry*/ - Test((i+N-1)%N); /*try to acquire 2 forks*/ - Test(RIGHT); /*exit critical region*/ - Up(&mutex); /*block if forks were not acquired*/ + // 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*/ + // Test(RIGHT); /*exit critical region*/ + // Up(&mutex); /*block if forks were not acquired*/ } // Takes in a number for a selected philosopher, from 0 to N-1 -void TakeForks(int selectedPhilosopher) +void TakeForks(PhilosopherData PhilosopherList) { - Down(&mutex); /*enter critical region*/ - philosopherState[selectedPhilosopher] = 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*/ + // 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*/ } // 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(int selectedPhilosopher) +void Test(PhilosopherData PhilosopherList[], int positionSelected, int numPhilosophers) { - if (philosopherState[selectedPhilosopher] != HUNGRY) + if (PhilosopherList[positionSelected].state != HUNGRY) return; - if (philosopherState[(selectedPhilosopher+N-1)%N LEFT] == EATING) + int leftPhilosopher = (positionSelected + (numPhilosophers-1)) % numPhilosophers; + int rightPhilosopher = (positionSelected + 1) % numPhilosophers; + if (PhilosopherList[leftPhilosopher].state == EATING) return; - if (philosopherState[RIGHT] == EATING) + if (PhilosopherList[rightPhilosopher].state == EATING) return; - philosopherState[selectedPhilosopher] = EATING; - Up(&philosopherSemaphore[selectedPhilosopher]); + PhilosopherList[positionSelected].state = EATING; + // Up(&philosopherSemaphore[selectedPhilosopher]); } void Think(int selectedPhilosopher) @@ -134,7 +163,8 @@ void Think(int selectedPhilosopher) // Pick up for on left and right // (Right fork is owned by selectedPhilosopher number for consistency) -void Up(sem_t* selectedSemaphore) +// sem_post +void Up(pthread_mutex_t selectedSemaphore) { printf("Function Up() was called.\n"); }