[Linux]signal function does not work

Source: Internet
Author: User
Tags signal handler

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.