// ProcessClean will take the dyn array ProcessArr and call a waitpid on every child, catching any errors. void ProcessCleanup(int ProcessCount, int ProcessArr[]){ for (int i = 0; i < ProcessCount; i++){ waitpid(ProcessArr[i], &pidstatus,NULL); // find a way to check if exit is wrong if ((!WIFEXITED(pidstatus)) || (WIFSIGNALED(pidstatus)) || (WEXITSTATUS(pidstatus))) printf("[%s] Child Process ID #%i did not terminate successfully. \n",CurrTime(ltime),ProcessArr[i]);} }
void GreenKoopa::CreateGreenKoopaIfAny() { MovingAnimator* g = NULL; for(Dim i = 0; i < 15; i++) for(Dim j = 0; j < VIEW_WINDOW_TILE_WIDTH; j++){ Dim y = i; Dim x = j + Terrain::GetTileLayer()->GetViewWindow().GetX(); Rect view = Terrain::GetTileLayer()->GetViewWindow(); if(Enemies::GetFromMap(y, x) == 194) { if(Enemies::IsEnemyActive(y, x)) continue; if(suspending["greenkoopaleft"].empty()) Create("greenkoopaleft"); g = suspending["greenkoopaleft"].back(); suspending["greenkoopaleft"].pop_back(); if(!g) return ; g->GetSprite()->SetX((j % VIEW_WINDOW_TILE_HEIGHT) * 16); g->GetSprite()->SetY(y * 16); g->SetLastTime(CurrTime()); AnimatorHolder::MarkAsRunning(g); Enemies::SetEnemyAsActive(y,x); running["greenkoopaleft"].push_back(g); } else if(Enemies::GetFromMap(y, x) == 195) { if(Enemies::IsEnemyActive(y, x)) continue; if(suspending["redkoopaleft"].empty()) Create("redkoopaleft"); g = suspending["redkoopaleft"].back(); suspending["redkoopaleft"].pop_back(); if(!g) return ; g->GetSprite()->SetX((j % VIEW_WINDOW_TILE_HEIGHT) * 16); g->GetSprite()->SetY(y * 16); g->SetLastTime(CurrTime()); AnimatorHolder::MarkAsRunning(g); Enemies::SetEnemyAsActive(y,x); running["redkoopaleft"].push_back(g); } } }
void GreenKoopa::ComeOutFromShell(Animator* a, void* v) { MovingAnimator* g; char toCreate[20]; const std::string id = ((MovingAnimator*)a)->GetSprite()->GetCurrFilm()->GetID(); if(!strcmp(id.c_str(), "greenkoopahit")) strcpy(toCreate, "greenkoopaleft"); else strcpy(toCreate, "redkoopaleft"); AnimatorHolder::MarkAsSuspended(a); //AnimatorHolder::Cancel(a); suspendingdead[id].push_back((MovingAnimator*)a); if(suspending[toCreate].size() == 0) Create(id.c_str()); g = suspending[toCreate].back(); suspending[toCreate].pop_back(); assert(g); g->GetSprite()->SetX( ((MovingAnimator*) a)->GetSprite()->GetX() ); g->GetSprite()->SetY( ((MovingAnimator*) a)->GetSprite()->GetY() - 16 ); g->SetLastTime(CurrTime()); AnimatorHolder::MarkAsRunning(g); running[toCreate].push_back(g); }
/*__________________________________________________________________________________*/ MSFunctionType(FarPtr(unsigned long)) MSGetTimeAddr(TMSGlobalPtr g) { return &CurrTime(g); }
/*=========================================================================== External MidiShare functions implementation =========================================================================== */ MSFunctionType(unsigned long) MSGetTime (TMSGlobalPtr g) { return CurrTime(g); }
void pipedecryption(FILE *input, int CoreNumber){ int ProcessCount = 0; fd_set rfds; fd_set wfds; // we are only using the number of cores - because we restrict one to the parent int ProcessTotal = CoreNumber-1; int ProcessArr[ProcessTotal]; FD_ZERO(&rfds); FD_ZERO(&wfds); // timeval is used for the SELECT function struct timeval tv; tv.tv_sec = 100; tv.tv_usec = 0; int EncryptionDirectoryPipe[ProcessTotal][2]; int ProcessReadyPipe[ProcessTotal][2]; int ProcessStatus = 1; int RRcount = 0; char* directory[2050]; int directorysize; time_t ltime; for(int i = 0; i < ProcessTotal; i++){ // create the pipes. Include error handling if it fails. if(pipe(EncryptionDirectoryPipe[i])||pipe(ProcessReadyPipe[i])) { printf("[%s] Parent #%i failed to pipe. Exiting.\n",CurrTime(ltime), getpid()); exit(-1); } int ChildID = fork(); if (ChildID){ // parent process FD_SET(ProcessReadyPipe[i][1],&wfds); FD_SET(EncryptionDirectoryPipe[i][1],&wfds); FD_SET(ProcessReadyPipe[i][0],&rfds); // close the writing end of the pipe, we willl be reading from the child for its status close(ProcessReadyPipe[i][1]); // close reading end, we will be writing to the child processes close(EncryptionDirectoryPipe[i][0]); // add the child into the ProcessArr to keep track of processes created ProcessArr[i] = ChildID; } else if (ChildID == 0){ // child process // close appropriate ends of the pipe for the child process close(ProcessReadyPipe[i][0]); close(EncryptionDirectoryPipe[i][1]); while(1){ // read from the pipe the length of the incoming msg into dirsize. If its null, the pipe is empty, // so finish the loop with a break. int dirsize = 0; int readstatus = read(EncryptionDirectoryPipe[i][0], &dirsize,sizeof(int)); if (readstatus==0){ break;} // get the incoming msg using the size dirsize given in the privious msg // create character array with null termination, then zero it out char directorybuffer[dirsize+1]; bzero(&directorybuffer, dirsize+1); read(EncryptionDirectoryPipe[i][0], directorybuffer,dirsize); // parse the directory into input and output pointers respectfully char *inouttemp = strtok(directorybuffer, " "); char *input = inouttemp; inouttemp = strtok(NULL, " "); char *output = inouttemp; output = strtok(output, "\n"); // run decryption printf("[%s] Child Process ID #%i will decrypt %s. \n",CurrTime(ltime),getpid(),input); if(decrypt(input,output) > 0) printf("[%s] Process ID #%i decrypted %s successfully. \n",CurrTime(ltime),getpid(),input); if // Case for FCFS: send msg to parent saying its ready. write(ProcessReadyPipe[i][1],&ProcessStatus,sizeof(ProcessStatus)); } // Process is done all jobs. Close its writing pipe. printf("closing all work, jobs are done \n"); close(ProcessReadyPipe[i][1]); exit(0); } else{
// run decryption printf("[%s] Child Process ID #%i will decrypt %s. \n",CurrTime(ltime),getpid(),input); if(decrypt(input,output) > 0) printf("[%s] Process ID #%i decrypted %s successfully. \n",CurrTime(ltime),getpid(),input); if // Case for FCFS: send msg to parent saying its ready. write(ProcessReadyPipe[i][1],&ProcessStatus,sizeof(ProcessStatus)); } // Process is done all jobs. Close its writing pipe. printf("closing all work, jobs are done \n"); close(ProcessReadyPipe[i][1]); exit(0); } else{ // Parent fails to fork. Clean up previously created children prior to crash printf("[%s] Process ID #%i failed to create a child. \n",CurrTime(ltime), getpid()); ProcessCleanup(i, ProcessArr); fclose(input); exit(-1); } } // children processes are instantiated int dircount; while(fgets(directory, 2050, input)){ //Selected Algo is FCFS
/*------------------------------------ Sending --------------------------------*/ void MIDISHAREAPI MidiSendIm(short refNum, MidiEvPtr e) { MSSendIm( refNum,e, SorterList(gMem), CurrTime(gMem)); }
/*------------------------------------- Time ----------------------------------*/ unsigned long MIDISHAREAPI MidiGetTime() { return CurrTime(gMem); }
void GreenKoopa::MoveGreenKoopas(const char* id) { CommitDestructions(id); // to destroy the lost koopas for (std::list<MovingAnimator*>::iterator it=running[id].begin(); it != running[id].end(); ++it) { MovingAnimator* g = *it; Dim currPosX = g->GetSprite()->GetX(); Dim currPosY = g->GetSprite()->GetY(); Dim TileX = g->GetSprite()->GetTileX(); Dim TileY = g->GetSprite()->GetTileY(); if(Enemies::IsMarioAbove(TileX, TileY + 1)){ char toCreate[20]; const std::string id = g->GetSprite()->GetCurrFilm()->GetID(); if(!strcmp(id.c_str(), "greenkoopaleft")) strcpy(toCreate, "greenkoopahit"); else strcpy(toCreate, "redkoopahit"); MovingAnimator* d; Dim x, y; if(suspendingdead[toCreate].size() == 0) Dead(toCreate); d = suspendingdead[toCreate].back(); d->GetMovingAnimation()->SetDx(0); d->GetMovingAnimation()->SetDelay(10000); d->GetMovingAnimation()->SetContinuous(false); d->SetLastTime(currTime); suspendingdead[toCreate].pop_back(); assert(d); d->GetSprite()->SetX(x = g->GetSprite()->GetX()); d->GetSprite()->SetY(y = g->GetSprite()->GetY() + 16); d->SetLastTime(CurrTime()); AnimatorHolder::MarkAsRunning(d); walkingdead[toCreate].push_back(d); suspending[id].push_back(*it); AnimatorHolder::MarkAsSuspended(*it); running[id].erase(it); Sounds::Play("stomp"); //jump mario: Dim mx = Mario::GetMarioCurrentSprite()->GetX(); Dim my = Mario::GetMarioCurrentSprite()->GetY(); Mario::SetDimensions(mx, my); AnimatorHolder::MarkAsSuspended( Mario::GetAnimator()); Mario::SetDimensions(mx, my); Mario::ChangeState(Jumping); MovingPathAnimator* g = Mario::GetStandJump(); g->SetCurrIndex(0); g->SetLastTime(currTime); AnimatorHolder::MarkAsRunning(g); return ; } if(Enemies::CanGoLeft(TileX, TileY) && g->GetMovingAnimation()->GetDx() < 0) g->GetMovingAnimation()->SetDx(-2); else if(!Enemies::CanGoLeft(TileX, TileY) && g->GetMovingAnimation()->GetDx() < 0) g->GetMovingAnimation()->SetDx(2); else if( Enemies::CanGoRight(TileX, TileY) && g->GetMovingAnimation()->GetDx() > 0) g->GetMovingAnimation()->SetDx(2); else if( !Enemies::CanGoRight(TileX, TileY) && g->GetMovingAnimation()->GetDx() > 0) g->GetMovingAnimation()->SetDx(-2); else g->GetMovingAnimation()->SetDx(0); Rect r = g->GetSprite()->GetCurrFilm()->GetFrameBox(g->GetSprite()->GetFrame()); Dim f = r.GetHeight(); if( Enemies::IsOnAir(TileX, TileY, f-1) && !Enemies::IsOnBrick(currPosX, currPosY)) g->GetMovingAnimation()->SetDy(3); else g->GetMovingAnimation()->SetDy(0); } }
int main (int argc, char *argv[]) { //has to be exactly 2 arguments if (argc != 2){ printf( "You have put too many/few arguments.\n"); exit(-1);} // start file pointer FILE *input; // open input file input = fopen(argv[1], "r"); //if input returns 0 the input file is missing if (input == 0){ printf("Input file is missing.\n"); exit(-2); } char *inouttemp; // I've set array size to 1000 to start, but will increase if its needed since its only holding ints, it wont be too large to begin with. int ArrSize = 1000; // This dynamic array will hold the pids of all the processes ProcessArr = (int*) malloc(ArrSize*sizeof(int)); int ProcessCount = 0; /* I have used a character string of 2050 because we can safely say each directory will be at most 1024 chars. So two directorys + the space inbetween is (1024*2)+1 = 2049, which I figured I'd round up to 2050 just in case. */ char str[2050]; // run while loop where it grabs each line from the txt file while (fgets(str, 2050, input)){ int ChildID = fork(); // Parent Process if (ChildID){ // increase process count and add it to the array. ProcessCount++; if (ProcessCount <= ArrSize) ProcessArr[ProcessCount-1] = ChildID; else { // if dyn array is too small, we will allocate more room and add to array. ProcessArr = (int*) realloc(ProcessArr, (ArrSize*2)*sizeof(int)); ArrSize = ArrSize*2; ProcessArr[ProcessCount-1] = ChildID;} } // New Process else if (ChildID == 0){ // set char pointers that will point to the input output names according to the // strtok. Only need to run strtok twice since its 2 columns inouttemp = strtok(str, " "); char *input = inouttemp; // get next token inouttemp = strtok(NULL, " "); char *output = inouttemp; output = strtok(output, "\n"); printf("input is %s\n", input); printf("output is %s\n", output); // run decrypt using input and output params printf("[%s] Child Process ID #%i created to decrypt %s. \n",CurrTime(ltime),getpid(),input); decrypt(input,output); printf("[%s] Decryption of %s complete. Process ID #%i Exiting. \n",CurrTime(ltime),input, getpid()); free(ProcessArr); exit(0); } else { // New Process fails to create, will do a process cleanup for all children made before error. printf("[%s] Failed to create new process. Exiting.\n", CurrTime(ltime)); ProcessCleanup(ProcessCount, ProcessArr); free(ProcessArr); exit(1);} } // clean up of Processes and I/O files and exit ProcessCleanup(ProcessCount, ProcessArr); fclose(input); free(ProcessArr); exit(0); }