I found a strange problem in golang.The program executed by exec.Start()
will quit if the parent program is interrupt by the signal os.Interrupt,while the child program will NOT quit if the parent program exit normally.What's the difference between that two conditions?
For examples:
package main
import "fmt"
import "os"
import "time"
import "os/exec"
func main(){
cmd := exec.Command("sleep", "100000")
cmd.Env = os.Environ()
fmt.Println(cmd.Env)
cmd.Start()
time.Sleep(1e9*20)
return
}
In the later condition the parent of sleep 100000
will be the init process
after 20s if we didn't interrupt the main program.
When a parent process dies before a child process, the kernel knows that it's not going to get a wait call, so instead it makes these processes "orphans" and puts them under the care of init (remember mother of all processes).
The parent process installs a SIGCHLD handler that is executed when its child process terminates. The parent process forks a child process to execute the command. The parent process sleeps for the specified number of seconds. When it wakes up, it sends its child process a SIGINT signal to kill it.
If you deliberately kill the intermediate process, then the child won't be killed when the parent dies. If the child exits before the parent, then the intermediate process will try to kill the original child pid, which could now refer to a different process.
Exiting from a process does not end its child processes directly. The SIGHUP signal may end children in some cases.
What's happening is that if you send a process SIGINT (as e.g. os.Interrupt does), all proceses in the same process group will also get that signal (which includes child processes) - SIGINT will by default terminate a process.
If however a parent process exits normally, not because of SIGINT or similar, a process in the same process group does not get any signal - it will continue to run, but be adopted by the init process. This is not specific to Go.
I think this is because you're using Start
instead of Run
.
Start starts the specified command but does not wait for it to complete.
whereas:
Run starts the specified command and waits for it to complete.
Therefore Start
will just handover the process to the operating system when the Go (parent) process exits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With