我想使用 Golang for Linux os 从特定子进程 id (pid) 获取父进程 id (ppid)我有这段代码,它提供当前进程的 ppid 和 pid,但我想检索我指定的子进程的 ppid,而不是当前进程。package main import ( "fmt" "os" ) func main() { pid := os.Getpid() parentpid := os.Getppid() fmt.Printf("The parent process id of %v is %v\n", pid, parentpid) }有没有办法像这样传递 pidos.Getppid(pid)或任何其他方法来检索 Golang 中指定 pid 的 ppid ?
2 回答

慕娘9325324
TA贡献1783条经验 获得超4个赞
我认为 go 标准库不允许您执行此操作,但是, mitchellh/go-ps等第三方软件包提供了更多信息。
例子:
import ps "github.com/mitchellh/go-ps"
...
list, err := ps.Processes()
if err != nil {
panic(err)
}
for _, p := range list {
log.Printf("Process %s with PID %d and PPID %d", p.Executable(), p.Pid(), p.PPid())
}
输出:
2019/06/12 09:13:04 Process com.apple.photom with PID 68663 and PPID 1
2019/06/12 09:13:04 Process CompileDaemon with PID 49896 and PPID 49895
您还可以用来ps.FindProcess(<pid>)查找特定进程并检查其 PPid
没有找到匹配的内容?试试慕课网站内搜索吧
- 2 回答
- 0 关注
- 428 浏览
添加回答
举报
0/150
提交
取消