int main() { pthread_t pt; pthread_create(&pt, 0, owner, 0); thief(0); return 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; }
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); } }
void mobile_activity(void) { struct char_data *ch, *next_ch, *vict; struct obj_data *obj, *best_obj; int door, found, max; memory_rec *names; for (ch = character_list; ch; ch = next_ch) { next_ch = ch->next; if (!IS_MOB(ch)) continue; // Mobs that steal from players if (MOB_FLAGGED(ch, MOB_PLR_THIEF_COINS) || MOB_FLAGGED(ch, MOB_PLR_THIEF_ITEMS)) thief(ch, NULL, 0, NULL); /* Examine call for special procedure */ if (MOB_FLAGGED(ch, MOB_SPEC) && !no_specials) { if (mob_index[GET_MOB_RNUM(ch)].func == NULL) { log("SYSERR: %s (#%d): Attempting to call non-existing mob function.", GET_NAME(ch), GET_MOB_VNUM(ch)); REMOVE_BIT(MOB_FLAGS(ch), MOB_SPEC); } else { char actbuf[MAX_INPUT_LENGTH] = ""; if ((mob_index[GET_MOB_RNUM(ch)].func) (ch, ch, 0, actbuf)) continue; /* go to next char */ } } /* check for mob class or race options here */ mob_magic_user(ch); /* If the mob has no specproc, do the default actions */ if (FIGHTING(ch) || !AWAKE(ch)) continue; /* Scavenger (picking up objects) */ if (MOB_FLAGGED(ch, MOB_SCAVENGER)) if (world[IN_ROOM(ch)].contents && !rand_number(0, 10)) { max = 1; best_obj = NULL; for (obj = world[IN_ROOM(ch)].contents; obj; obj = obj->next_content) if (CAN_GET_OBJ(ch, obj) && GET_OBJ_COST(obj) > max) { best_obj = obj; max = GET_OBJ_COST(obj); } if (best_obj != NULL) { obj_from_room(best_obj); obj_to_char(best_obj, ch); act("$n gets $p.", FALSE, ch, best_obj, 0, TO_ROOM); } } /* Mob Movement */ if (!MOB_FLAGGED(ch, MOB_SENTINEL) && (GET_POS(ch) == POS_STANDING) && ((door = rand_number(0, 18)) < NUM_OF_DIRS) && CAN_GO(ch, door) && !ROOM_FLAGGED(EXIT(ch, door)->to_room, ROOM_NOMOB | ROOM_DEATH) && (!MOB_FLAGGED(ch, MOB_STAY_ZONE) || (world[EXIT(ch, door)->to_room].zone == world[IN_ROOM(ch)].zone))) { perform_move(ch, door, 1); } /* Aggressive Mobs */ if (AFF_FLAGGED(ch, AFF_BENEFICENCE) && (rand_number(0, 100) > 25)) { if (MOB_FLAGGED(ch, MOB_AGGRESSIVE | MOB_AGGR_TO_ALIGN)) { found = FALSE; for (vict = world[IN_ROOM(ch)].people; vict && !found; vict = vict->next_in_room) { if (IS_NPC(vict) || !CAN_SEE(ch, vict) || PRF_FLAGGED(vict, PRF_NOHASSLE)) continue; if (MOB_FLAGGED(ch, MOB_WIMPY) && AWAKE(vict)) continue; if (MOB_FLAGGED(ch, MOB_AGGRESSIVE ) || (MOB_FLAGGED(ch, MOB_AGGR_EVIL ) && IS_EVIL(vict)) || (MOB_FLAGGED(ch, MOB_AGGR_NEUTRAL) && IS_NEUTRAL(vict)) || (MOB_FLAGGED(ch, MOB_AGGR_GOOD ) && IS_GOOD(vict))) { /* Can a master successfully control the charmed monster? */ if (aggressive_mob_on_a_leash(ch, ch->master, vict)) continue; if(GET_CLASS(ch) == MOB_CLASS_THIEF) hit(ch, vict, SKILL_BACKSTAB); else hit(ch, vict, TYPE_UNDEFINED); found = TRUE; } } } } /* Mob Memory */ if (MOB_FLAGGED(ch, MOB_MEMORY) && MEMORY(ch)) { found = FALSE; for (vict = world[IN_ROOM(ch)].people; vict && !found; vict = vict->next_in_room) { if (IS_NPC(vict) || !CAN_SEE(ch, vict) || PRF_FLAGGED(vict, PRF_NOHASSLE)) continue; for (names = MEMORY(ch); names && !found; names = names->next) { if (names->id != GET_IDNUM(vict)) continue; /* Can a master successfully control the charmed monster? */ if (aggressive_mob_on_a_leash(ch, ch->master, vict)) continue; if (GET_POS(ch) != POS_SLEEPING) { found = TRUE; act("'Hey! You're the fiend that attacked me!!!', exclaims $n.", FALSE, ch, 0, 0, TO_ROOM); hit(ch, vict, TYPE_UNDEFINED); } } } } /* * Charmed Mob Rebellion * * In order to rebel, there need to be more charmed monsters * than the person can feasibly control at a time. Then the * mobiles have a chance based on the charisma of their leader. * * 1-4 = 0, 5-7 = 1, 8-10 = 2, 11-13 = 3, 14-16 = 4, 17-19 = 5, etc. */ if (AFF_FLAGGED(ch, AFF_CHARM) && ch->master && num_followers_charmed(ch->master) > (GET_CHA(ch->master) - 2) / 3) { if (!aggressive_mob_on_a_leash(ch, ch->master, ch->master)) { if (CAN_SEE(ch, ch->master) && !PRF_FLAGGED(ch->master, PRF_NOHASSLE)) hit(ch, ch->master, TYPE_UNDEFINED); stop_follower(ch); } } /* Helper Mobs */ if (MOB_FLAGGED(ch, MOB_HELPER) && !AFF_FLAGGED(ch, AFF_BLIND | AFF_CHARM)) { found = FALSE; for (vict = world[IN_ROOM(ch)].people; vict && !found; vict = vict->next_in_room) { if (ch == vict || !IS_NPC(vict) || !FIGHTING(vict)) continue; if (IS_NPC(FIGHTING(vict)) || ch == FIGHTING(vict)) continue; act("$n jumps to the aid of $N!", FALSE, ch, 0, vict, TO_ROOM); hit(ch, FIGHTING(vict), TYPE_UNDEFINED); found = TRUE; } } /* Add new mobile actions here */ } /* end for() */ }