问题描述:

I've tried man(3) setsid, but it only explains how to use it, I don't quiet understand when is setsid useful?

网友答案:

A session is a set of processes which shares a controlling terminal. setsid is useful when you want to start a new session, because you have started to be connected to a new terminal -- such as when starting a shell inside a terminal emulator -- or you want a daemon (which you don't want to be associated with a controlling terminal).

The best explanation I know of these aspect is in R.W. Stevens Advanced programming in the Unix environment.

网友答案:

Why do we need to group processes? Consider the situation in which you wish to shut down cleanly, and that includes sending a signal to your children. There is an inherent race condition: a SIGCHLD has not been received, so you know that child is still alive. So you send a signal. But the child terminates before the signal is sent and another (unrelated) process starts up and gets the same pid as the child to which the signal was sent. The signal then goes to the new, unrelated process. This is bad. So, rather than sending a signal to specific pids, you signal the process group. When the child dies and a new process begins with the original pid, the new process is not part of the process group and the problem described above is avoided.

网友答案:

This is useful as part of becoming a daemon, i.e. disconnecting the process from controlling terminal. See also daemon(3).

相关阅读:
Top