编写守护程序时,需要为其创建一个新会话。
例如:
fork a child process
exit main process
call setsid in child process
我想知道为什么我们需要为它创建一个新会话?我们可以通过
$myserver&
谢谢
当您在 xterm 中在后台启动程序时,如果myprog &
您在没有命令 exit 的情况下关闭 xterm,该程序将被终止。创建一个新的会话可以解决这个pb(该进程是xterm的子进程,当你创建一个新的会话时他不是xterm的子进程。你可以关闭xterm而不退出,程序仍然在运行)。
man 2 setsid
给出:
setsid() creates a new session if the calling process is not a process group leader.
The calling process is the leader of the new session, the process group leader of the new
process group, and has no controlling terminal.
The process group ID and session ID of the calling process are set to the PID of
the calling process.
The calling process will be the only process in this new process group and in
this new session.
我用 xterm 给你的例子是合理的has no controlling terminal
。