os.FindProcess 足以驗證流程是否存在嗎?
在已知進程 PID 的情況下,您可能想知道是否使用os.FindProcess 本身就足以確定進程的存在。本文深入研究了這一特定場景,並提供了一種利用作業系統原理的替代方法。
os.FindProcess 限制
使用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 }
改進的程序存在檢測
結論
雖然os.FindProcess 提供了進程存在的初步指示,但使用kill -s 0 擁抱傳統的Unix 方法提供了更全面的驗證和對流程所有權的見解。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3