goexecforkpidprocess-group

Why does os.StartProcess in Go set the child's process group id equal to the child pid


The golang library function os.StartProcess() calls setpgid(0,0) in the child process after forking.

It does this on the "linux" and "bsd" configurations, sources here: http://golang.org/src/pkg/syscall/exec_bsd.go?h=SYS_SETPGID#L105 http://golang.org/src/pkg/syscall/exec_linux.go?h=SYS_SETPGID#L117

I noticed in the past that the OSX Foundation library NSTask - (void)launch method does the same thing (undocumented). At that time it seemed like an annoyance as it makes it harder to manage trees of processes.

What is the benefit of these libraries doing this automatically?


Solution

  • The short answer is that it doesn't. It only changes the group if attr.Sys.Setpgid is true. This is to have feature parity with the system's API. It is not even an option on all OSes