Sighup signal and control terminal

Source: Internet
Author: User
From: http://bbs.chinaunix.net/viewthread.php? Tid = 766356

In UNIX, the process organization structure is that the session 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 closed, the signal is sent to the first process of the session and the process submitted as a job (that is, the process submitted with the & symbol)
2. When the first process of the session exits, the signal is sent to each process in the foreground Process Group of the session.
3. If the process exits, the process becomes an orphan process group, and the process in the process group is in the stopped state (the sigstop or sigtstp signal is received ), 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, ifProgramDoes not capture this signal. When this signal is received, the process exits.

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,CodeAs follows:
# Include <stdio. h>
# Include <signal. h>
Char ** ARGs;
Void exithandle (INT sig)
{
Printf ("% s: sighup received \ n", 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 file is front: sighup received ed.
Cause: sigtest is a front-end process. After the terminal is closed, according to the 1st cases mentioned above,
As the first process of the session, Login Shell receives the sighup signal and then exits,
According to the 2nd cases, sigtest serves as the foreground process,
The sighup signal sent by login shell is received.
2. Command: sigtest back> tt.txt &
Operation: Close the terminal.
Result: The content of the TT file is back: sighup encoded ed.
Cause: sigtest is a submitted job. According to the 1st cases mentioned above,
Sigtest receives the sighup signal.
3. Write a shell with the content
Sigtest &
Execute the shell
Operation: Close the terminal.
Result: PS-Ef | grep sigtest shows that the process is still running and the TT file is empty.
Cause: when the shell is executed, sigtest is submitted as a job, and then 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, so no sighup will be sent after login shell exits.
To sigtest, because it only sends the 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 state,
The sighup signal is also received, but the sigtest is not in the stopped status,
So no sighup signal will be received
4. nohup sigtest> TT
Operation: Close the terminal.
Result: The TT file is empty.
Cause: nohup prevents the process from receiving the sighup Signal

So far, we know under what circumstances the process will exit after the terminal is closed, and under what circumstances it will not exit.

to disable the process after the terminal is closed, you can use the following methods:
1. Write a shell, the content is as follows
trap "" sighup # the function of this sentence is to shield the sighup signal, trap can shield many signals
sigtest
2. nohup sigtest can be executed directly on the command line,
if you want to continue other operations after this operation, you can add sigtest &
3. Write a shell, the content is as follows
sigtest &
In fact, any way to turn a process into an orphan process is acceptable, including fork and immediate termination of the parent process

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.