Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why process created by exec.Start() quits if its parent is killed by SIGINT?

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.

like image 442
rpbear Avatar asked Oct 08 '13 09:10

rpbear


People also ask

What happens to a child process when the parent is killed?

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

Does sigint kill child process?

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.

What happens to the child process when the parent process kills dies first explain with an example?

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.

Does exit kill child processes?

Exiting from a process does not end its child processes directly. The SIGHUP signal may end children in some cases.


2 Answers

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.

like image 120
nos Avatar answered Sep 28 '22 08:09

nos


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.

like image 27
Intermernet Avatar answered Sep 28 '22 07:09

Intermernet