//=================================================================== // HUNTER //=================================================================== void hunter(int time) { pid_t localpid = getpid(); setpgid(localpid, hunterGID); printf("HUNTERHUNTERHUNTER Hunter[%d] is looking for the path\n", localpid); int pathtime = random(); if (usleep(time) == -1) { /* exit when usleep interrupted by kill signal */ if (errno == EINTR)exit(4); } semopChecked(semID, &SignalNHunterPath, 1); semopChecked(semID, &WaitPHunterPath, 1); *numHunterPath = *numHunterPath + 1; semopChecked(semID, &SignalPHunterPath, 1); printf("HUNTERHUNTERHUNTER Hunter[%d] enters the magic path, now %d hunters in path\n", localpid, *numHunterPath); semopChecked(semID, &SignalSDragonWakeUp, 1); semopChecked(semID, &WaitSHunterCave, 1); printf("HUNTERHUNTERHUNTER Hunter[%d] enters the cave\n", localpid); semopChecked(semID, &SignalSDragonFight, 1); semopChecked(semID, &WaitSHunterFight, 1); semopChecked(semID, &WaitPHunterLeave, 1); *numHunterLeave = *numHunterLeave + 1; printf("HUNTERHUNTERHUNTER Hunter[%d] leaves, now Smaug has fought %d hunters\n", localpid, *numHunterLeave); semopChecked(semID, &SignalPHunterLeave, 1); if(checkHunter()) { terminateSimulation(); } exit(0); }
//=================================================================== // THIEF //=================================================================== void thief(int time) { pid_t localpid = getpid(); setpgid(localpid, thiefGID); printf("THIEFTHIEFTHIEFTHIEF Thief[%d] is looking for the path\n", localpid); if (usleep(time) == -1) { /* exit when usleep interrupted by kill signal */ if (errno == EINTR)exit(4); } semopChecked(semID, &SignalNThiefPath, 1); semopChecked(semID, &WaitPThiefPath, 1); *numThiefPath = *numThiefPath + 1; semopChecked(semID, &SignalPThiefPath, 1); printf("THIEFTHIEFTHIEFTHIEF Thief[%d] enters the magic path, now %d thieves in path\n", localpid, *numThiefPath); semopChecked(semID, &SignalSDragonWakeUp, 1); semopChecked(semID, &WaitSThiefCave, 1); printf("THIEFTHIEFTHIEFTHIEF Thief[%d]enters the cave\n", localpid); semopChecked(semID, &SignalSDragonPlay, 1); semopChecked(semID, &WaitSThiefPlay, 1); semopChecked(semID, &WaitPThiefLeave, 1); *numThiefLeave = *numThiefLeave + 1; printf("THIEFTHIEFTHIEFTHIEF Thief[%d] leaves, now Smaug has played with %d thieves\n", localpid, *numThiefLeave); semopChecked(semID, &SignalPThiefLeave, 1); if(checkThief()) { terminateSimulation(); } exit(0); }
//========================================================= //Smaug //========================================================= //smaug eats a meal void eat() { semopChecked(semID, &WaitNMeal, 1); //guarantee that there is really a meal int i; // let the sheep and cows be ready to be eaten for (i = 0; i < SHEEP_IN_MEAL; i++) { semopChecked(semID, &SignalSSheepWaiting, 1); } for (i = 0; i < COW_IN_MEAL; i++) { semopChecked(semID, &SignalSCowWaiting, 1); } //printf("Smaug waits to eat each sheep and cow in the meal\n"); semopChecked(semID, &WaitSDragonEat, 1); printf("DRAGONDRAGONDRAGON Smaug starts eating\n"); int j; //deal with sheep for (j = 0; j < SHEEP_IN_MEAL; j++) { semopChecked(semID, &SignalSSheepEaten, 1); } //deal with cows for (j = 0; j < COW_IN_MEAL; j++) { semopChecked(semID, &SignalSCowEaten, 1); } //waiting the meal to be done semopChecked(semID, &WaitSMealDone, 1); printf("DRAGONDRAGONDRAGON Smaug finishes eating\n"); //decrement the number of meals *numMeal = *numMeal - 1; semopChecked(semID, &WaitPEatenMeal, 1); *numEatenMeal = *numEatenMeal + 1; printf("DRAGONDRAGONDRAGON Smaug eats one more snack,%d snacks eaten, %d snacks left\n", *numEatenMeal, *numMeal); semopChecked(semID, &SignalPEatenMeal, 1); }
int checkTermination() { semopChecked(semID, &WaitPTermination, 1); if (*flagTermination == 1) { semopChecked(semID, &SignalPTermination, 1); return 1; } else { semopChecked(semID, &SignalPTermination, 1); return 0; } }
int checkHunter() { semopChecked(semID, &WaitPHunterLeave, 1); if (*numHunterLeave >= MAX_TREASUREHUNTER) { printf("Smaug has fought with more than maximum number of hunters, the simulation will terminate\n"); setTerminate(); semopChecked(semID, &SignalPHunterLeave, 1); return 1; } else { semopChecked(semID, &SignalPHunterLeave, 1); return 0; } }
int checkThief() { semopChecked(semID, &WaitPThiefLeave, 1); if (*numThiefLeave >= MAX_THIEF) { printf("Smaug has played with more than maximum number of thieves, the simulation will terminate\n"); setTerminate(); semopChecked(semID, &SignalPThiefLeave, 1); return 1; } else { semopChecked(semID, &SignalPThiefLeave, 1); return 0; } }
int checkCow() { semopChecked(semID, &WaitPCowEaten, 1); if (*numCowEaten >= MAX_COW) { printf("Smaug has eaten more than the maximum number of cows, the simulation will terminate\n"); setTerminate(); semopChecked(semID, &SignalPCowEaten, 1); return 1; } else { semopChecked(semID, &SignalPCowEaten, 1); return 0; } }
int checkSheep() { semopChecked(semID, &WaitPSheepEaten, 1); if (*numSheepEaten >= MAX_SHEEP) { printf("Smaug has eaten more than maximum number of sheep, the simulation will terminate\n"); setTerminate(); semopChecked(semID, &SignalPSheepEaten, 1); return 1; } else { semopChecked(semID, &SignalPSheepEaten, 1); return 0; } }
int checkJewel() { semopChecked(semID, &WaitPDragonJewel, 1); if (*numDragonJewel >= MAX_JEWEL) { printf("Dragon has more than maximum number of jewels, the simulation will terminate\n"); setTerminate(); semopChecked(semID, &SignalPDragonJewel, 1); return 1; } else if (*numDragonJewel <= MIN_JEWEL) { printf("Dragon has fewer than minimum number of jewels, the simulation will terminate\n"); setTerminate(); semopChecked(semID, &SignalPDragonJewel, 1); return 1; } else { semopChecked(semID, &SignalPDragonJewel, 1); return 0; } }
void hunter(int startTimeN) { int localpid; localpid = getpid(); printf("HUNTER %8d HUNTER A hunter arrived outside the valley\n", localpid); if( startTimeN > 0) { if( usleep( startTimeN) == -1) { /* exit when usleep interrupted by kill signal */ if(errno==EINTR)exit(4); } // Terminate check semopChecked(semID, &WaitProtectTerminate, 1); if( *terminateFlagp == 1 ) { printf("HUNTER %8d HUNTER hunter has found the magical path after we've been told to terminate\n", localpid); semopChecked(semID, &SignalProtectTerminate, 1); kill(localpid, SIGKILL); return; } else { printf("HUNTER %8d HUNTER hunter has found the magical path in %d ms\n", localpid, startTimeN); semopChecked(semID, &SignalProtectTerminate, 1); } printf("HUNTER %8d HUNTER A hunter was looking for path for a %f ms\n", localpid, startTimeN/1000.0); semopChecked(semID, &WaitProtectHunterCount, 1); *hunterCounterp = *hunterCounterp + 1; semopChecked(semID, &SignalProtectHunterCount, 1); printf("HUNTER %8d HUNTER hunter is under smaug's spell and is waiting to be interacted with\n", localpid); printf("HUNTER %8d HUNTER hunter wakes smaug\n", localpid); semopChecked(semID, &SignalDragonSleeping, 1); semopChecked(semID, &WaitHuntersWaiting, 1); printf("HUNTER %8d HUNTER hunter enters smaug's cave\n", localpid); printf("HUNTER %8d HUNTER hunter fights with smaug\n", localpid); semopChecked(semID, &WaitHunterFinish, 1); printf("HUNTER %8d HUNTER hunter leaves cave and goes home\n", localpid); kill(localpid, SIGKILL); // KILL AT THE END } }
//smaug fights the treasure hunter void fight() { semopChecked(semID, &WaitNHunterPath, 1); *numHunterPath = *numHunterPath - 1; printf("DRAGONDRAGONDRAGON Smaug is ready to interact with a hunter waiting in the path\n"); semopChecked(semID, &SignalSHunterCave, 1); semopChecked(semID, &WaitSDragonFight, 1); printf("DRAGONDRAGONDRAGON Smaug starts fighting with the hunter\n"); int fightrand = (double)rand()/RAND_MAX; if (fightrand > THIEF_WIN) { semopChecked(semID, &WaitPDragonJewel, 1); *numDragonJewel = *numDragonJewel + 5; printf("DRAGONDRAGONDRAGON Smaug wins and gets 5 jewels, now smaug has %d jewels\n", *numDragonJewel); semopChecked(semID, &SignalPDragonJewel, 1); } else { semopChecked(semID, &WaitPDragonJewel, 1); printf("DRAGONDRAGONDRAGON The hunter fights well and will get rewarded with 10 jewels\n"); if(*numDragonJewel >= 10 ) { *numDragonJewel = *numDragonJewel - 10; printf("DRAGONDRAGONDRAGON Smaug gives 10 jewels, now it has %d jewels\n", *numDragonJewel); } else { printf("DRAGONDRAGONDRAGON Smaug does not have enough jewels, the simulation will terminate\n"); setTerminate(); terminateSimulation(); } semopChecked(semID, &SignalPDragonJewel, 1); } if(checkJewel()) { terminateSimulation(); } semopChecked(semID, &SignalSHunterFight, 1); }
//smaug plays with the thief void play() { semopChecked(semID, &WaitNThiefPath, 1); *numThiefPath = *numThiefPath - 1; printf("DRAGONDRAGONDRAGON Smaug is ready to interact with a thief waiting in the path\n"); semopChecked(semID, &SignalSThiefCave, 1); semopChecked(semID, &WaitSDragonPlay, 1); printf("DRAGONDRAGONDRAGON Smaug fights with the hunter\n"); int fightrand = (double)rand()/RAND_MAX; if (fightrand > HUNTER_WIN) { semopChecked(semID, &WaitPDragonJewel, 1); *numDragonJewel = *numDragonJewel + 20; printf("DRAGONDRAGONDRAGON Smaug wins and gets 20 jewels, now smaug has %d jewels\n", *numDragonJewel); semopChecked(semID, &SignalPDragonJewel, 1); } else { semopChecked(semID, &WaitPDragonJewel, 1); printf("DRAGONDRAGONDRAGON Smaug loses and loses 8 jewels\n"); if(*numDragonJewel >= 8) { *numDragonJewel = *numDragonJewel - 8; printf("DRAGONDRAGONDRAGON Smaug loses 8 jewels, now it has %d jewels\n", *numDragonJewel); } else { printf("DRAGONDRAGONDRAGON Smaug has only %d jewels, now simulation terminates\n", *numDragonJewel); setTerminate(); terminateSimulation(); } semopChecked(semID, &SignalPDragonJewel, 1); } if(checkJewel()) { terminateSimulation(); } semopChecked(semID, &SignalSThiefPlay, 1); }
//======================================================== //Cow //======================================================== void cow(int time) { pid_t localpid = getpid(); setpgid(localpid, beastGID); printf("COWCOWCOWCOWCOWCOW Cow[%d] is grazing for %d usec\n", localpid, time); //grazing if (usleep(time) == -1) { /* exit when usleep interrupted by kill signal */ if (errno == EINTR) exit(4); } //the cow is enchanted //keep in this order in all files to avoid deadlock semopChecked(semID, &WaitPSheepInValley, 1); //use the number of sheep in valley semopChecked(semID, &WaitPCowInValley, 1); //use the number of cows in valley *CowInValley = *CowInValley + 1; semopChecked(semID, &SignalNCowInValley, 1); printf("COWCOWCOWCOWCOWCOW Cow[%d] is enchanted, now %d cows and %d sheep in Valley\n", localpid, *CowInValley, *SheepInValley); // There is a meal in the Valley if (*CowInValley >= COW_IN_MEAL && *SheepInValley >= SHEEP_IN_MEAL) { int i; // decrement the counters only when the semaphores are decremented for (i = 0; i < COW_IN_MEAL; i++) { semopChecked(semID, &WaitNCowInValley, 1); *CowInValley = *CowInValley - 1; } for (i = 0; i < SHEEP_IN_MEAL; i++) { semopChecked(semID, &WaitNSheepInValley, 1); *SheepInValley = *SheepInValley - 1; } //increment the number of meals semopChecked(semID, &WaitPNumMeal, 1); semopChecked(semID, &SignalNMeal, 1); *numMeal = *numMeal + 1; printf("COWCOWCOWCOWCOWCOW Cow[%d] results in a new meal, now number of meals:%d\n", localpid, *numMeal); semopChecked(semID, &SignalPNumMeal, 1); //release the use of shared variables semopChecked(semID, &SignalPCowInValley, 1); semopChecked(semID, &SignalPSheepInValley, 1); //send a wakeup to dragon semopChecked(semID, &SignalSDragonWakeUp, 1); } else { //the cow in the Valley is not enough for a meal semopChecked(semID, &SignalPCowInValley, 1); semopChecked(semID, &SignalPSheepInValley, 1); } // printf("Cow %d waiting in the snack\n", localpid); semopChecked(semID, &WaitSCowWaiting, 1); semopChecked(semID, &SignalNCowToEat, 1); //Wait in this order to avoid deadlock semopChecked(semID, &WaitPSheepToEat, 1); semopChecked(semID, &WaitPCowToEat, 1); *numCowToEat = *numCowToEat + 1; if (*numCowToEat >= COW_IN_MEAL && *numSheepToEat >= SHEEP_IN_MEAL) { int i; for (i = 0; i < COW_IN_MEAL; i++) { semopChecked(semID, &WaitNCowToEat, 1); *numCowToEat = *numCowToEat - 1; } for (i = 0; i < SHEEP_IN_MEAL; i++) { semopChecked(semID, &WaitNSheepToEat, 1); *numSheepToEat = *numSheepToEat - 1; } semopChecked(semID, &SignalPCowToEat, 1); semopChecked(semID, &SignalPSheepToEat, 1); semopChecked(semID, &SignalSDragonEat, 1); } else { semopChecked(semID, &SignalPCowToEat, 1); semopChecked(semID, &SignalPSheepToEat, 1); } // it waits to be eaten semopChecked(semID, &WaitSCowEaten, 1); semopChecked(semID, &WaitPCowEaten, 1); *numCowEaten = *numCowEaten + 1; semopChecked(semID, &SignalPCowEaten, 1); printf("COWCOWCOWCOWCOWCOW Cow[%d] is eaten, now %d cows eaten\n", localpid, *numCowEaten); if(checkCow()) { terminateSimulation(); exit(0); } semopChecked(semID, &SignalNMealCow, 1); //signal smaug that the meal is done only when all beasts are eaten //keep in this order to avoid deadlock semopChecked(semID, &WaitPMealSheep, 1); semopChecked(semID, &WaitPMealCow, 1); *numMealCow = *numMealCow + 1; if(*numMealSheep >= SHEEP_IN_MEAL && *numMealCow >= COW_IN_MEAL) { int i; for (i = 0; i < SHEEP_IN_MEAL ; i++) { semopChecked(semID, &WaitNMealSheep, 1); *numMealSheep = *numMealSheep - 1; } for (i = 0; i < COW_IN_MEAL ; i++) { semopChecked(semID, &WaitNMealCow, 1); *numMealCow = *numMealCow - 1; } semopChecked(semID, &SignalPMealCow, 1); semopChecked(semID, &SignalPMealSheep, 1); semopChecked(semID, &SignalSMealDone, 1); } else { semopChecked(semID, &SignalPMealCow, 1); semopChecked(semID, &SignalPMealSheep, 1); } }
void smaug(int smaugWinChance)// { int k; int localpid; double elapsedTime; /* local counters used only for smaug routine */ int numJewels = INITIAL_TREASURE_IN_HOARD; int cowsEatenTotal = 0; int sheepsEatenTotal = 0; int thiefsWonTotal = 0; int huntersWonTotal = 0; int sleepThisIteration = 1; /* Initialize random number generator*/ /* Random numbers are used to determine the time between successive beasts */ smaugProcessID = getpid(); printf("SMAUGSMAUGSMAUGSMAUGSMAU PID is %d \n", smaugProcessID ); localpid = smaugProcessID; while (*terminateFlagp==0) { // Smaug goes to sleep if nothing happens and sleepThisIteration is 1 if(sleepThisIteration == 1) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has gone to sleep\n" ); // We must reset the semaphore to prevent smaug waking up when there's no need to seminfo.val = 0; semctlChecked(semID, SEM_DRAGONSLEEPING, SETVAL, seminfo); semopChecked(semID, &WaitDragonSleeping, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug sniffs his valey\n" ); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has woken up \n" ); } else { sleepThisIteration = 1; } semopChecked(semID, &WaitProtectMealWaitingFlag, 1); semopChecked(semID, &WaitProtectSheepMealWaitingFlag, 1); if(*mealWaitingFlagp >= 1 && *mealSheepWaitingFlagp >= 1) { while( *mealWaitingFlagp >= 1 && *mealSheepWaitingFlagp >= 1 ) { *mealWaitingFlagp = *mealWaitingFlagp - 1; *mealSheepWaitingFlagp = *mealSheepWaitingFlagp - 1; printf("SMAUGSMAUGSMAUGSMAUGSMAU signal cow meal flag %d\n", *mealWaitingFlagp); semopChecked(semID, &SignalProtectMealWaitingFlag, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug is eating a meal\n"); for( k = 0; k < COWS_IN_GROUP; k++ ) { semopChecked(semID, &SignalCowsWaiting, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU A cow is ready to eat\n"); } printf("SMAUGSMAUGSMAUGSMAUGSMAU signal sheep meal flag %d\n", *mealSheepWaitingFlagp); semopChecked(semID, &SignalProtectSheepMealWaitingFlag, 1); for( k = 0; k < SHEEPS_IN_GROUP; k++ ) { semopChecked(semID, &SignalSheepsWaiting, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU A sheep is ready to eat\n"); } /*Smaug waits to eat*/ semopChecked(semID, &WaitDragonEating, 1); for( k = 0; k < COWS_IN_GROUP; k++ ) { semopChecked(semID, &SignalCowsDead, 1); cowsEatenTotal++; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug finished eating a cow\n"); } for( k = 0; k < SHEEPS_IN_GROUP; k++ ) { semopChecked(semID, &SignalSheepsDead, 1); sheepsEatenTotal++; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug finished eating a sheep\n"); } printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has finished a meal\n"); if( (cowsEatenTotal >= MAX_COWS_EATEN) && (sheepsEatenTotal >= MAX_SHEEPS_EATEN) ) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has eaten the allowed number of cows and sheeps\n"); *terminateFlagp= 1; break; } printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug takes a nap for %d us\n", SMAUG_NAP_LENGTH); usleep(SMAUG_NAP_LENGTH); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug takes a deep breath\n"); /* Smaug check to see if another snack is waiting */ semopChecked(semID, &WaitProtectMealWaitingFlag, 1); semopChecked(semID, &WaitProtectSheepMealWaitingFlag, 1); if( *mealWaitingFlagp > 0 && *mealSheepWaitingFlagp > 0 ) { printf("SMAUGSMAUGSMAUGSMAUGSMAU %d Smaug eats again\n", localpid); continue; } } } semopChecked(semID, &SignalProtectMealWaitingFlag, 1); // release the protection semopChecked(semID, &SignalProtectSheepMealWaitingFlag, 1); // release the protection semopChecked(semID, &WaitProtectThiefCount, 1); if(*thiefCounterp > 0) { *thiefCounterp = *thiefCounterp - 1; semopChecked(semID, &SignalProtectThiefCount, 1); // Wake thief from wander state for interaction semopChecked(semID, &SignalThievesWaiting, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug is playing with a thief\n"); if( rand() % 100 <= smaugWinChance ) { numJewels += 20; thiefsWonTotal++; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has defeated a thief\n"); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has gained a treasure (%d jewels). He now has %d jewels.\n", 20, numJewels); } else { numJewels -= 8; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has been defeated by a thief\n"); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has lost a treasure (%d jewels). He now has %d jewels.\n", 8, numJewels); } printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has finished a game (1 thief process has been terminated)\n"); if(thiefsWonTotal >= MAX_THEIFES_WON ) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has won the allowed number of thieves\n"); *terminateFlagp= 1; break; } if( numJewels >= MAX_TREASURE_IN_HOARD) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has %d, so he is richer then everyone so he leaves.\n", numJewels); *terminateFlagp= 1; break; } if( numJewels <= MIN_TREASURE_IN_HOARD) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has %d, so is very poor and decide to leave\n", numJewels); *terminateFlagp= 1; break; } semopChecked(semID, &SignalThiefFinish, 1); } semopChecked(semID, &SignalProtectThiefCount, 1); //release semopChecked(semID, &WaitProtectHunterCount, 1); if(*hunterCounterp > 0) { // check thift *hunterCounterp = *hunterCounterp - 1; semopChecked(semID, &SignalProtectHunterCount, 1); semopChecked(semID, &SignalHuntersWaiting, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug is fighting with a hunter\n"); if( rand() % 100 <= smaugWinChance ) { huntersWonTotal++; numJewels += 5; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has defeated a hunter\n"); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has gained a treasure (%d jewels). He now has %d jewels.\n", 10, numJewels); } else { numJewels -= 10; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has been defeated by a hunter\n"); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has lost a treasure (%d jewels). He now has %d jewels.\n", 5, numJewels); } printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has finished a fight(1 hunter process has been terminated)\n"); if(huntersWonTotal >= MAX_HUNTERS_WON ) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has won the allowed number of hunters\n"); *terminateFlagp= 1; break; } if( numJewels >= MAX_TREASURE_IN_HOARD) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has %d, so he is richer then everyone so he leaves.\n", numJewels); *terminateFlagp= 1; break; } if( numJewels <= MIN_TREASURE_IN_HOARD) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has %d, so is very poor and decide to leave\n", numJewels); *terminateFlagp= 1; break; } semopChecked(semID, &SignalHunterFinish, 1); } semopChecked(semID, &SignalProtectHunterCount, 1); //release printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug sleeps again\n"); semopChecked(semID, &WaitDragonSleeping, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug is awake again\n"); sleepThisIteration = 0; } }
void smaug() { int newWakeup = 1; pid_t localid = getpid(); smaugID = localid; printf("DRAGONDRAGONDRAGON Smaug[%d] is born\n", smaugID); setpgid(smaugID, dragonGID); printf("DRAGONDRAGONDRAGON Smaug[%d] goes to sleep\n", smaugID); semopChecked(semID, &WaitPDragonJewel, 1); *numDragonJewel = INIT_JEWEL; semopChecked(semID, &SignalPDragonJewel, 1); semopChecked(semID, &WaitSDragonWakeUp, 1); newWakeup = 1; int time = 0; while (1) { //sleeping/waking loop printf("DRAGONDRAGONDRAGON Smaug[%d] wakes up\n", smaugID); while (1) { //swimming loop // the meal, thief, hunter process in this loop int onceMeal = 0; int onceThief = 0; int onceHunter = 0; //looking for meals semopChecked(semID, &WaitPNumMeal, 1); while(*numMeal > 0 && onceMeal < MEAL_ONCE) { if(newWakeup==1) newWakeup = 0; else semopChecked(semID, &WaitSDragonWakeUp, 1); printf("DRAGONDRAGONDRAGON Smaug[%d] finds the %d-th snack\n", smaugID, onceMeal+1); eat(); onceMeal ++; } semopChecked(semID, &SignalPNumMeal, 1); //check meal this round if(onceMeal > 0) { //if the swim(); continue; } else printf("DRAGONDRAGONDRAGON Smaug[%d] finds no snacks, it looks for thieves\n", smaugID); //looking for thieves semopChecked(semID, &WaitPThiefPath, 1); while(*numThiefPath > 0 && onceThief < THIEF_ONCE) { if(newWakeup==1) newWakeup = 0; else semopChecked(semID, &WaitSDragonWakeUp, 1); printf("DRAGONDRAGONDRAGON Smaug[%d] finds the %d-th thief\n", smaugID, onceThief+1); play(); onceThief ++; } semopChecked(semID, &SignalPThiefPath, 1); //check thief this round if(onceThief > 0) { printf("DRAGONDRAGONDRAGON Smaug[%d] is a happy dragon now, it curls up its jewels and goes to sleep\n", smaugID); break; } else printf("DRAGONDRAGONDRAGON Smaug[%d] finds no thieves, it looks for hunters\n", smaugID); //looking for hunters semopChecked(semID, &WaitPHunterPath, 1); while(*numHunterPath > 0 && onceHunter < HUNTER_ONCE) { if(newWakeup==1) newWakeup = 0; else semopChecked(semID, &WaitSDragonWakeUp, 1); printf("DRAGONDRAGONDRAGON Smaug[%d] finds the %d-th hunter\n", smaugID, onceHunter + 1); fight(); onceHunter++; } semopChecked(semID, &SignalPHunterPath, 1); //check hunter if(onceHunter > 0) { printf("DRAGONDRAGONDRAGON Smaug[%d] fought with the treasure hunter and goes to swim\n", smaugID); swim(); continue; } else { printf("DRAGONDRAGONDRAGON Smaug[%d] finds no hunters, there is nothing left, smaug goes to sleep\n", smaugID); break; } } printf("DRAGONDRAGONDRAGON Smaug[%d] goes to sleep\n", smaugID); semopChecked(semID, &WaitSDragonWakeUp, 1); newWakeup = 1; } }
void cow(int startTimeN) { int localpid; // int retval; int k; localpid = getpid(); /* graze */ printf("CCCCCCC %8d CCCCCCC A cow is born\n", localpid); if (startTimeN > 0) { if (usleep(startTimeN) == -1) { /* exit when usleep interrupted by kill signal */ if (errno == EINTR) exit(4); } } printf("CCCCCCC %8d CCCCCCC cow grazes for %f ms\n", localpid, startTimeN / 1000.0); /* does this beast complete a group of BEASTS_IN_GROUP ? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectCowsInGroup, 1); semopChecked(semID, &SignalCowsInGroup, 1); *cowCounterp = *cowCounterp + 1; printf("CCCCCCC %8d CCCCCCC %d cows have been enchanted \n", localpid, *cowCounterp); if ((*cowCounterp >= COWS_IN_GROUP) && (*sheepCounterp >= SHEEP_IN_GROUP)) { *cowCounterp = *cowCounterp - COWS_IN_GROUP; for (k = 0; k < COWS_IN_GROUP; k++) { semopChecked(semID, &WaitCowsInGroup, 1); } printf("CCCCCCC %8d CCCCCCC The last cow is waiting\n", localpid); semopChecked(semID, &SignalProtectCowsInGroup, 1); semopChecked(semID, &WaitProtectMealWaitingFlag, 1); *mealWaitingFlagp = *mealWaitingFlagp + 1; printf("CCCCCCC %8d CCCCCCC signal meal flag %d\n", localpid, *mealWaitingFlagp); semopChecked(semID, &SignalProtectMealWaitingFlag, 1); semopChecked(semID, &SignalDragonSleeping, 1); printf("CCCCCCC %8d CCCCCCC last cow wakes the dragon \n", localpid); } else { semopChecked(semID, &SignalProtectCowsInGroup, 1); } semopChecked(semID, &WaitCowsWaiting, 1); /* have all the beasts in group been eaten? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectCowsEaten, 1); semopChecked(semID, &SignalCowsEaten, 1); *cowsEatenCounterp = *cowsEatenCounterp + 1; if ((*cowsEatenCounterp >= COWS_IN_GROUP)) { *cowsEatenCounterp = *cowsEatenCounterp - COWS_IN_GROUP; for (k = 0; k < COWS_IN_GROUP; k++) { semopChecked(semID, &WaitCowsEaten, 1); } printf("CCCCCCC %8d CCCCCCC The last cow has been eaten\n", localpid); semopChecked(semID, &SignalProtectCowsEaten, 1); semopChecked(semID, &SignalDragonEating, 1); } else { semopChecked(semID, &SignalProtectCowsEaten, 1); printf("CCCCCCC %8d CCCCCCC A cow is waiting to be eaten\n", localpid); } semopChecked(semID, &WaitCowsDead, 1); printf("CCCCCCC %8d CCCCCCC cow dies\n", localpid); }
void sheep(int startTimeN) { int localpid; int k; localpid = getpid(); /* graze */ printf("SSSSSSS %8d SSSSSSS A sheep is born\n", localpid); if (startTimeN > 0) { if (usleep(startTimeN) == -1) { /* exit when usleep interrupted by kill signal */ if (errno == EINTR) exit(4); } } printf("SSSSSSS %8d SSSSSSS sheep grazes for %f ms\n", localpid, startTimeN / 1000.0); /* does this beast complete a group of BEASTS_IN_GROUP ? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectSheepInGroup, 1); semopChecked(semID, &SignalSheepInGroup, 1); *sheepCounterp = *sheepCounterp + 1; printf("SSSSSSS %8d SSSSSSS %d sheep have been enchanted \n", localpid, *sheepCounterp); if ((*cowCounterp >= COWS_IN_GROUP) && (*sheepCounterp >= SHEEP_IN_GROUP)) { *sheepCounterp = *sheepCounterp - SHEEP_IN_GROUP; for (k = 0; k < SHEEP_IN_GROUP; k++) { semopChecked(semID, &WaitSheepInGroup, 1); } printf("SSSSSSS %8d SSSSSSS The last sheep is waiting\n", localpid); semopChecked(semID, &SignalProtectSheepInGroup, 1); semopChecked(semID, &WaitProtectMealWaitingFlag, 1); *mealWaitingFlagp = *mealWaitingFlagp + 1; printf("SSSSSSS %8d SSSSSSS signal meal flag %d\n", localpid, *mealWaitingFlagp); semopChecked(semID, &SignalProtectMealWaitingFlag, 1); semopChecked(semID, &SignalDragonSleeping, 1); printf("SSSSSSS %8d SSSSSSS last sheep wakes the dragon \n", localpid); } else { semopChecked(semID, &SignalProtectSheepInGroup, 1); } semopChecked(semID, &WaitSheepWaiting, 1); /* have all the beasts in group been eaten? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectSheepEaten, 1); semopChecked(semID, &SignalSheepEaten, 1); *sheepEatenCounterp = *sheepEatenCounterp + 1; if ((*sheepEatenCounterp >= SHEEP_IN_GROUP)) { *sheepEatenCounterp = *sheepEatenCounterp - SHEEP_IN_GROUP; for (k = 0; k < SHEEP_IN_GROUP; k++) { semopChecked(semID, &WaitSheepEaten, 1); } printf("SSSSSSS %8d SSSSSSS The last sheep has been eaten\n", localpid); semopChecked(semID, &SignalProtectSheepEaten, 1); semopChecked(semID, &SignalDragonEating, 1); } else { semopChecked(semID, &SignalProtectSheepEaten, 1); printf("SSSSSSS %8d SSSSSSS A sheep is waiting to be eaten\n", localpid); } semopChecked(semID, &WaitSheepDead, 1); printf("SSSSSSS %8d SSSSSSS sheep dies\n", localpid); }
//the function which sets the termination void setTerminate() { semopChecked(semID, &WaitPTermination, 1); *flagTermination = 1; semopChecked(semID, &SignalPTermination, 1); }
//=================================================================== // SHEEP //=================================================================== void sheep(int time) { pid_t localpid = getpid(); setpgid(localpid, beastGID); printf("SHEEPSHEEPSHEEPSHEEP Sheep[%d] is grazing for %d usec\n", localpid, time); //grazing if (usleep(time) == -1) { /* exit when usleep interrupted by kill signal */ if (errno == EINTR)exit(4); } //the sheep is enchanted //keep in this order to get use semopChecked(semID, &WaitPSheepInValley, 1); semopChecked(semID, &WaitPCowInValley, 1); *SheepInValley = *SheepInValley + 1; semopChecked(semID, &SignalNSheepInValley, 1); printf("SHEEPSHEEPSHEEPSHEEP Sheep[%d] is enchanted, now %d cows and %d sheep in Valley\n", localpid, *CowInValley, *SheepInValley); // There is a meal in the Valley if (*SheepInValley >= SHEEP_IN_MEAL && *CowInValley >= COW_IN_MEAL) { int i; for (i = 0; i < SHEEP_IN_MEAL; i++) { semopChecked(semID, &WaitNSheepInValley, 1); *SheepInValley = *SheepInValley - 1; } for (i = 0; i < COW_IN_MEAL; i++) { semopChecked(semID, &WaitNCowInValley, 1); *CowInValley = *CowInValley - 1; } semopChecked(semID, &WaitPNumMeal, 1); *numMeal = *numMeal + 1; printf("SHEEPSHEEPSHEEPSHEEP Sheep[%d] results in a new meal, now number of meals:%d\n", localpid, *numMeal); semopChecked(semID, &SignalNMeal, 1); semopChecked(semID, &SignalPNumMeal, 1); semopChecked(semID, &SignalPCowInValley, 1); semopChecked(semID, &SignalPSheepInValley, 1); //send a wake up to the dragon semopChecked(semID, &SignalSDragonWakeUp, 1); } else { //the sheep in the Valley is not enough for a meal semopChecked(semID, &SignalPCowInValley, 1); semopChecked(semID, &SignalPSheepInValley, 1); } // sheep waiting semopChecked(semID, &WaitSSheepWaiting, 1); semopChecked(semID, &SignalNSheepToEat, 1); // smaug starts eating only when the the meal is ready semopChecked(semID, &WaitPSheepToEat, 1); semopChecked(semID, &WaitPCowToEat, 1); *numSheepToEat = *numSheepToEat + 1; if (*numSheepToEat >= SHEEP_IN_MEAL && *numCowToEat >= COW_IN_MEAL) { int i; for (i = 0; i < SHEEP_IN_MEAL; i++) { semopChecked(semID, &WaitNSheepToEat, 1); *numSheepToEat = *numSheepToEat - 1; } for (i = 0; i < COW_IN_MEAL; i++) { semopChecked(semID, &WaitNCowToEat, 1); *numCowToEat = *numCowToEat - 1; } semopChecked(semID, &SignalPCowToEat, 1); semopChecked(semID, &SignalPSheepToEat, 1); semopChecked(semID, &SignalSDragonEat, 1); } else { semopChecked(semID, &SignalPCowToEat, 1); semopChecked(semID, &SignalPSheepToEat, 1); } // printf("Sheep %d is about to be eaten\n", localpid); semopChecked(semID, &WaitSSheepEaten, 1); semopChecked(semID, &WaitPSheepEaten, 1); *numSheepEaten = *numSheepEaten + 1; semopChecked(semID, &SignalPSheepEaten, 1); printf("SHEEPSHEEPSHEEPSHEEP Sheep[%d] is eaten, now %d sheep eaten\n", localpid, *numSheepEaten); if(checkSheep()) { terminateSimulation(); exit(0); } semopChecked(semID, &SignalNMealSheep, 1); //keep in this order to avoid deadlock semopChecked(semID, &WaitPMealSheep, 1); semopChecked(semID, &WaitPMealCow, 1); *numMealSheep = *numMealSheep + 1; if(*numMealSheep >= SHEEP_IN_MEAL && *numMealCow >= COW_IN_MEAL) { int i; for (i = 0; i < SHEEP_IN_MEAL ; i++) { semopChecked(semID, &WaitNMealSheep, 1); *numMealSheep = *numMealSheep - 1; } for (i = 0; i < COW_IN_MEAL ; i++) { semopChecked(semID, &WaitNMealCow, 1); *numMealCow = *numMealCow - 1; } semopChecked(semID, &SignalPMealCow, 1); semopChecked(semID, &SignalPMealSheep, 1); semopChecked(semID, &SignalSMealDone, 1); } else { semopChecked(semID, &SignalPMealCow, 1); semopChecked(semID, &SignalPMealSheep, 1); } }
void smaug() { int k; // int temp; int localpid; // double elapsedTime; /* local counters used only for smaug routine */ int cowsEatenTotal = 0; int sheepEatenTotal = 0; /* Initialize random number generator*/ /* Random numbers are used to determine the time between successive beasts */ smaugProcessID = getpid(); printf("SMAUGSMAUGSMAUGSMAUGSMAU PID is %d \n", smaugProcessID); localpid = smaugProcessID; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has gone to sleep\n"); semopChecked(semID, &WaitDragonSleeping, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has woken up \n"); while (TRUE) { semopChecked(semID, &WaitProtectMealWaitingFlag, 1); while (*mealWaitingFlagp >= 1) { *mealWaitingFlagp = *mealWaitingFlagp - 1; printf("SMAUGSMAUGSMAUGSMAUGSMAU signal meal flag %d\n", *mealWaitingFlagp); semopChecked(semID, &SignalProtectMealWaitingFlag, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug is eating a meal\n"); for (k = 0; k < COWS_IN_GROUP; k++) { semopChecked(semID, &SignalCowsWaiting, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU A cow is ready to eat\n"); } for (k = 0; k < SHEEP_IN_GROUP; k++) { semopChecked(semID, &SignalSheepWaiting, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU A sheep is ready to eat\n"); } /*Smaug waits to eat*/ semopChecked(semID, &WaitDragonEating, 1); for (k = 0; k < COWS_IN_GROUP; k++) { semopChecked(semID, &SignalCowsDead, 1); cowsEatenTotal++; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug finished eating a cow\n"); } for (k = 0; k < SHEEP_IN_GROUP; k++) { semopChecked(semID, &SignalSheepDead, 1); sheepEatenTotal++; printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug finished eating a sheep\n"); } printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has finished a meal\n"); if (cowsEatenTotal >= MAX_COWS_EATEN && sheepEatenTotal >= MAX_SHEEP_EATEN) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug has eaten the allowed number of sheep and cows\n"); *terminateFlagp = 1; break; } /* Smaug check to see if another snack is waiting */ semopChecked(semID, &WaitProtectMealWaitingFlag, 1); if (*mealWaitingFlagp > 0) { printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug eats again\n"); //, localpid); continue; } else { semopChecked(semID, &SignalProtectMealWaitingFlag, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug sleeps again\n"); //, localpid); semopChecked(semID, &WaitDragonSleeping, 1); printf("SMAUGSMAUGSMAUGSMAUGSMAU Smaug is awake again\n"); //, localpid); break; } } } }
void cow(int startTimeN) { int localpid; int k; localpid = getpid(); /* graze */ printf("CCCCCCC %8d CCCCCCC A cow is born\n", localpid); if( startTimeN > 0) { if( usleep( startTimeN) == -1){ /* exit when usleep interrupted by kill signal */ if(errno==EINTR)exit(4); } } printf("CCCCCCC %8d CCCCCCC cow grazes for %f ms\n", localpid, startTimeN/1000.0); semopChecked(semID, &WaitProtectTerminate, 1); if( *terminateFlagp == 1 ) { printf("CCCCCCC %8d CCCCCCC cow has found the magical path after we've been told to terminate\n", localpid); semopChecked(semID, &SignalProtectTerminate, 1); kill(localpid, SIGKILL); return; } else { printf("CCCCCCC %d CCCCCCC cow has found the magical path in %d ms\n", localpid, startTimeN); semopChecked(semID, &SignalProtectTerminate, 1); } /* does this beast complete a group of BEASTS_IN_GROUP ? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectCowsInGroup, 1); semopChecked(semID, &SignalCowsInGroup, 1); *cowCounterp = *cowCounterp + 1; printf("CCCCCCC %8d CCCCCCC %d cows have been enchanted \n", localpid, *cowCounterp ); if( ( *cowCounterp >= COWS_IN_GROUP )) { *cowCounterp = *cowCounterp - COWS_IN_GROUP; for (k=0; k<COWS_IN_GROUP; k++) { semopChecked(semID, &WaitCowsInGroup, 1); } printf("CCCCCCC %8d CCCCCCC The last cow is waiting\n", localpid); semopChecked(semID, &SignalProtectCowsInGroup, 1); semopChecked(semID, &WaitProtectMealWaitingFlag, 1); *mealWaitingFlagp = *mealWaitingFlagp + 1; printf("CCCCCCC %8d CCCCCCC signal meal flag %d\n", localpid, *mealWaitingFlagp); semopChecked(semID, &SignalProtectMealWaitingFlag, 1); semopChecked(semID, &WaitProtectSheepMealWaitingFlag, 1); if(*mealSheepWaitingFlagp >= 1 ) { semopChecked(semID, &SignalDragonSleeping, 1); } semopChecked(semID, &SignalProtectSheepMealWaitingFlag, 1); printf("CCCCCCC %8d CCCCCCC last cow wakes the dragon \n", localpid); } else { semopChecked(semID, &SignalProtectCowsInGroup, 1); } semopChecked(semID, &WaitCowsWaiting, 1); /* have all the beasts in group been eaten? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectCowsEaten, 1); semopChecked(semID, &SignalCowsEaten, 1); *cowsEatenCounterp = *cowsEatenCounterp + 1; if( ( *cowsEatenCounterp >= COWS_IN_GROUP )) { *cowsEatenCounterp = *cowsEatenCounterp - COWS_IN_GROUP; for (k=0; k<COWS_IN_GROUP; k++){ semopChecked(semID, &WaitCowsEaten, 1); } printf("CCCCCCC %8d CCCCCCC The last cow has been eaten\n", localpid); semopChecked(semID, &SignalProtectCowsEaten, 1); semopChecked(semID, &SignalDragonEating, 1); } else { semopChecked(semID, &SignalProtectCowsEaten, 1); printf("CCCCCCC %8d CCCCCCC A cow is waiting to be eaten\n", localpid); } semopChecked(semID, &WaitCowsDead, 1); printf("CCCCCCC %8d CCCCCCC cow dies\n", localpid); kill(localpid, SIGKILL); // KILL AT THE END }
void sheep(int startTimeN) { int localpid; int k; localpid = getpid(); /* graze */ printf("SHEEP %8d SHEEP A Sheep is born\n", localpid); if( startTimeN > 0) { if( usleep( startTimeN) == -1){ /* exit when usleep interrupted by kill signal */ if(errno==EINTR)exit(4); } } printf("SHEEP %8d SHEEP Sheep grazes for %f ms\n", localpid, startTimeN/1000.0); semopChecked(semID, &WaitProtectTerminate, 1); if( *terminateFlagp == 1 ) { printf("SHEEP %8d SHEEP Sheep has found the magical path after we've been told to terminate\n", localpid); semopChecked(semID, &SignalProtectTerminate, 1); kill(localpid, SIGKILL); return; } else { printf("SHEEP %d SHEEP Sheep has found the magical path in %d ms\n", localpid, startTimeN); semopChecked(semID, &SignalProtectTerminate, 1); } /* does this beast complete a group of BEASTS_IN_GROUP ? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectSheepsInGroup, 1); semopChecked(semID, &SignalSheepsInGroup, 1); *sheepCounterp = *sheepCounterp + 1; printf("SHEEP %8d SHEEP %d Sheep have been enchanted \n", localpid, *sheepCounterp ); if( ( *sheepCounterp >= SHEEPS_IN_GROUP )) { *sheepCounterp = *sheepCounterp - SHEEPS_IN_GROUP; for (k=0; k<SHEEPS_IN_GROUP; k++) { semopChecked(semID, &WaitSheepsInGroup, 1); } printf("SHEEP %8d SHEEP The last sheep is waiting\n", localpid); semopChecked(semID, &SignalProtectSheepsInGroup, 1); semopChecked(semID, &WaitProtectSheepMealWaitingFlag, 1); // *mealSheepWaitingFlagp = *mealSheepWaitingFlagp + 1; printf("SHEEP %8d SHEEP signal meal flag %d\n", localpid, *mealSheepWaitingFlagp); semopChecked(semID, &SignalProtectSheepMealWaitingFlag, 1); // semopChecked(semID, &WaitProtectMealWaitingFlag, 1); if(*mealSheepWaitingFlagp >= 1 && *mealWaitingFlagp >= 1 ) { printf("!!!!!!!!!!!! signal cow meal flag %d \n", *mealWaitingFlagp); printf("!!!!!!!!!!!! signal sheep meal flag %d \n", *mealSheepWaitingFlagp); semopChecked(semID, &SignalDragonSleeping, 1); } semopChecked(semID, &SignalProtectMealWaitingFlag, 1); printf("SHEEP %8d SHEEP last sheep wakes the dragon \n", localpid); } else { semopChecked(semID, &SignalProtectSheepsInGroup, 1); } semopChecked(semID, &WaitSheepsWaiting, 1); /* have all the beasts in group been eaten? */ /* if so wake up the dragon */ semopChecked(semID, &WaitProtectSheepsEaten, 1); semopChecked(semID, &SignalSheepsEaten, 1); *sheepsEatenCounterp = *sheepsEatenCounterp + 1; if( ( *sheepsEatenCounterp >= SHEEPS_IN_GROUP )) { *sheepsEatenCounterp = *sheepsEatenCounterp - SHEEPS_IN_GROUP; for (k=0; k<SHEEPS_IN_GROUP; k++){ semopChecked(semID, &WaitSheepsEaten, 1); } printf("SHEEP %8d SHEEP The last cow has been eaten\n", localpid); semopChecked(semID, &SignalProtectSheepsEaten, 1); semopChecked(semID, &SignalDragonEating, 1); } else { semopChecked(semID, &SignalProtectSheepsEaten, 1); printf("SHEEP %8d SHEEP A sheep is waiting to be eaten\n", localpid); } semopChecked(semID, &WaitSheepsDead, 1); printf("SHEEP %8d SHEEP sheep dies\n", localpid); kill(localpid, SIGKILL); // KILL AT THE END }
int main() { // int k; // int temp; /* variables to hold process ID numbers */ int parentPID = 0; int cowPID = 0; int sheepPID = 0; int smaugPID = 0; /* local counters, keep track of total number */ /* of processes of each type created */ int cowsCreated = 0; int sheepCreated = 0; /* Variables to control the time between the arrivals */ /* of successive beasts*/ // double minwait = 0; int newSeed = 0; int sleepingTime = 0; int maxCowIntervalUsec = 0; int maxSheepIntervalUsec = 0; int nextInterval = 0.0; int status; int w = 0; double maxCowInterval = 0.0; double totalCowInterval = 0.0; double maxSheepInterval = 0.0; double totalSheepInterval = 0.0; double elapsedTime; // double hold; parentPID = getpid(); setpgid(parentPID, parentPID); parentProcessGID = getpgid(0); printf("CRCRCRCRCRCRCRCRCRCRCRCR main process group %d %d\n", parentPID, parentProcessGID); /* initialize semaphores and allocate shared memory */ initialize(); /* inialize each variable in shared memory */ *cowCounterp = 0; *cowsEatenCounterp = 0; *sheepCounterp = 0; *sheepEatenCounterp = 0; *mealWaitingFlagp = 0; printf("Please enter a random seed to start the simulation\n"); scanf("%d", &newSeed); srand(newSeed); printf("Please enter the maximum interval length for cow (ms)\n"); scanf("%lf", &maxCowInterval); printf("max Cow interval time %f \n", maxCowInterval); maxCowIntervalUsec = (int)maxCowInterval * 1000; printf("Please enter the maximum interval length for sheep (ms)\n"); scanf("%lf", &maxSheepInterval); printf("max Sheep interval time %f \n", maxSheepInterval); maxSheepIntervalUsec = (int)maxSheepInterval * 1000; gettimeofday(&startTime, NULL); if ((smaugPID = fork()) == 0) { printf("CRCRCRCRCRCRCRCRCRCRCRCR Smaug is born\n"); smaug(); printf("CRCRCRCRCRCRCRCRCRCRCRCR Smaug dies\n"); exit(0); } else { if (smaugProcessID == -1) { smaugProcessID = smaugPID; } setpgid(smaugPID, smaugProcessID); printf("CRCRCRCRCRCRCRCRCRCRCRCR Smaug PID %8d PGID %8d\n", smaugPID, smaugProcessID); } printf("CRCRCRCRCRCRCRCRCRCRCRCR Smaug PID create cow %8d \n", smaugPID); usleep(10); while (TRUE) { semopChecked(semID, &WaitProtectTerminate, 1); if (*terminateFlagp != 0) { semopChecked(semID, &SignalProtectTerminate, 1); break; } semopChecked(semID, &SignalProtectTerminate, 1); /* Create a cow process if needed */ /* The condition used to determine if a process is needed is */ /* if the last cow created will be enchanted */ elapsedTime = timeChange(startTime); if (totalCowInterval - elapsedTime < totalCowInterval) { nextInterval = (int)((double)rand() / RAND_MAX * maxCowIntervalUsec); totalCowInterval += nextInterval / 1000.0; sleepingTime = (int)((double)rand() / RAND_MAX * maxCowIntervalUsec); if ((cowPID = fork()) == 0) { /* Child becomes a beast */ elapsedTime = timeChange(startTime); cow(sleepingTime); /* Child (beast) quits after being consumed */ exit(0); } else if (cowPID > 0) { cowsCreated++; if (cowProcessGID == -1) { cowProcessGID = cowPID; printf("CRCRCRCRCR %8d CRCRCRCRCR cow PGID %8d \n", cowPID, cowProcessGID); } setpgid(cowPID, cowProcessGID); printf("CRCRCRCRCRCRCRCRCRCRCRCR NEW COW CREATED %8d \n", cowsCreated); } else { printf("CRCRCRCRCRCRCRCRCRCRCRCR cow process not created \n"); continue; } } /* Create a sheep process if needed */ /* The condition used to determine if a process is needed is */ /* if the last sheep created will be enchanted */ elapsedTime = timeChange(startTime); if (totalSheepInterval - elapsedTime < totalSheepInterval) { nextInterval = (int)((double)rand() / RAND_MAX * maxSheepIntervalUsec); totalCowInterval += nextInterval / 1000.0; sleepingTime = (int)((double)rand() / RAND_MAX * maxSheepIntervalUsec); if ((sheepPID = fork()) == 0) { /* Child becomes a beast */ elapsedTime = timeChange(startTime); sheep(sleepingTime); /* Child (beast) quits after being consumed */ exit(0); } else if (sheepPID > 0) { sheepCreated++; if (sheepProcessGID == -1) { sheepProcessGID = sheepPID; printf("CRCRCRCRCR %8d CRCRCRCRCR sheep PGID %8d \n", sheepPID, sheepProcessGID); } setpgid(sheepPID, sheepProcessGID); printf("CRCRCRCRCRCRCRCRCRCRCRCR NEW SHEEP CREATED %8d \n", sheepCreated); } else { printf("CRCRCRCRCRCRCRCRCRCRCRCR sheep process not created \n"); continue; } } /* wait for processes that have exited so we do not accumulate zombie or cows */ while ((w = waitpid(-1, &status, WNOHANG)) > 1) { if (WIFEXITED(status)) { if (WEXITSTATUS(status) > 0) { printf("exited, status=%8d\n", WEXITSTATUS(status)); terminateSimulation(); printf("GOODBYE from main process %8d\n", getpid()); exit(0); } else { printf(" REAPED process %8d\n", w); } } } /* terminateFlagp is set to 1 (from initial value of 0) when any */ /* termination condition is satisfied */ if (*terminateFlagp == 1) break; /* sleep for 80% of the cow interval */ /* then try making more processes */ usleep((totalCowInterval * 800)); } if (*terminateFlagp == 1) { terminateSimulation(); } printf("GOODBYE from main process %8d\n", getpid()); exit(0); }