int main(int argc, char **argv) { pid_t chid = 0; int flag = 0; //用来标记程序是否后台 int exec_flag = 0; int length = 0; if(2 > argc){ printf("Usage "); exit(0); } length = strlen(argv[2]); if(argv[2][length - 1] == '@'){ flag = 1; argv[2][length - 1] = 0; } if(argc == 4 && !strcmp(argv[3], "@")){ flag = 1; } if(!strcmp(argv[1], "start")){ exec_flag = START; }else if(!strcmp(argv[1], "stop")){ exec_flag = STOP; }else if(!strcmp(argv[1], "restart")){ exec_flag = RESTART; }else if(!strcmp(argv[1], "status")){ exec_flag = STATUS; } switch (exec_flag){ case START: start_program(argv[2], flag); break; case STOP: stop_program(argv[2]); break; case RESTART: restart_program(argv[2], flag); break; case STATUS: status_program(argv[2]); } return 0; }
int main(int argc, char *argv[]) { /* definition of basic variables */ int state; /* Handle signals. This is useful to intercept accidental Ctrl+C * which would otherwise just kill the process without any cleanup. * This could also be useful when the process is managed by some * other program, e.g. systemD. * - SIGINT will be send by pressing Ctrl+C * - SIGTERM will be send by process managers, e.g. systemD * wishing to stop the process * * Windows has its own ctrl handlers, we only really care about * Ctrl+C, but others could be implemented as well (see above). */ #ifdef POSIX struct sigaction sa, osa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = &stop_program; sigaction(SIGINT, &sa, &osa); sigaction(SIGTERM, &sa, &osa); #else if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE)) { log_error("Control handler could not be installed, Ctrl+C won't work"); } #endif /* initiate the logfile and start logging */ state = log_init(); if (state != 0) { /* * if creating a logfile fails we have to terminate the program. * The error message then has to go directly to the screen */ printf("creating a logfile failed. Program is aborting...\n"); stop_program(state); return state; } /* * set default values, read config file and process cli arguments, * which override settings in the config file */ config.dHistMinInterval = 350; config.dHistPercentage = 5; config.dInterFrameDelay = 10; config.dBufferlength = 1376256; state = load_config("configurations//SO2Config.conf", &config); if (state != 0) { log_error("loading configuration failed"); stop_program(1); return 1; } state = process_cli_arguments(argc, argv, &config); if (state != 0) { log_error("Could not handle command line arguments"); return state; } /* Initialise parameter structures */ structInit(&sParameters_A, &config, 'a'); structInit(&sParameters_B, &config, 'b'); /* initialize IO */ state = io_init(&config); if (state != 0) { log_error("io_init failed"); stop_program(1); return state; } /* init filterwheel */ state = filterwheel_init(&config); if (state != 0) { log_error("failed to initialize filterwheel"); stop_program(1); return state; } log_message("filterwheel initialized"); /* open filterwheel */ state = filterwheel_send(FILTERWHEEL_OPENED_A); if (state != 0) { /* this is critical if this function fails no camera handle is returned */ log_error("failed to open filterwheel"); stop_program(1); return state; } log_message("filterwheel opened"); /* initiate camera */ state = camera_init(&sParameters_A); if (state != 0) { /* this is critical if this function fails no camera handle is returned */ log_error("camera_init for Camera A failed"); stop_program(1); return state; } log_message("camera A initialized"); state = camera_init(&sParameters_B); if (state != 0) { /* this is critical if this function fails no camera handle is returned */ log_error("camera_init for Camera B failed"); stop_program(1); return state; } log_message("camera B initialized"); /* configure camera */ state = camera_config(&sParameters_A); if (state != 0) { log_error("config camera A failed"); stop_program(1); return 1; } log_message("camera A configured"); state = camera_config(&sParameters_B); if (state != 0) { log_error("config camera B failed"); stop_program(1); return 1; } log_message("camera B configured"); /* set exposure */ state = setExposureTime(&sParameters_A, &config); if (state != 0) { log_error("setExposureTime for cam B failed"); stop_program(1); return 1; } log_message("exposure time for cam A set"); state = setExposureTime(&sParameters_B, &config); if (state != 0) { log_error("setExposureTime for cam B failed"); stop_program(1); return 1; } log_message("exposure time for cam B set"); /* * Starting the acquisition with the exposure parameter set in * configurations.c and exposureTimeControl.c */ state = startAquisition(&sParameters_A, &sParameters_B, &config); log_message("Aquisition stopped"); if (state != 0) { log_error("Aquisition failed"); stop_program(1); return 1; } /* we're done! */ stop_program(0); return 0; }
void restart_program(char *name, int flag) { int pid = -1; stop_program(name); start_program(name, flag); }
BOOL CtrlHandler(DWORD fdwCtrlType) { if (fdwCtrlType == CTRL_C_EVENT) stop_program(1); return 0; }