fork()返回值与进程的ps命令输出不同

时间:2014-02-04 07:57:59

标签: c linux process

pid = fork();

if (pid == 0)
{
  pid = execve(command, args, envvars);

  if (pid == -1)
  {
    printf("failed to execute: %s\n", command);
  }
}
else if (pid > 0)
{
  if (strcmp(rootcmd, C_EXEC) == 0)
  {
    wait(pid);

    /** Reap any zombie processes that hang around */
    while ((pid = waitpid(-1, (int *)&status, WNOHANG)) > 0)
    {
    }

    pid = 0;
  }
}
printf("%d\n", pid);

在上面的代码中,子进程的pid值与ps -ax命令输出中找到的pid不匹配。在我尝试的示例中,从ps -ax命令返回的pid值为17,fork()返回的pid值为1701969937.有人可以帮助我理解,为什么值不同?

1 个答案:

答案 0 :(得分:0)

注意,-ax param意味着,您不仅要显示您的进程,还要显示所有进程。也许,在表中你看到UID + PID而不是pid。使用

ps -ef | pg

代替。你在那看到什么?

使用标准语法查看系统上的每个进程:

ps -e
ps -ef
ps -eF
ps -ely 

使用BSD语法查看系统上的每个进程:

ps ax
ps axu 

您确定需要BSD语法吗?