Go中的os.StartProcess为什么将子进程的进程组ID设置为等于子进程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?

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