Two signals should be ignored when using the system function

Source: Internet
Author: User
Tags exit printf

In section 8.13, we show the implementation of a system function. However, that version did not process the signal. POSIX.1 requires system to ignore SIGINT and Sigquit and block sigchld. Before showing the correct handling of these signals, let's see why we need to worry about the processing of these signals.

The following code uses the system version of section 8.13 to invoke the Ed editor. (This editor has been part of the UNIX system for a long time.) We use it here because it is an interactive program that captures interrupt and exit signals. If we call a shell and enter a break, it captures the interrupt and prints a question mark. The ED program also sets the exit signal to the layout so that it is ignored. )

<span style= "FONT-SIZE:18PX;" ><strong> #include <signal.h>  
      
static void
sig_int (int signo)  
{  
    printf ("Caught SIGINT \ n ");  
}  
      
static void
sig_chld (int signo)  
{  
    printf ("Caught sigchld\n");  
}  
      
int
main (void)  
{  
    if (signal (SIGINT, sig_int) = = Sig_err) {  
        printf ("Signal (SIGINT) error\n");  
        Exit (1);  
    }  
    if (Signal (SIGCHLD, sig_chld) = = Sig_err) {  
        printf ("Signal (SIGCHLD) error\n");  
        Exit (1);  
    }  
    if (System ("/bin/ed") < 0) {  
        printf ("System () error");  
        Exit (1);  
    }  
    Exit (0);  
} </strong></span>

The code above captures both SIGINT and SIGCHLD. The results of the operation are:
$./a.out
A (add text command)
This is one line of text
. (terminating add mode)
1, $p (printing starting from the first line)
This is one line of text
W Temp.foo (write buffer to file)
25 (25 bytes written)
Q (Exit)
Caught SIGCHLD

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.