When running an external program from my code, I have these three options:
- Do nothing after
fork()
. The external program will be in its parent's process group. - Call
setpgid(0, 0)
or similar, so the external program will get its own process group. - Call
setsid()
, which also separates the external program from its parent's controlling tty.
Which of the above behaviors is correct in which situations?
For example, I'm seeing a funny behavior using vim and gnome-open:
- I run
:!gnome-open somefile.pdf
, which starts a PDF viewer and returns immediately. - I then run, for example,
:!cat
, and while cat is running, send aSIGQUIT
usingCTRL-\
.
The PDF viewer crashes. This happens because both vim and gnome-open seem to spawn their subprocesses in their own process group.
It might be too obscure to be called a bug, but still I wonder who's at fault here: is there any reason for vim or gnome-open not to call setpgid
after forking, like a shell would do? Would it make more sense for gnome-open
to call setsid
instead to separate the X application from the terminal it was launched from? (I find it strange that GLib doesn't offer a standard and cross-platform way to do such things.)