Esempio n. 1
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);
}
Esempio n. 2
0
//===================================================================
// 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);
}
Esempio n. 3
0
//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);
}
Esempio n. 4
0
//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);
}
Esempio n. 5
0
int main()
{
 initialize();
 
 printf("Welcome to Smaug World Simulator\n");

 const int seed = 1; //askUserValue("Enter the random value seed");
 const long int maximumCowInterval = 10000000;//askUserValue("Enter maximumCowInterval in (us)");
 const long int maximumThiefInterval = 10000000;//askUserValue("Enter maximumThiefInterval in (us)");
 const long int maximumHunterInterval = 10000000;//askUserValue("Enter maximumHunterInterval in (us)");
 const long int maximumSheepInterval = 10000000;//askUserValue("Enter maximumSheepInterval in (us)");
 const int smaugWinChance = 50; //askUserValue("smaugWinProb (0 to 100)");
 srand(seed);

 
 double cowTimer = 0;
 double sheepTimer = 0;
 double thiefTimer = 0;
 double hunterTimer = 0;
 
 
 parentProcessID = getpid();

 smaugProcessID = -1;
 cowProcessGID = parentProcessID - 1;
 thiefProcessGID = parentProcessID - 2;
 hunterProcessGID = parentProcessID - 3;
 sheepProcessGID = parentProcessID - 4;
  
  pid_t childPID = fork();
  
 if(childPID < 0) {
 	 printf("FORK FAILED\n");
 	 return 1;
 } else if(childPID == 0) {
 	 smaug(smaugWinChance); // run the smaug
 	 return 0;
 }
 
 smaugProcessID = childPID;
 gettimeofday(&startTime, NULL);
 int zombieRemoveCounter = 0; // Variable to kill zombie
 while(*terminateFlagp == 0) 
 {
 	 zombieRemoveCounter++;
 	 double simDuration = timeChange(startTime);
 
 
 	 if(cowTimer - simDuration <= 0) {
 	 	 cowTimer = simDuration + (rand() % maximumCowInterval) / 1000.0;
 	 	 printf("COW CREATED! next cow at: %f\n", cowTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) {
 	 	 	 cow((rand() % maximumCowInterval) / 1000.0);
 	 	 	 return 0;
 	 	 }
 	 }
 	 if(thiefTimer - simDuration <= 0) 
 	 {
 	 	 thiefTimer = simDuration + (rand() % maximumThiefInterval) / 1000.0;
 	 	 printf("THIEF CREATED! next thief at: %f\n", thiefTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) 
 	 	 {
 	 	 	thief((rand() % maximumThiefInterval) / 1000.0);
 	 	 	return 0;
 	 	 }
 	 }
 	 
 	if(hunterTimer - simDuration <= 0) 
 	 {
 	 	 hunterTimer = simDuration + (rand() % maximumHunterInterval) / 1000.0;
 	 	 printf("HUNTER CREATED! next hunter at: %f\n", hunterTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) 
 	 	 {
 	 	 	hunter((rand() % maximumHunterInterval) / 1000.0);
 	 	 	return 0;
 	 	 }
 	 }
 	if(sheepTimer - simDuration <= 0) 
 	 {
 	 	 sheepTimer = simDuration + (rand() % maximumSheepInterval) / 1000.0;
 	 	 printf("SHEEP CREATED! next sheep at: %f\n", sheepTimer);
 	 	 int childPID = fork();
 	 	 if(childPID == 0) 
 	 	 {
 	 	 	sheep((rand() % maximumSheepInterval) / 1000.0);
 	 	 	return 0;
 	 	 }
 	 }

 	 // remove zombie processes once in a 10 runs ~ at most 10 process
 	 if(zombieRemoveCounter % 10 == 0) 
 	 {
 	 	 zombieRemoveCounter -= 10;
 	 	 int w = 0; int status = 0;
 	 	 while( (w = waitpid( -1, &status, WNOHANG)) > 1)
 	 	 {
			printf("REAPED zombie process %d from main loop\n", w);
 	 	 }
 	 }
 }

 terminateSimulation();
 return 0;
}
Esempio n. 6
0
//===================================================================
// 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);
    }
}
Esempio n. 7
0
//========================================================
//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);
    }
}
Esempio n. 8
0
int main(void) {
    initialize();
    srand(time(NULL));

    int SHEEP_INTERVAL = 0;
    int COW_INTERVAL = 0;
    int THIEF_INTERVAL = 0;
    int HUNTER_INTERVAL = 0;

    long long sheep_time = 0;
    long long cow_time = 0;
    long long thief_time = 0;
    long long hunter_time = 0;

    int sheepSleepTime = 0;
    int cowSleepTime = 0;
    int thiefPathTime = 0;
    int hunterPathTime = 0;

    //========================================================
    printf("Input the Sheep Interval:");
    scanf("%d", &SHEEP_INTERVAL);
    printf("Input the maximum time that a sheep grazes:");
    scanf("%d", &sheepSleepTime);
    //=======================================================
    printf("Input the Cow Interval:");
    scanf("%d", &COW_INTERVAL);
    printf("Input the maximum time that a cow grazes:");
    scanf("%d", &cowSleepTime);
    //=======================================================
    printf("Input the Thief Interval:");
    scanf("%d", &THIEF_INTERVAL);
    printf("Input the maximum time that a thief looks for the path:");
    scanf("%d", &thiefPathTime);
    //========================================================
    printf("Input the Hunter Interval:");
    scanf("%d", &HUNTER_INTERVAL);
    printf("Input the maximum time that a hunter looks for the path:");
    scanf("%d", &hunterPathTime);
    //========================================================

    sheep_time += SHEEP_INTERVAL;
    cow_time += COW_INTERVAL;
    thief_time += THIEF_INTERVAL;
    hunter_time += HUNTER_INTERVAL;
    int genflag = 0; // the flag tells what to generate

    long long elapsetime = 0;
    long long lastelapsetime = 0;
    struct timeval lasttime;
    struct timeval curtime;
    gettimeofday(&lasttime, NULL);

    //produce smaug
    pid_t result = fork();
    if (result < 0) {
        printf("fork error\n");
        exit(1);
    }
    if (result == 0) {
        smaug();
    }
    else {
        pid_t r;
        while (1) {
            gettimeofday(&curtime, NULL);
            elapsetime += (curtime.tv_sec - lasttime.tv_sec) * 1000000 + (curtime.tv_usec - lasttime.tv_usec);
//	        pid_t localpid = getpid();
            //printf("In process: %d Elapsed time: %lld\n", localpid,elapsetime);
//            if(elapsetime - lastelapsetime >= 500000)
            lasttime = curtime;

            if (checkTermination()) {

                printf("****************************terminating in parent process**************************************************\n");

                terminateSimulation();
                int status;
                // block till all children exits
                waitpid(-1, &status, 0);
                printf("****************************In main: all children have exited\n");
                releaseResource();
                exit(0);
            }

            if (elapsetime > sheep_time) {
                genflag = 0;
                sheep_time += SHEEP_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > cow_time) {
                genflag = 1;
                cow_time += COW_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > thief_time) {
                genflag = 2;
                thief_time += THIEF_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > hunter_time) {
                genflag = 3;
                hunter_time += HUNTER_INTERVAL;
                r = fork();
                if (r == 0) break;
            }
        }
        //=============================================================
        if (genflag == 0) sheep(rand() % sheepSleepTime);
        else if (genflag == 1) cow(rand() % cowSleepTime);
        else if (genflag == 2) thief(rand() % thiefPathTime);
        else if (genflag == 3) hunter(rand() % hunterPathTime);
        exit(0);
    }
}
Esempio n. 9
0
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);
}