Golang os.Getpid(), FindProcess() and Getppid() functions example

package os

Golang os.Getpid(), FindProcess() and Getppid() functions usage example. Useful in situation where you want to trace a child processes of a parent process within your Golang program.

 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)

 proc, err := os.FindProcess(pid) // or replace with other process number

 if err != nil {
 panic(err)
 }

 fmt.Println(proc.Pid)

 }

References :

http://golang.org/pkg/os/#Getpid

http://golang.org/pkg/os/#Getppid

http://golang.org/pkg/os/#FindProcess

Advertisement