Sample Header Ad - 728x90

Why does the termination of the parent terminate the child when it is in the suspend (T) state?

2 votes
1 answer
61 views
I am using Ubuntu (linux). I have the following two simple programs. Parent:
package main

import (
	"fmt"
	"syscall"
	"time"
)

func main() {
	attr := &syscall.ProcAttr{
		Files: []uintptr{0, 1, 2},
		Sys: &syscall.SysProcAttr{ // child in its own group
			Setpgid: true,
			Pgid:    0,
		},
	}

	_, err := syscall.ForkExec("./child/child", []string{"child"}, attr)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	for {
		fmt.Println("Parent is live")
		time.Sleep(10 * time.Second)
	}
}
Child (child in its own group):
package main

import (
	"fmt"
	"time"
)

func main() {
	for {
		fmt.Println("hi from child")
		time.Sleep(time.Second * 20)
	}
}
After starting the parent program (./parent), the result of calling the ps (specifically, ps -t /dev/pts/0 -o pid,ppid,pgid,stat,comm) command is as follows: PID PPID PGID STAT COMMAND 466922 466896 466922 Ss bash 467049 466922 467049 Sl+ parent 467054 467049 467054 Sl child After terminating the parent process (either with kill -SIGKILL 467049, kill -SIGINT 467049 or CTRL-C), child continues to work (S/R state). This is exactly what I expect. PID PPID PGID STAT COMMAND 466922 466896 466922 Ss+ bash 467054 467049 467054 Sl child What confuses me is the following scenario. Firstly, I start the parent process (./parent). Result of the ps command is the same as in the previous case. Then I suspend the child process with kill -SIGTSTP 467054 or kill -SIGSTOP 467054. The result of ps command is the following: PID PPID PGID STAT COMMAND 466922 466896 466922 Ss bash 467049 466922 467049 Sl+ parent 467054 467049 467054 Tl child Then, I terminate the parent process (either with kill -SIGKILL 467049, kill -SIGINT 467049 or CTRL-C). **For some reason, in this case child is terminated as well!** Result of the ps command: PID PPID PGID STAT COMMAND 466922 466896 466922 Ss+ bash **How? Why?**
Asked by Yakog (517 rep)
Jan 22, 2025, 11:03 AM
Last activity: Jan 22, 2025, 01:38 PM