SIGUSR1 and SIGUSR2 signals for Linux

Source: Internet
Author: User
Tags signal handler

SIGUSR1 user-defined signal default processing: Process termination
SIGUSR2 user-defined signal default processing: Process termination

When a process calls fork, because the child process replicates the storage image of the parent process at the beginning, the address of the signal capture function is meaningful in the child process, so the child process inherits the signal processing of the parent process.
However, when the child process calls exec, because the exec runs the new program and overwrites the storage image inherited from the parent process, the signal capture function is meaningless in the new program, so exec changes the signal that was originally set to capture to the default action.

C + + Parent-child processes communicate using SIGUSR1 and SIGUSR2

#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <sys /wait.h>void handler (int signo) {switch (signo) {case SIGUSR1://Processing signal SIGUSR1 printf ("Parent:catch Sigus    R1\n "); break;    Case SIGUSR2://Processing signal SIGUSR2 printf ("Child:catch sigusr2\n");        Default://This example does not support printf ("should not being here\n");    Break    }}int Main (void) {pid_t ppid, cpid;        Set the signal handler for two signals if (signal (SIGUSR1, handler) = = Sig_err) {//Set error perror ("Can ' t set handler for sigusr1\n");    Exit (1);        } if (signal (SIGUSR2, handler) = = Sig_err) {//Set error perror ("Can ' t set handler for sigusr2\n");    Exit (1);        } ppid = Getpid ();//get Parent Process ID if ((cpid = fork ()) < 0) {perror ("fail to fork\n");    Exit (1); } else if (cpid = = 0) {//child process sends a signal to the parent process SIGUSER1 if (Kill (Ppid, SIGUSR1) = =-1) {perror ("fail to send Signa            L\n ");        Exit (1); } while (1)////cycle, wait for the parent process signal} else {sleep (1);//hibernate, ensure the child process runs first, and send SIGUSR1 signal//parent process to itself send SIGUSER2 signal if (Kill (Cpid, SIGUSR            2) = =-1) {perror ("fail to send signal\n");        Exit (1);        }//must sleep, otherwise the child process cannot capture the SIGUSER2 signal sleep (1); printf ("would kill child\n");//Output Prompt if (Kill (Cpid, SIGKILL) = = 1) {//Send SIGKILL signal, kill child process perror ("fail to S            End Signal\n ");        Exit (1);            } if (Wait (NULL) ==-1) {//Reclaim subprocess State, avoid zombie process perror ("fail to wait\n");        Exit (1);    }printf ("Child had been killed.\n"); } return;

  

Simple programs for capturing SIGUSR1 and SIGUSR2

#include <stdio.h> #include <signal.h> #include <unistd.h>static void sig_usr (int); int main (void) {        if (signal (SIGUSR1, sig_usr) = = Sig_err)            printf ("Can not catch sigusr1\n");        if (Signal (SIGUSR2, sig_usr) = = Sig_err)            printf ("Can not catch sigusr2\n");        for (;;)                Pause ();} static void sig_usr (int signo) {        if (Signo = = SIGUSR1)            printf ("Received sigusr1\n");        else if (Signo = = SIGUSR2)            printf ("Received sigusr2\n");        else            printf ("Received signal%d\n", signo);}
Operation Result:
[[email protected] apue]$/a.out &[1] 2581[[email protected] apue]$ KILL-USR1 2581received sigusr1[[email protected] apue]$ KILL-USR2 2581received sigusr2[[email protected] apue]$ kill 2581[1]+ Terminated              ./a.out

  

SIGUSR1 and SIGUSR2 signals for Linux

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.