Check if a process exists in go way

后端 未结 7 1883
甜味超标
甜味超标 2020-12-31 02:29

If I have the PID of a process, is os.FindProcess enough to test for the existing of the process? I mean if it returns err can I assume that it\'s terminated (o

7条回答
  •  春和景丽
    2020-12-31 02:56

    If a previously known pid is not found in the system (not sure of go functions), it means process has definitely terminated and has been joined (on Unix, with wait call) too.

    But other way around is not necessarily true. Just because a pid exists, it does not quarantee it is same process as before. There are only 65535 valid pids in standard Linux for example, and they can get re-used when there is a wrap-around. However, if you check reasonably often, for practical purposes you don't need to care about this (as long as pid of wrong new process being found is not a security vulnerability or something else critical, which somebody might try to trigger intentionally for malicious purposes).

    Related links (and Related questions on their right columns):

    • https://superuser.com/questions/135007/how-are-pids-generated
    • https://unix.stackexchange.com/questions/26677/will-process-ids-be-recycled-what-if-you-reach-the-maximal-id

提交回复
热议问题