Sigaction realizes signal processing and can successfully read

Source: Internet
Author: User
Tags printf sigint signal
MAIN.C
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <
signal.h> int cnt = 0;
    void sighandler_new (int signo, siginfo_t *psigninfo, void *preserved) {int count = ++cnt;
    printf ("%d Enter, Signo:%d\n", count, Signo);
    Sleep (3);
printf ("%d leave, Signo:%d\n", count, Signo);
    } int main (void) {struct Sigaction act; Act.sa_flags = Sa_siginfo |
    Sa_restart;
    Act.sa_sigaction = sighandler_new;
    If sigquit in the signal screening set, and the program is executing SIGINT signal processing function,//At this time a sigquit signal, then sigquit temporarily blocked,//sigint signal processing function after the execution of the Sigquit signal processing function.
    Sigemptyset (&act.sa_mask);
    Sigaddset (&act.sa_mask, sigquit);

    Sigaction (SIGINT, &act, NULL);
    Char Buf[8];
    int iret;
        do {iret = Read (Stdin_fileno, buf, sizeof (BUF)-1);
            if ( -1 = = Iret) {perror ("read error");
        Exit (-1);
        } Buf[iret] = ' + ';
    Puts (BUF);
}while (strcmp (buf, "quit\n"));
    return 0; }
Compile Run

Comment out the main function

  Sigemptyset (&act.sa_mask);  Sigaddset (&act.sa_mask, sigquit);

After these two lines of code, the compile run results are as follows

Both figures are first to send a SIGINT signal, and then send a sigquit signal. The first figure blocks the sigquit signal, and the second one does not block the sigquit signal

The Man Handbook has:

int sigaction (int signum, const struct sigaction *act,
              struct sigaction *oldact);

struct Sigaction {
    void     (*sa_handler) (int);
    void     (*sa_sigaction) (int, siginfo_t *, void *);
    sigset_t   Sa_mask;
    int        sa_flags;
    void     (*sa_restorer) (void);
};

Sa_restart: automatically re-initiates the system call that is interrupted by the signal.
If the program is blocking a system call in the event of a signal, such as calling the Read () function, the signal is processed and then returned from the blocked system. If you do not specify this parameter, the Read function reads failed after the interrupt processing is complete, as shown in the following figure.

Related Article

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.