Various signal collection and kill signal list in Linux

Source: Internet
Author: User

1. sighup Signal

In UNIX, the process organization structure is session. It contains one foreground process group and one or more background process groups. A process group contains multiple processes. A session may have a session first process, and a session first process may have a control terminal. A process group may have a first process in the process group. The process ID of the first process in the process group is equal to that of the Process Group. There may be, but under some circumstances, none. The process that interacts with the terminal is the foreground process, otherwise it is the background process. Sighup will be sent to the corresponding process in the following three cases: 1. When the terminal is disabled, this signal is sent to the first process of the session and the process submitted as the job (that is, the process submitted with the & symbol). 2. When the first process of the session exits, this signal is sent to every process in the front-end process group of the session. 3. If the parent process exits, the process becomes an orphan process group, if a process in the process group is in the stopped status (receives the sigstop or sigtstp signal), the signal is sent to every process in the process group.The system processes the sighup signal by default and terminates the process that receives the signal. Therefore, if the program does not capture the signal, the process will exit when it receives the signal.Next we will observe several cases where the process exits due to terminal shutdown. Here the process exits because it receives the sighup signal. Login Shell is the first process of the session. First, write a test program. The Code is as follows: # include <stdio. h>
# Include <signal. h>
Char ** ARGs;
Void exithandle (INT sig)
{
Printf ("% s
: Sighup received ", argS [1]);
}
Int main (INT argc, char ** argv)
{
ARGs = argv;
Signal (sighup, exithandle );
Pause ();
Return 0;
} A piece of information is printed after the sighup signal is captured in the program, and pause () stops the program. The compiled execution file is sigtest. 1. Command: sigtest front> tt.txt operation: Close the terminal result: the content of the tt.txt file is front: sighup received ed. Original cause: sigtest is the front-end process. After the terminal is closed, according to the above 1st cases, login shell, as the first process of the session, will receive the sighup signal and then exit. According to the 2nd cases, as the foreground process, sigtest receives the sighup signal from login shell. 2. Command: sigtest back> tt.txt & Operation: Close the terminal. Result: The content of the tt.txt file is back: sighup received ed. The original cause is: sigtest is a submitted job, according to the above 1st cases, sigtest will receive a sighup signal. 3. Life order: Write a shell with the content of [sigtest &], and then execute the shell operation: Close the terminal. Result: PS-Ef | grep sigtest: the process is still running. The TT file is empty. The source cause: when the shell is executed, sigtest is submitted as a job, and the shell exits, as a result, sigtest becomes an orphan process and is no longer the job of the current session. Therefore, sigtest is neither the first process of the session nor the job and will not receive sighup. At the same time, the orphan process is a background process. Therefore, after login shell exits, it does not send sighup to sigtest, because it only sends this signal to the foreground process. Article 3 it is said that if a process group becomes an orphan process group, if a process is in the stopped status, it will also receive a sighup signal, but sigtest is not in the stopped status, so it will not receive a sighup signal. 4. Command Line: nohup sigtest> TT operation: Close the terminal. Result: The TT file is empty. The original cause: nohup can prevent the process from receiving the sighup signal, we know under what circumstances the process will exit after the terminal is closed, and under what circumstances it will not exit.


The following methods can be used to prevent a process from exiting after the terminal is closed: 1. Write a shell with the following content: Trap "" sighup # the function of this sentence is to shield the sighup signal, trap can shield many signals, such as sigtest2 and nohup sigtest, which can be executed directly on the command line. If you want to continue other operations after this operation, you can compile nohup sigtest & 3 and shell, the content is as follows: sigtest & in fact, any way to turn the process into an orphan process can be done, including fork's immediate termination of the parent process.

2. sigchld Signal

After a sub-process dies, it sends a sigchld signal to the parent process.

When a process calls the exit command to end its own life, it is not actually destroyed, but it leaves a data structure called Zombie (the system calls exit, it is used to exit a process, but it is only limited to converting a normal process into a zombie process and cannot completely destroy it ). In the status of a Linux Process, a zombie process is a very special one. It has abandoned almost all the memory space, no executable code, and cannot be scheduled, only one location is retained in the process list, and information such as the exit status of the process is recorded for collection by other processes. In addition, zombie processes no longer occupy any memory space. It requires its parent process to collect dead parts for it. If its parent process does not have the sigchld signal processing function installed, it calls wait or waitpid () to wait until the child process ends, if the signal is not explicitly ignored, it will remain in zombie state. If the parent process ends, the INIT process will automatically take over the child process and send a zombie to it, it can still be cleared. However, if the parent process is a loop and does not end, the child process will remain zombie, which is why many zombie processes sometimes exist in the system.

2. sigterm Signal

Kill () can send sigterm in the past; the kill command also uses sigterm signal by default.


The sigterm signal processing function, which is usually used to clean and exit the signal; or the program can ignore this signal to prevent false positives.
Sigterm is the default signal sent
To a process by the kill or killall commands.
It causes the termination of a process, but unlike the sigkillsignal,
It can be caught and interpreted (or ignored) by the process. therefore, sigterm is more akin to asking a process to terminate nicely, allowing cleanup and closure of files. for this reason, on running UNIX systems during shutdown, init issues
Sigterm to all processes that are not essential to powering off, waits a few seconds, and then issues sigkill to forcibly terminate other processes to allow the computer to halt.

Linux kill signal list
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD
18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN
22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO
30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1
36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5
40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9
44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13
52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9
56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5
60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1
64) SIGRTMAX

List, numbered 1 ~ The 31 signal is a traditional Unix-supported signal, which is an unreliable signal (non-real-time) numbered 32 ~ The 63 signal was expanded later, called a reliable signal (real-time signal ). The difference between unreliable and reliable signals is that the former does not support queuing and may cause signal loss, while the latter does not.

Below we will discuss the signal number smaller than sigrtmin.

1) sighup

This signal is sent at the end of the user terminal connection (normal or abnormal). Generally, when the control process of the terminal ends, it notifies all jobs in the same session, they are no longer associated with control terminals.

When you log on to Linux, the system assigns a session to the login user ). All programs running on this terminal, including foreground and background process groups, generally belong to this session. When you log out of Linux, processes in the frontend process group and in the background that are output to the terminal will receive a sighup signal. The default operation for this signal is to terminate the process. Therefore, the Process with terminal output in the frontend and backend will be aborted. However, this signal can be captured. For example, wget can capture the sighup signal and ignore it. In this way, even if you log out of Linux, wget can continue downloading.

In addition, this signal is used to notify the daemon that is out of the relationship with the terminal to re-read the configuration file.

2) SIGINT

The interrupt signal is sent when you type the intr character (usually Ctrl-C) to notify the foreground process group to terminate the process.

3) sigquit

Similar to SIGINT, but controlled by the quit character (usually Ctrl-\). A process generates a core file when it exits because it receives a sigquit exit. In this sense, it is similar to a program error signal.

4) sigill

The execution of invalid commands is usually caused by errors in the executable file, or the attempt to execute data segment. Stack Overflow may also generate this signal.

5) sigtrap

Generated by breakpoint commands or other trap commands. Used by debugger.

6) SIGABRT

The signal generated by calling the abort function.

7) sigbus

Invalid Address, including memory address alignment error. For example, you can access an integer with four characters in length, but its address is not a multiple of 4. It differs from SIGSEGV in that the latter is triggered by illegal access to valid storage addresses (for example, access does not belong to your own bucket or read-only bucket ).

8) sigfpe

When a fatal arithmetic operation error occurs, it includes not only floating point operation errors, but also overflow and Division 0 and other arithmetic errors.

9) sigkill

It is used to immediately end the running of the program. This signal cannot be blocked, processed, or ignored. If the Administrator finds that a process cannot be terminated, he can try to send this signal.

10) SIGUSR1

Reserved for users

11) SIGSEGV

Try to access the memory not allocated to you, or try to write data to the memory address that has no write permission.

12) sigusr2

Reserved for users

13) sigpipe

Pipe rupture. This signal is usually generated during inter-process communication. For example, for two processes that use FIFO (pipeline) Communication, the read pipeline is written to the pipeline if it is not opened or unexpectedly terminated, and the write process receives the sigpipe signal. In addition, when writing a socket to two processes that communicate with a socket, the read process is terminated.

14) sigalrm

The scheduled clock signal is used to calculate the actual time or clock time. The alarm function uses this signal.

15) sigterm

The program terminate signal. Unlike sigkill, the signal can be blocked and processed. It is usually used to require the program to exit normally. The shell command kill generates this signal by default. If the process cannot be terminated, we will try sigkill.

17) sigchld

When the child process ends, the parent process receives this signal.

If the parent process does not process this signal and does not wait for the child process (wait), although the child process terminates, it still occupies a table item in the kernel table, at this time, sub-processes are called Zombie processes. In this situation, we should avoid (parent process, ignore sigchild signal, capture it, or wait, its derived child process, or the parent process is terminated first, at this time, the termination of the sub-process is automatically taken over by the INIT process ).

18) sigcont

Let a stopped process continue execution. this signal cannot be blocked. A handler can be used to allow a program to complete a specific job when it changes from the stopped state to the continue execution. for example, re-display the prompt...

19) sigstop

Stop (stopped) process execution. Note the difference between it and terminate and interrupt: the process has not ended, but is paused. This signal cannot be blocked, processed, or ignored.

20) sigtstp

The process is stopped, but the signal can be processed and ignored. This signal is sent when you type the susp character (usually Ctrl-Z ).

21) sigttin

When a background job reads data from a user terminal, all processes in the job receive the sigttin signal.

22) sigttou

Similar to sigttin, but received when writing terminal (or modifying terminal mode.

23) sigurg

"Urgent" data or out-of-band data is generated when it reaches the socket.

24) sigxcpu

The CPU time limit is exceeded. This limit can be read/changed by getrlimit/setrlimit.

25) sigxfsz

When a process attempts to expand a file so that it exceeds the file size resource limit.

26) sigvtalrm

The virtual clock signal is similar to sigalrm, but the CPU time occupied by the process is calculated.

27) sigprof

Similar to sigalrm/sigvtalrm, but includes the CPU time used by the process and the time when the system calls it.

28) sigwinch

Window size changed.

29) sigio

The file descriptor is ready for input/output operations.

30) sigpwr

Power failure

31) sigsys

Invalid system call.

Among the Signals listed above, signals that cannot be captured, blocked, or ignored by the program are: sigkill, sigstop

Signals that cannot be restored to the default action include: sigill and sigtrap.

By default, the signals that cause process abortion include: SIGABRT, sigbus, sigfpe, sigill, sigiot, sigquit, SIGSEGV, sigtrap, sigxcpu, sigxfsz

By default, the process exits with the following signals: sigalrm, sighup, SIGINT, sigkill, sigpipe, sigpoll, sigprof, sigsys, sigterm, SIGUSR1, sigusr2, and sigvtalrm.

By default, the signal that causes the process to stop is: sigstop, sigtstp, sigttin, sigttou

Signals ignored by default include sigchld, sigpwr, sigurg, and sigwinch.

In addition, sigio exits in svr4 and is ignored in 4.3bsd; sigcont continues when the process is suspended; otherwise, it is ignored and cannot be blocked.

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.