Turn-unix system process to Sigterm, SIGUSR1 and SIGUSR2 signal processing

Source: Internet
Author: User

Unix system processes for sigterm, SIGUSR1, and SIGUSR2 signal processing
    • Vfhky | Time: 2015-08-05-17:41 | Category: CSeries

Long time no update blog, write an article in addition to weeding. This article focuses on a simple example of how the Unix/linux process, if captured and processed SIGTERM , SIGUSR1 and SIGUSR2 signals.

Let's start by explaining these three signals:

Signal (signal) is a common way of communicating between processes (IPC) in a *nix system. SIGTERM: The process termination signal, the effect is equivalent to the *nix shell without 9 of the KILL command, SIGUSR1: reserved for the user to use the signal; SIGUSR2: With SIGUSR1, reserved for the user to use the signal.
1 How the signal is processed

For SIGTERM , SIGUSR1 and SIGUSR2 These three signals, if not captured or ignored (Sighold) in the current process, the *nix system kernel automatically exits the process and reclaims the process resources. So how does the process 优雅 handle the signal? Usually needs to be handled according to the actual situation: for example, ignoring, the sigset( SIGCLD, SIG_IGN ) parent process ignores the child process exit signal, by the kernel init process recycling processing, for example, blocking, sighold( SIGCLD ) temporarily the process exit signal added to the signal mask, and then by deleting and re sigrelse( SIGCLD ) -receive the signal.

2 program Demo

Below is a simple demonstration of how a process handles sigterm signals with two programs.

2.1 sigterm Signal Processing process: SIGTERM.C

Registers a handler function sterminate for the signal sigterm, and then the pause() function keeps the current process in a pending state until a signal is received to return. The static variable Iextflag determines whether the process exits directly in the SIGTERM signal processing function sterminate.

1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435363738394041424344454647
/*** @FileName SIGTERM.C* @Describe use kill function to send a signal-a process with the PID coming from argv[1]* @Author Vfhky 2015.08.03 https://typecodes.com/cseries/unixsigtermkill.html* @Excute./sigterm means sterminate () do not exit before; ./sigterm 1 means sterminate () containing Exit function.* @Return 0*/#include <stdio.h>#include <stdlib.h>#include <signal.h>voidSterminate();StaticIntIextflag=0;IntMain(Intargc,Char**Argv){Sigset(SIGTERM,Sterminate);Printf("Main process_pid=[%d] would sleep!\ n",Getpid());If(argc==2&&*Argv[1]==0x31)Iextflag=1;The process would be hung to forever if not receving a signal.Pause();If there is a signal coming, this code would keep going.Printf("Main process begin to work!\ n");Let the process being sleeping sec, and exit then.Sleep(10);Printf("Main Process exit!\ n");Return0;}/*** Functions to handle the signal of SIGTERM.* If the value of static variable iextflag is isn't equal zero, this func would exit without waiting for the main function to Do to other things.*/void sterminate(){ printf( "Get a SIGTERM signal!  \ n" ); if( iextflag ) { printf( "Making process exit!  \ n" ); exit(exit_success); } return;               
2.2 Sigterm Signal sending process: SIGKILL.C

The program function is simple: kill send the SIGTERM signal to the previous Sigterm process through the function.

1 2 3 4 5 6 7 8 910111213141516171819202122
/*** @FileName SIGKILL.C* @Describe use kill function to send a signal to a process with the PID from argv[1]* @Author Vfhky 2015.08.03 https://typecodes.com/cseries/unixsigtermkill.html* @Return success:0 Failure:-1*/#include <stdio.h>#include <stdlib.h>#include <signal.h>IntMain(Intargc,Char**Argv){If(argc!=2){Printf("Usage:./sigkill process_pid");return -1} printf ( "you'll send a signal to the process=[%s]!< span class= "se" >\n "argv[1< Span class= "P" >] ); kill (atoi (argv [1sigterm ); printf ( "Send over! \n "); return 0;}        
3 Execution Results

gcc -Wall sigterm.c -o sigterm gcc -Wall sigkill.c -o sigkill generate executables sigterm and Sigkill using GCC commands and compilation.

then enter the./sigterm to turn on the sigterm signal processing process sigterm, the interface is shown below the current process PID, due to pause enter the hung up state.

Main process_pid=[27346] 'll sleep!

At this point, enter the following command on another shell terminal, and the Sigkill process sends a sigterm signal to the sigterm process (27346).

[[email protected] signal]#./sigkill 27346##### function equivalent to the KILL command directly through the Linux shell: [[email protected] signal]# kill 27346

The sigterm process (27346) returns from the pause function after receiving the signal, and calls the Sterminate function for processing. After the function has finished processing, continue with the code that follows the pause function. So, after 10 seconds of sleep, the entire process exits.

[Email protected] signal]#/sigterm Main process_pid=[27346] would sleep! Get a SIGTERM signal! Main process begin to work! Main Process exit! [Email protected] signal]#

Effect:

4 other Instructions

For a ./sigterm 1 signal sent by the sigterm process after it receives the Sigkill process, the sigterm process does not enter hibernation but exits directly in the Sterminate function.

Because processes and SIGUSR1 SIGUSR2 Other signals SIGTERM can be treated exactly as they are, this is no longer demonstrated (just replace the sigterm in two programs).

Turn-unix system process to Sigterm, SIGUSR1 and SIGUSR2 signal processing

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.