/* 주어진 fitAction의 유형을 따라서 기능 실행하는 함수 */ IDE_RC iduFitManager::validateFitAction( iduFitAction *aFitAction ) { switch ( aFitAction->mType ) { case IDU_FIT_ACTION_HIT: { IDE_TEST( hitServer(aFitAction) != IDE_SUCCESS ); break; } case IDU_FIT_ACTION_JUMP: { IDE_TEST( abortServer(aFitAction) != IDE_SUCCESS ); break; } case IDU_FIT_ACTION_KILL: { IDE_TEST( killServer(aFitAction) != IDE_SUCCESS ); break; } case IDU_FIT_ACTION_SLEEP: { IDE_TEST( sleepServer(aFitAction) != IDE_SUCCESS ); break; } case IDU_FIT_ACTION_TIME_SLEEP: { IDE_TEST( timeSleepServer(aFitAction) != IDE_SUCCESS ); break; } case IDU_FIT_ACTION_WAKEUP: { IDE_TEST( wakeupServerBySignal(aFitAction) != IDE_SUCCESS ); break; } case IDU_FIT_ACTION_WAKEUP_ALL: { IDE_TEST( wakeupServerByBroadcast(aFitAction) != IDE_SUCCESS ); break; } case IDU_FIT_ACTION_SIGSEGV: { IDE_TEST( sigsegvServer( aFitAction ) != IDE_SUCCESS ); break; } default: { IDE_TEST( 0 ); } } return IDE_SUCCESS; IDE_EXCEPTION_END; IDU_FIT_DEBUG_LOG( "FIT validateFitAction Failure." ); return IDE_FAILURE; }
int main() { pid_t server[5]; char str[1024]; int fd[5][2]; int serverNum = 0; int child_index = -1; displayPrompt(); while(1) { fgets(str, 1024, stdin); if(strstr(str, "createServer") != NULL) { if(serverNum == 4) { printf("cannot spawn anymore servers\n"); fflush(stdout); exit(0); } if(pipe(fd[serverNum]) < 0) { perror("pipe error"); } server[serverNum] = fork(); //Child if(server[serverNum] == 0) { char* serverName = NULL; int minProcs = 0; int maxProcs = 0; int activeProcs = 0; char* flag[3]; int fd2[10][2]; pid_t child[10]; char* throwAway = strtok(str, " "); flag[0] = strtok(NULL, " "); int m = 0; //Pass arguments while(flag[m] != NULL) { m++; flag[m] = strtok(NULL, " "); } //Set important variables minProcs = atoi(flag[0]); maxProcs = atoi(flag[1]); serverName = flag[2]; serverName[strlen(serverName) - 1] = '\0'; child_index = serverNum; printf("Creating Child PID: %d Name: %s Min Procs:%d Max Procs:%d\n", getpid(), serverName, minProcs, maxProcs); fflush(stdout); displayPrompt(); //Create children within server while(activeProcs < minProcs) { if(pipe(fd2[activeProcs]) < 0) { perror("pipe error 3"); } child[activeProcs] = fork(); //Child of server if(child[activeProcs] == 0) { fflush(stdout); createGrandChild(fd2, activeProcs); displayPrompt(); } //Parent if(child[activeProcs]){ activeProcs++; } } close(fd[child_index][1]); //Listen for command while(1) { char fromPipe[1024]; read(fd[child_index][0], fromPipe, sizeof(fromPipe)); fromPipe[strlen(fromPipe) - 1] = '\0'; char* doNothing = strtok(fromPipe, " "); if(strstr(fromPipe, "abortServer") != NULL) { char* serverToAbort = strtok(NULL, " "); abortServer(serverName, serverToAbort); } else if(strstr(fromPipe, "abortProc") != NULL) { char* onServer = strtok(NULL, " "); abortGrandChild(fd2, activeProcs, minProcs, onServer, serverName); } else if(strstr(fromPipe, "displayStatus") != NULL) { printServerStatus(serverName, getpid(), child, activeProcs); } else if(strstr(fromPipe, "createProc") != NULL) { char* onServer = strtok(NULL, " "); //Special case: Must create new process without function call :( if((activeProcs + 1) > maxProcs && strcmp(serverName, onServer ) == 0) { printf("Adding another proc would put %s over it's limit of %d procs", serverName, maxProcs); fflush(stdout); displayPrompt(); } else { if(pipe(fd2[activeProcs]) < 0) { perror("pipe error 3"); } child[activeProcs] = fork(); //Server's new child if(child[activeProcs] == 0) { printf("Creating grandchild for process %d\n", getppid()); fflush(stdout); displayPrompt(); createGrandChild(fd2, activeProcs); } if(child[activeProcs]){ activeProcs++; } } } } break; } //Parent if(server[serverNum]) { serverNum++; } } else if(strstr(str, "abortServer") != NULL) { sendCommandToChild(fd, str, serverNum); serverNum--; } else if(strstr(str, "abortProc") != NULL) { sendCommandToChild(fd, str, serverNum); } else if(strstr(str, "createProc") != NULL) { sendCommandToChild(fd, str, serverNum); } else if(strstr(str, "displayStatus") != NULL) { sendCommandToChild(fd, str, serverNum); } else { printf("please enter a valid command\n"); fflush(stdout); displayPrompt(); } } return 0; }