[Linux Study Notes] usage of kill and kill-9 and how to implement elegant process exit

Source: Internet
Author: User
Tags exit in signal handler

[Linux Study Notes] usage of kill and kill-9 and how to exit the process elegantly 1. kill and signals the kill we mentioned here refers to the kill used as the shell command (in contrast, there is a system call called kill in the linux system, man 2 kill can be used to view its functions and usage. Input man kill in the shell terminal to see that kill is used to send a specified signal to a specified process or process group, end the process/process group. -S option can specify the specific signal to be sent. If not specified, the SIGTERM signal is sent to the specified process/process group by default. If the process does not capture the signal logic, then, SIGTERM is used to terminate the process.
The list of signals that can be sent by kill can be viewed by kill-l, and the specific meanings of these signals can be viewed by man 7 signal. On my machine, the POSIX standard signal output by man 7 signal is as follows (the signal supported by kill is also a non-standard signal not defined by POSIX, which is not extracted here, for more information, see man ). [Plain] Signal Value Action Comment inclusighup 1 Term Hangup detected on controlling terminal or death of controlling process SIGINT 2 Term found from keyboard SIGQUIT 3 Core Quit from keyboard SIGILL 4 Core Illegal Instruction SIGABRT 6 core Abort signal from abort (3) SIGFPE 8 Core Floating point exception SIGKILL 9 Term Kill signal SIGSEGV 11 Core Invalid memory reference SIGPIPE 13 Term Broken pipe: write to pipe with no readers SIGALRM 14 Term Timer signal from alarm (2) SIGTERM 15 Term Termination signal SIGUSR1 30,10, 16 Term User-defined signal 1 SIGUSR2 31,12, 17 Term User-defined signal 2 SIGCHLD 20,17, 18 Ign Child stopped or terminated SIGCONT 19,18, 25 Continue if stopped SIGSTOP 17,19, 23 Stop process SIGTSTP 18,20, 24 Stop typed at tty SIGTTIN 21,21, 26 Stop tty input for background process SIGTTOU 22,22, 27 Stop tty output for background process
In the above output result, 1st is the signal name; 2nd is the corresponding signal value. Note that some signal names correspond to three signal values, this is because these signal values are related to the platform. the description of the three signal values in man's Manual is as follows: the first one is usually valid for alpha and iSCSI, the middle one for i386, ppc and sh, and the last one for mips. the 3rd column lists the actions taken after the operating system receives the signal. Term indicates that the default action is to terminate the process, Ign indicates that the default action is to ignore the signal, and Core indicates that the default action is to terminate the process and output the core dump at the same time, stop indicates that the process is stopped by default. The 4th column is a description of the effect of the signal, which is easy to understand and will not be described here. Specifically, the signals SIGKILL and SIGSTOP cannot be captured by the application or blocked or ignored by the operating system. 2. the difference between kill pid and kill-9 pid the function of kill pid is to send SIGTERM to the process whose process number is pid (this is the signal sent by kill by default ), the signal is a signal that ends the process and can be captured by the application. If the application does not capture and respond to the logic code of the signal, the default action of the signal is kill the process. This is a recommended practice for terminating a specified process.
Kill-9 pid sends SIGKILL (the number of the signal is 9) to the process with the pid. As described above, SIGKILL cannot be captured by the application, it cannot be blocked or ignored. The action is to immediately end the specified process. In layman's terms, the application cannot "perceive" the SIGKILL signal at all. It is killed by the operating system that receives the SIGKILL signal without any preparation. Obviously, in this "brute force" situation, the application has no chance to release the resources currently occupied. In fact, the SIGKILL signal is sent directly to the init process. After receiving the signal, it is responsible for terminating the process specified by the pid. In some cases (for example, if the process is already hang and cannot respond to normal signals), kill-9 can be used to end the process. If a Process terminated by kill is a parent Process that has created a child Process, the child Process will become an Orphan Process. In this case, the exit status of the sub-process cannot be captured by the application process (because the application as the parent process does not exist), but it should not adversely affect the entire linux system. 3. how can an application exit the Linux Server elegantly? applications often run for a long time. During the running process, many system resources may be applied and many statuses may be saved. In these scenarios, we hope that the process can release resources, dump the current status to the disk, or print some important logs before exiting, that is, to exit the gracefully ). From the above introduction, it is not difficult to see that elegant exit can be achieved by capturing SIGTERM. Specifically, it usually takes only two steps: 1) register the processing function of the SIGTERM signal and prepare for process exit in the processing function. You can use signal () or sigaction () to register the signal processing function. We recommend that you use the latter to set the signal response function. The simpler the logic of the signal processing function, the better. The common practice is to set a bool flag variable in the function to indicate that the process has received the SIGTERM signal and is ready to exit.
2) in the main () of the main process (! BQuit) logic to detect the flag variable. Once bQuit is set to true in the signal handler function, the main process exits the while () loop, the next step is to release resources or the current status of the dump process or record the logs. After these actions are completed, the main process exits.
 

Related Article

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.