Calling setpgid/setsid when spawning external programs

703 Views Asked by At

When running an external program from my code, I have these three options:

  1. Do nothing after fork(). The external program will be in its parent's process group.
  2. Call setpgid(0, 0) or similar, so the external program will get its own process group.
  3. 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:

  1. I run :!gnome-open somefile.pdf, which starts a PDF viewer and returns immediately.
  2. I then run, for example, :!cat, and while cat is running, send a SIGQUIT using CTRL-\.

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.)

0

There are 0 best solutions below