【问题标题】:POSIX compliant way to find out if a process with a certain PID is alive找出具有特定 PID 的进程是否处于活动状态的符合 POSIX 标准的方法
【发布时间】:2015-03-07 16:18:54
【问题描述】:

我从https://serverfault.com/q/366474 了解到,以下代码是一种符合 POSIX 标准的方法,用于测试 PID = $pid 的进程是否处于活动状态。它使用kill -0 命令。

# First code sample
pid=100

if kill -0 "$pid" 2> /dev/null
then
    echo PID "$pid" is alive.
else
    echo PID "$pid" not found.
fi

pid=100

我学到的另一种方法是使用ps -p 命令。

# Second code sample
if ps -p "$pid" > /dev/null
then
    echo PID "$pid" is alive.
else
    echo PID "$pid" not found.
fi

我一直试图弄清楚使用kill -0 命令的第一个代码示例是否确实符合 POSIX。我发现最接近它的是http://pubs.opengroup.org/onlinepubs/9699919799/utilities/kill.html 的“退出状态”和“基本原理”部分中的陈述。重点是我加的。

退出状态

应返回以下退出值:

0 为每个pid操作数找到至少一个匹配的进程,并且指定的信号被成功处理了至少一个 匹配过程。

>0 发生错误。

基本原理

...

...

早期的提议发明了名称 SIGNULL 作为信号名称 signal 0(由 POSIX.1-2008 的系统接口卷使用测试 为一个进程的存在而不向它发送信号)。由于 signal_name 0 在这种情况下可以明确使用,SIGNULL 已经 删除。

但我在 POSIX.1-2008 的系统接口卷中找不到提及这一点。

对于第二个代码示例,我在http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html 上没有发现任何东西可以保证如果$pid 命令没有找到具有匹配$pid 的进程,则退出状态将大于零。

这是我的问题。

  1. 第一个代码示例真的是一种符合 POSIX 标准的方式来确定 PID = $pid 的进程是否确实存在?
  2. 能否请您指向 POSIX.1-2008 系统接口卷中的相应页面和部分,我可以在其中找到记录的信号 0 的行为?
  3. 第二个代码示例是否是一种符合 POSIX 标准的方式来确定 PID = $pid 的进程是否处于活动状态?

【问题讨论】:

  • 你引用了授权它的文本。你不会从标准中得到任何更明确的东西。注意kill -0 pid即使进程存在也可能失败;它只是意味着您,用户名mortal,不能向具有该 PID 的进程发送信号,因为该进程属于 deity。因此,在其限制范围内,它由标准中定义的行为来保证。如果命令失败,你只知道你不能向进程发送信号,但这可能是因为它不存在,也可能是因为你不被允许向它发出信号。

标签: shell process posix


【解决方案1】:

来自kill(1p) 手册页:

SYNOPSIS
   kill -s signal_name pid ...

   kill -l [exit_status]

   kill [-signal_name] pid ...

   kill [-signal_number] pid ...

 ...

   -signal_number

          Specify a non-negative decimal  integer,  signal_number,  repre‐
          senting  the  signal  to  be used instead of SIGTERM, as the sig
          argument in the effective call  to  kill().  The  correspondence
          between  integer  values  and the sig value used is shown in the
          following table.

   The effects of specifying any signal_number other than those listed  in
   the table are undefined.

                          signal_number   sig Value
                          0               0
                           ...

 ...

SEE ALSO
   Shell Command Language, ps, wait(), the  System  Interfaces  volume  of
   IEEE Std 1003.1-2001,   kill(),   the   Base   Definitions   volume  of
   IEEE Std 1003.1-2001, <signal.h>

来自kill(3p) 手册页:

SYNOPSIS
   #include <signal.h>

   int kill(pid_t pid, int sig);

DESCRIPTION
   The kill() function shall send a signal to a process or a group of pro‐
   cesses  specified by pid. The signal to be sent is specified by sig and
   is either one from the list given in <signal.h> or 0. If sig is 0  (the
   null  signal),  error  checking  is performed but no signal is actually
   sent. The null signal can be used to check the validity of pid.

 ...

SEE ALSO
   getpid(), raise(), setsid(), sigaction(), sigqueue(), the Base  Defini‐
   tions volume of IEEE Std 1003.1-2001, <signal.h>, <sys/types.h>

编辑:

  1. 是的。 POSIX 指定信号 0 将透明地从 kill 传递到 kill() 以及信号 0 的作用。

  2. kill() function描述的第一段。

  3. POSIX 指定 ps 的非零返回状态意味着“发生错误”,但 not 是否指定没有与给定参数匹配的命令是错误。因此,应将第二段代码的行为视为系统特定的。

【讨论】:

  • 这些手册页是否来自 POSIX 标准?您能否分享这些文件的 URL?能否请您回答我提出的三个具体问题?
  • 我在我的 Linux 系统上找到了这些手册页;它们应该是 POSIX 手册页的逐字副本,并添加了特定于 Linux 的序言(“此手册页是 POSIX 程序员手册的一部分。此接口的 Linux 实现可能不同(有关详细信息,请参阅相应的 Linux 手册页) Linux 行为),或者该接口可能无法在 Linux 上实现。”)。不过,我会挖掘这三个问题的具体参考资料。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多