我正在尝试打印wait()函数的当前值。我从输出中想到的是,当子进程正在运行时,子上下文中wait()的当前值为-1,一旦它完成并返回,则在父上下文中wai()的值等于它的子进程的pid。根据我的理解,这个暗示是正确的吗?
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int statloc;
int stat;
printf("nthis process id is %d", getpid());
int pid;
pid =fork();
stat=wait(&statloc);
printf("n Value of stat is %d",stat);
getchar();
}
输出:
this process id is 10740
Value of stat is -1k // k is entered as input due to getchar
this process id is 10740
Value of stat is 10741j // j is entered as input due to getchar
我认为你的理解是错误的。
等待的用途是等待,直到任何一个子进程退出。如果子进程退出,它将返回退出的子进程的进程id,并将进程的退出状态存储在等待函数的传递参数中。
我们可以使用WEXITSTATUS()宏来获取退出状态。
在该宏中,我们必须传递传递给等待系统调用的参数。如果我们通过,它将返回进程的实际退出状态。
要了解更多关于等待的信息,请阅读下面的链接。
http://man7.org/linux/man-pages/man2/wait.2.html