注册信号
#include <unistd.h> #include <signal.h> #include <string.h> #include <sys/wait.h> #include <stdio.h> #define MAXLINE 100 static void sig_int(int); int main() { char buf[MAXLINE]; pid_t pid; int status; if(signal(SIGINT, sig_int) == SIG_ERR) { fprintf(stderr, "signal error.\n"); return -1; } printf("%% "); while(fgets(buf, MAXLINE, stdin) != NULL) { if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; if((pid = fork()) < 0) { fprintf(stderr, "fork error.\n"); return -1; } else if(pid == 0) { execlp(buf, buf, (char*)0); return 0; } if((pid=waitpid(pid, &status, 0)) < 0) { fprintf(stderr, "waitpid error."); return -1; } printf("%% "); } return 0; } void sig_int(int signo) { printf("interrupt\n%% "); }