#include"apue.h"#include<sys/wait.h>Static voidSig_int (int);/*Our signal-catching function*/intMainintargcChar*argv[]) {printf ("uid =%d, gid =%d\n", Getuid (), Getgid ()); CharBuf[maxline];/*From apue.h*/pid_t pid; intstatus; //register the signal handler if(Signal (SIGINT, sig_int) = =sig_err) Err_sys ("Signal Error"); printf ("%% ");/*Print prompt (printf requires% to print%)*/ while(Fgets (buf, MAXLINE, stdin)! =NULL) { if(Buf[strlen (BUF)-1] =='\ n') Buf[strlen (BUF)-1] =0;/*replace newline with null*/ if(PID = fork ()) <0) {Err_sys ("Fork Error"); } Else if(PID = =0) {/* Child*/EXECLP (buf, buf, (Char*)0); Err_ret ("couldn ' t execute:%s", BUF); Exit (127); } /*Parent*/ if(PID = Waitpid (PID, &status,0)) <0) Err_sys ("waitpid Error"); printf ("Last pid:%ld\n", (Long) (PID); printf ("%% "); } exit (0);}voidSig_int (intSigno) {printf ("sig_int:interrupt\n%%"); Exit (0);//I Add this line to exit the program through CTRL + C (SIGINT), Ctrl+d is to terminate the program}
Code from Apue Figure1.10
I compiled it with GDB debugging and found that Sig_int is never called
Finally found the problem is because the compilation option added-G, in the debug state of the signal mechanism seems to be disabled
[Linux]signal function does not work