sigaction () function

sigaction function    

Modification signal processing operation (which is usually a function of a capture register signal Linux)

    int sigaction (int signum, const struct sigaction * act, struct sigaction * oldact); Success: 0; failed: -1, sets errno

parameter:

act: incoming parameters, the new approach.

              oldact: outgoing parameters, the old approach. [Signal.c]

struct sigaction structure

    struct sigaction {

        void (* sa_handler) (int);

        void     (*sa_sigaction)(int, siginfo_t *, void *);

        sigset_t sa_mask;

        you sa_flags;

        void     (*sa_restorer)(void);

    };

       sa_restorer: This element is obsolete and should not be used, POSIX.1 standard will not specify the element. (Deprecated)

       sa_sigaction: When sa_flags SA_SIGINFO flag is designated using the signal processing program. (rarely use) 

Key master:

       ① sa_handler: handler name after the specified signal capture (ie registration function). SIG_IGN table can also be assigned to ignore or perform the default action table SIG_DFL

       ② sa_mask: signal processing function is invoked, the desired set of mask signals (signal word shielding). Note: Only shielded take effect during the processing function is called, it is a temporary setting.

       ③ sa_flags: usually set to 0, the default attribute table.   

/***
signaction.c
***/
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
#include<signal.h>

void docatch(int signo)
{
    printf("%d signal is catch\n",signo);
}

int main()
{
    int ret;
    struct sigaction act;

    act.sa_handler = docatch;
    sigemptyset(&act.sa_mask);
    sigaddset(&act.sa_mask,SIGQUIT);
    act.sa_flags = 0;

    ret = sigaction(SIGINT,&act,NULL);
    if(ret < 0 )
    {
        perror("sigaction error");
        exit(1);
    }

    while(1);

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11333984.html