os/exec: document method to check if a process is alive

Fixes #34396
This commit is contained in:
Moritz Poldrack 2023-06-13 13:04:33 +02:00
parent b0e1707f94
commit 4f06764109
No known key found for this signature in database
GPG Key ID: 8426D7099B8C6DDA
2 changed files with 21 additions and 1 deletions

View File

@ -86,7 +86,9 @@ func Getppid() int { return syscall.Getppid() }
// about the underlying operating system process.
//
// On Unix systems, FindProcess always succeeds and returns a Process
// for the given pid, regardless of whether the process exists.
// for the given pid, regardless of whether the process exists. To test whether
// the process actually exists, see whether p.Signal(syscall.Signal(0)) reports
// an error.
func FindProcess(pid int) (*Process, error) {
return findProcess(pid)
}

View File

@ -9,6 +9,7 @@ package os_test
import (
"internal/testenv"
. "os"
"syscall"
"testing"
)
@ -25,3 +26,20 @@ func TestErrProcessDone(t *testing.T) {
t.Errorf("got %v want %v", got, ErrProcessDone)
}
}
func TestUNIXProcessAlive(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()
p, err := StartProcess(testenv.GoToolPath(t), []string{"sleep", "1"}, &ProcAttr{})
if err != nil {
t.Skipf("starting test process: %v", err)
}
defer p.Kill()
proc, _ := FindProcess(p.Pid)
err = proc.Signal(syscall.Signal(0))
if err != nil {
t.Errorf("OS reported error for running process: %v", err)
}
}