Processes, Threads, and Interprocess Communication – Signal Catching

Signal capture

Signal capture process:

  1. Define the execution function handle of the new signal.
  2. Use the signal/sigaction function to associate the custom handle with the specified signal.

signal function:

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);

Function: capture signal to execute custom function

Return value: returns the original signal processing function on success, returns SIG_ERR on failure

parameter:

signo The signal type to set

The signal processing function specified by handler: SIG_DFL stands for default mode; SIG_IGN stands for ignore signal;

The system recommends using the sigaction function, because the behavior of signal in different Unix systems is not exactly the same.

sigactionFunction:< /strong>

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);

}

parameter:

signum: the signal to process

act, oldact: The new behavior and old behavior of the signal processing, which is a sigaction structure.

The members of the sigaction structure are defined as follows:

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

sa_sigaction: Another signal processing function, it has three parameters, you can get more detailed information about the signal.

The reference values of sa_flags are as follows:

SA_SIGINFO: Use sa_sigaction member instead of sa_handler as signal handler function

SA_RESTART: Automatically re-initiate system calls interrupted by signals.

SA_RESETHAND: Reset to the default processing method after signal processing.

SA_NODEFER: Invalidate the shielding of the signal, that is, the signal can still be issued during the execution of the signal processing function.

re_restorer: is an obsolete data field

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <linux/posix_types.h>

typedef void (*sighandler_t)(int);

sighandler_t old act;

void handle(int sig)
{
    if (sig == SIGINT)
    {
        printf("I cath the SIGINT \\
");
    }
    else if (sig == SIGALRM)
    {
        printf("second timer \\
");
        alarm(1);
    }
    //signal(SIGINT,oldact);
}

void mytimer(int sig)
{


}

int main()
{
    struct sigaction act;//definition structure
    act.sa_handler = handle;//struct member initialization
    act.sa_flags = 0;
    sigemptyset( & amp;act.sa_mask);//clear signal set
    sigaction(SIGINT, & amp;act, NULL);//capture ctr + c signal
    alarm(1);
    sigaction(SIGALRM, & amp;act, NULL);//Capture alarm signal
    // oldact = signal(SIGINT,handle);

    while (1)
    {
        sleep(1);
    }

}


Timer implementation

ualarm (cyclic send)

useconds_t ualarm (useconds_t usecs, useconds_t interval);

In seconds, the first parameter is the first generation time, and the second parameter is the interval generation

int setitimer (int which, const struct itimerval *new_value, struct itimerval *old_value);

Function: Send alarm signal regularly Parameters:
which:
ITIMER_REAL: Decrement by elapsed time. Send SIGALRM signal
ITIMER_VIRTUAL: Calculate the execution time of the process (user mode). Send SIGVTALRM signal

ITIMER_PROF: The process calculates time in both user mode (that is, when the program is executed) and kernel mode (that is, when the process is scheduled). Send SIGPROF signal

ITIMER_PROF: The process calculates time both in user mode (that is, when the program is executed) and in kernel mode (that is, when the process is scheduled). Send SIGPROF signal
new_value: responsible for setting the timout time
old_value: store the old timeout value, generally specified as

NULL struct itimerval{
struct timeval it_interval;//alarm clock trigger cycle

struct timeval it_value;//alarm clock trigger time

};
struct timeval {
time_t ty_sec /*seconds */
suseconds_t ty_usec / microseconds
}

Timing capture of alarm signal through signal capture

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <linux/posix_types.h>
#include <sys/time.h>


typedef void (*sighandler_t)(int);

sighandler_t old act;

void handle(int sig)
{
    if (sig == SIGINT)
    {
        printf("I cath the SIGINT \\
");
    }
    else if (sig == SIGALRM)
    {
        printf("second timer \\
");
        // alarm(1);
    }
    //signal(SIGINT,oldact);
}


int main()
{
    struct sigaction act;
    act.sa_handler = handle;
    act.sa_flags = 0;
    sigemptyset( &act.sa_mask);
    // sigaction(SIGINT, & act, NULL);
    // alarm(1);
    struct itimerval timevalue;
    timevalue.it_interval.tv_sec = 1;//alarm clock trigger cycle time interval 1S
    timevalue.it_interval.tv_usec = 0;//alarm clock trigger period
    timevalue.it_value.tv_sec = 5;//Alarm clock trigger time 5s start timer
    timevalue.it_value.tv_usec = 0;//Alarm trigger time

    setitimer(ITIMER_REAL, & amp;timevalue, NULL);//Send alarm signal regularly
    sigaction(SIGALRM, & amp;act, NULL);//Capture alarm signal
    // oldact = signal(SIGINT,handle);

    while (1)
    {
        // sleep(1);
    }

}

useSIGCHLD signal< /strong>Recycling child processes

Conditions for generating SIGCHLD

1 When the child process terminates

2 When the child process receives the SIGSTOP signal and stops

3 The child process is in the stopped state and wakes up after receiving SIGCONT

#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

// handle signal function
void handle(int sig)
{
    wait(NULL);//Recycle the child process, block the process
    printf("Get sig =%d\\
", sig);

}


int main()
{
    pid_t pid;
    struct sigaction act;
    act.sa_handler = handle;
    act.sa_flags = 0;
    sigemptyset( &act.sa_mask);

    pid = fork();

    if (pid > 0)
    {
        //wait(NULL);

        sigaction(SIGCHLD, & amp;act, NULL);//Capture alarm signal
        while (1)
        {
            printf("this is father process\\
");
            sleep(1);
        }

    }
    else if (pid == 0)
    {
        sleep(5);
        exit(0);//The child process exits
    }


}

syntaxbug.com © 2021 All Rights Reserved.