Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix Fork()的痕迹有问题_Unix_Process_Fork_Pid - Fatal编程技术网

Unix 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(

我有一个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("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);