gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:40:20 25 4
gpt4 key购买 nike

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。有人可以帮助我理解为什么这些值不同吗?

最佳答案

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

ps -ef | pg

相反。你在那里看到了什么?

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

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

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

ps ax
ps axu

您确定需要 BSD 语法吗?

关于c - fork() 返回值与进程的 ps 命令输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com