判断进程是否存在的几种方法


一共有四种方法,见粗体。
注意受pid回收的影响,可能不准确;
Checking for the Existence of a Process  Verifying the existence of a particular process ID doesn’t guarantee that a par-ticular program is still running. Because the kernel recycles process IDs as processes are born and die
the same process ID may, over time, refer to a different process. Furthermore, a particular process ID may exist, but be a zombie (i.e., a
process that has died, but whose parent has not yet performed a wait() to obtain its termination status).
(1) The kill() system with sig argument is specified as 0  (the so-called null signal), 
then no signal is sent. Instead, kill() merely performs error checking to see if the process can be signaled. Read another way, this means 
we can use the null signal to test if a process with a specific process ID exists. If sending a null signal fails with the error ESRCH, then we know the process doesn’t
exist. If the call fails with the error EPERM (meaning the process exists, but we don’t have permission to send a signal to it) or succeeds (meaning we do have permission
to send a signal to the process), then we know that the process exists.
(2)The wait() system calls:  They can be employed only if the monitored process is a child of the caller.
(3)Semaphores and exclusive file locks: If the process that is being monitored continuously holds a semaphore or a file lock, then, if we can acquire the semaphoreor lock, we know the process has terminated.
(4)IPC channels such as pipes and FIFOs: We set up the monitored process so that it holds a file descriptor open for writing on the channel as long as it is alive.
Meanwhile, the monitoring process holds open a read descriptor for the chan-nel, and it knows that the monitored process has terminated when the write
end of the channel is closed (because it sees end-of-file).
(5)The /proc/PID interface: For example, if a process with the process ID 12345 exists, then the directory /proc/12345 will exist, and we can check this using a
call such as stat().
All of these techniques, except the first and the last, are unaffected by recycling of process IDs.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值