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;
}
After the sighup signal is captured in the program, a message is printed, 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 sigkill signal, 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.
Http://hi.baidu.com/shiqyn/blog/item/9afa9afabce2ed8c9f514656.html