#include #include #include #include #include /* ASSUMPTIONS 1. Only up to 5 savages can attend the tribal communal dinner. (Test version only) 2. There is no limit on how many savages can come within a given time. 3. What they are eating is not missionary stew: it's "Survivor" stew. MWAHAHAHAHAHAHAHA!!!!!! */ /*global variables, threads, and mutexes*/ pthread_t thrSavages[5]; pthread_t thrCook; pthread_mutex_t lPot; pthread_mutex_t lCall; pthread_mutex_t lArrive; int iNumArrive = 0; int iCall = 0; int iPot; /*Process calls*/ void *savage (void *); void *cook (void *); /*main program*/ main() { int iTime = 0; int iSavageArrive[5]; //stores the arrival time of savages int i; int j; int iTempNA; //temporary variable holding //iNumArrive's max value int iIdArrive[5]; //stores the ID of those who //arrived at present time /*initialization of mutex variables*/ pthread_mutex_init(&lPot, pthread_mutexattr_default); pthread_mutex_init(&lCall, pthread_mutexattr_default); pthread_mutex_init(&lArrive, pthread_mutexattr_default); /*get input. Test version only*/ for(i=0; i<5; i++) { printf("\nEnter Savage %d 's arrival time: ", i); scanf("%d", &iSavageArrive[i]); } //Initialize the pot to contain 2 servings of //"Survivor" participants iPot = 2; /*MAIN LOOP STARTS HERE*/ for(iTime = 0; iTime < 10; iTime++) { printf("\nTime: %d", iTime); iNumArrive = 0; j = 0; /*gets the ID's of those who arrived in this time*/ for(i=0; i< 5; i++) { if (iSavageArrive[i] == iTime) { iNumArrive++; iIdArrive[j] = i; printf("\nSavage %d ",iIdArrive[j]); printf("arrived at time "); printf("%d.", iTime); j++; } } //printf("\n %d", iNumArrive); //gets iNumArrive's value since the said variable will //decremented by the threads... iTempNA = iNumArrive; //start the cook thread here /*This for loop calls the threads of the savages who arrived at this time*/ for(i=0; i< 5; i++) { for(j=0; j< iTempNA; j++) { if (i == iIdArrive[j]) { printf("\n%d", i); printf("eats! YUM!"); //call savage thread[i] here } } } //put the pthread join here joining this and the cook thread } sleep(3); printf("\nNo one wins Survivor. The savages get the one million dollars!!!"); printf("\n THE TRIBE HAS SPOKEN.\n"); }