gf/geg/os/gproc/gproc3.go

36 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"os"
"time"
"gitee.com/johng/gf/g/os/glog"
"gitee.com/johng/gf/g/os/gproc"
)
// 父进程销毁后,使用进程管理器查看存活的子进程。
// 请使用go build编译后运行不要使用IDE运行因为IDE大多采用的是子进程方式执行。
func main () {
if gproc.IsChild() {
glog.Printfln("%d: I am child, waiting 10 seconds to die", gproc.Pid())
//p, err := os.FindProcess(os.Getppid())
//fmt.Println(err)
//p.Kill()
time.Sleep(2*time.Second)
glog.Printfln("%d: 2", gproc.Pid())
time.Sleep(2*time.Second)
glog.Printfln("%d: 4", gproc.Pid())
time.Sleep(2*time.Second)
glog.Printfln("%d: 6", gproc.Pid())
time.Sleep(2*time.Second)
glog.Printfln("%d: 8", gproc.Pid())
time.Sleep(2*time.Second)
glog.Printfln("%d: died", gproc.Pid())
} else {
p := gproc.NewProcess(os.Args[0], os.Args, os.Environ())
p.Start()
glog.Printfln("%d: I am main, waiting 3 seconds to die", gproc.Pid())
time.Sleep(3*time.Second)
glog.Printfln("%d: died", gproc.Pid())
}
}