简体   繁体   English

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

[英]fork() return value is not same as ps command output for the 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);

In the above code, pid value of the child process doesn't match with the pid found in the ps -ax command output. 在上面的代码中,子进程的pid值与ps -ax命令输出中找到的pid不匹配。 In the example I tried, the pid value returned from the ps -ax command is 17 and the pid value returned from the fork() is 1701969937. Can someone help me understand, why the values are different? 在我尝试的示例中,从ps -ax命令返回的pid值为17,从fork()返回的pid值为1701969937。有人可以帮助我理解为什么这些值不同吗?

Notice, -ax param means, that you want to show not only your processes, but ALL of them. 注意,-ax param意味着,您不仅要显示您的进程,还要显示所有进程。 Maybe, in the table you see UID+PID instead of pid. 也许在表中,您看到的是UID + PID而不是pid。 Use 采用

ps -ef | pg

instead. 代替。 What do you see there? 您在那看到什么?

To see every process on the system using standard syntax: 要使用标准语法查看系统上的每个进程:

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

To see every process on the system using BSD syntax: 要使用BSD语法查看系统上的每个进程:

ps ax
ps axu 

Are you sure you need BSD syntax? 您确定需要BSD语法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM