Use sigaction to set signal processing mode for inter-process communication

One, sigaction function introduction

Header file: signal.h

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

Function: 1. Query the signal processing method through oldact; 2. Set the signal processing method through act.

Parameter Description:

  • signum: The signal to be operated. All signals except SIGKILL and SIGSTOP can be specified.
  • act: The new processing method of the signal to be set.
  • oldact: Get the original processing method of the signal.

Return value: 0 means success, -1 means an error occurred.

Two, structure struct sigaction description

 The struct sigaction type is used to describe the processing of signals and is defined as follows:
 struct sigaction
 {       void (*sa_handler)(int);       void (*sa_sigaction)(int, siginfo_t *, void *);       sigset_t sa_mask;       int sa_flags;       void (* sa_restorer)(void);  };





Structure member description:

sa_handler is a function pointer, and its meaning is similar to the signal processing function in the signal function.

sa_sigaction is another signal processing function, it has three parameters, you can get more detailed information about the signal. When the value of the sa_flags member contains the SA_SIGINFO flag, the system will use the sa_sigaction function as the signal processing function, otherwise it will use the sa_handler as the signal processing function. In some systems, the members sa_handler and sa_sigaction are placed in the union, so do not set them at the same time when using them.
The sa_mask member is used to specify the signal that needs to be masked during the execution of the signal processing function, especially when a signal is processed, it will be automatically put into the signal mask of the process, so this signal is not used during the execution of the signal processing function Will happen again.
The sa_flags member is used to specify the behavior of signal processing. It can be a "bitwise OR" combination of the following values.
     ◆ SA_RESTART: make the system call interrupted by the signal automatically re-initiate.
     ◆ SA_NOCLDSTOP: The parent process will not receive the SIGCHLD signal when its child process is suspended or continues to run.
     ◆ SA_NOCLDWAIT: The parent process will not receive the SIGCHLD signal when its child process exits. At this time, the child process will not become a zombie process if it exits.
     ◆ SA_NODEFER: disable the shielding of the signal, that is, the signal can still be sent out during the execution of the signal processing function.
     ◆ SA_RESETHAND: Reset to the default processing mode after signal processing.
     ◆ SA_SIGINFO: Use the sa_sigaction member instead of sa_handler as the signal processing function.

 The re_restorer member is an obsolete data field, do not use it.

Three, a simple chestnut

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>

void sigHandler(int signum)
{
    if (signum == SIGUSR1)
    {
        printf("receive signal SIGUSR1.\n");
    }
    else if (signum == SIGUSR2)
    {
        printf("receive signal SIGUSR2.\n");
    }
    else
    {
        printf("receive signal %d.\n", signum);
    }
}

int main()
{
    struct sigaction sigact;
    sigact.sa_flags = 0;
    sigact.sa_handler = sigHandler;

    sigaction(SIGUSR1, &sigact, NULL);
    sigaction(SIGUSR2, &sigact, NULL);

    printf("Current process id is %d\n", getpid());

    while (1)
    {
        int n = 0;
        char buf[512];
        if ((n = read(STDIN_FILENO, buf, 511)) == -1)
        {
            if (errno == EINTR)   // nterrupted system call 
            {
                printf("read is interrupted by signal\n");
            }
        }
        else
        {
            buf[n] = '\0';
            printf("%d bytes read %s\n", n , buf);
        }
    }
    
    return 0;
}

Results of the:

Guess you like

Origin blog.csdn.net/xunye_dream/article/details/109565874