Is os.FindProcess sufficient for verifying process existence?
In scenarios where the PID of a process is known, you might wonder if utilizing os.FindProcess alone adequately establishes the process's existence. This article delves into this specific scenario and provides an alternative approach that leverages operating system principles.
os.FindProcess limitations
Alternative approach using kill -s 0
import ( "log" "os/exec" "strconv" ) func checkPid(pid int) bool { out, err := exec.Command("kill", "-s", "0", strconv.Itoa(pid)).CombinedOutput() if err != nil { log.Println(err) } if string(out) == "" { return true // pid exist } return false }
Improved process existence detection
Conclusion
While os.FindProcess provides initial indications of process existence, embracing the traditional Unix approach using kill -s 0 offers more comprehensive verification and insights into process ownership.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3