Unix Fork()的痕迹有问题
我有一个fork()的例子,我需要做一个跟踪Unix Fork()的痕迹有问题,unix,process,fork,pid,Unix,Process,Fork,Pid,我有一个fork()的例子,我需要做一个跟踪 #include <unistd.h> int main(void) { int i; for (i=0; i<3; i++) if (fork()) wait(NULL); return 0; } 谢谢。我想你错过了括号和括号: if(pid>0) ///if is father (true) { printf(
#include <unistd.h>
int main(void) {
int i;
for (i=0; i<3; i++)
if (fork())
wait(NULL);
return 0;
}
谢谢。我想你错过了括号和括号:
if(pid>0) ///if is father (true)
{
printf("PID After the fork child === %d & father === %d (i = %d) \n\n",(int)getpid(),(int)getppid(),i);
wait(NULL);
printf(" After the wait child=== %d & father=== %d (i = %d)|||\n",(int)getpid(),(int)getppid(),i);
}
在您的代码中,将在父进程和子进程中调用第二个
printf(“等待之后…”
。您对printf()的参数
是getpid
和getppid
——您正在传入这些函数的地址,当它显示您想要打印调用函数的结果时。调用它们
printf("PID After the fork child === %d & father === %d (i = %d) \n\n",(int)getpid(),(int)getppid(),i);
不,大括号是正确的,但我不知道为什么在所有迭代中(从i=0到i@cleo:正如@mah所指出的,您忘记了在
getpid
和getppid
之后的put()
。如果启用高警告级别,编译器将提示:从'\u pid\t(*)强制转换()'到'int'失去精度
if(pid>0) ///if is father (true)
{
printf("PID After the fork child === %d & father === %d (i = %d) \n\n",(int)getpid(),(int)getppid(),i);
wait(NULL);
printf(" After the wait child=== %d & father=== %d (i = %d)|||\n",(int)getpid(),(int)getppid(),i);
}
printf("PID After the fork child === %d & father === %d (i = %d) \n\n",(int)getpid(),(int)getppid(),i);