1 #include <stdio.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <stdlib.h> 5 #include <sys/wait.h> 6 7 static void sig_int(int); 8 9 int main(void){ 10 char buf[100]; 11 pid_t pid; 12 int status; 13 14 if(signal(SIGINT,sig_int) == SIG_ERR) 15 printf("signal error"); 16 17 printf("$ "); 18 while(fgets(buf,100,stdin) != NULL){ 19 if(buf[strlen(buf)-1] == ‘\n‘) 20 buf[strlen(buf)-1]=0; 21 22 if( (pid = fork()) < 0){ 23 printf("fork error\n"); 24 }else if(pid == 0){ 25 execlp(buf,buf,(char *)0); 26 printf("couldn‘t execute:%s\n",buf); 27 exit(127); 28 } 29 30 if(pid = waitpid(pid,&status,0) < 0) 31 printf("waitpid error\n"); 32 33 printf("$ "); 34 } 35 exit(0); 36 } 37 38 void sig_int(int signo){ 39 printf("interrupt\n"); 40 printf("$ "); 41 fflush(NULL); 42 }
运行结果:
原文:https://www.cnblogs.com/yinguojin/p/10952370.html