【问题标题】:probleme with the trace of Fork()Fork() 的跟踪问题
【发布时间】:2012-02-20 12:09:41
【问题描述】:

我有这个 fork() 的例子,我需要做一个跟踪!

#include <unistd.h>

int main(void) {
    int i;
    for (i=0; i<3; i++)
        if (fork()) 
            wait(NULL);
    return 0;
}

我的跟踪解决方案是,出了点问题,我不知道如何进行跟踪:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {

    int i,pid;

    for(i=0;i<3;i++) {

        pid=fork();

        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);
    }
}

这是我在终端中得到的。我可以清楚地看到它是不正确的,但我不知道如何解决这个问题:

PID After the fork child === 134513568  & father === 134513632 (i = 0) 

After the wait  child=== 134513568  & father=== 134513632 (i = 0)|||
PID After the fork child === 134513568  & father === 134513632 (i = 1) 


After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||
After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||
After the wait  child=== 134513568  & father=== 134513632 (i = 1)|||
PID After the fork child === 134513568  & father === 134513632 (i = 2) 

After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||
After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||
After the wait  child=== 134513568  & father=== 134513632 (i = 0)|||
PID After the fork child === 134513568  & father === 134513632 (i = 1) 

After the wait  child=== 134513568  & father=== 134513632 (i = 1)|||
PID After the fork child === 134513568  & father === 134513632 (i = 2) 

After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||
After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||
After the wait  child=== 134513568  & father=== 134513632 (i = 1)|||
PID After the fork child === 134513568  & father === 134513632 (i = 2) 

After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||
After the wait  child=== 134513568  & father=== 134513632 (i = 2)|||

谢谢。

【问题讨论】:

    标签: unix process fork pid


    【解决方案1】:

    您对printf() 的参数是getpidgetppid——当您似乎打算打印调用这些函数的结果时,您正在传递这些函数的地址。打电话给他们...

    printf("PID After the fork child === %d  & father === %d (i = %d) \n\n",(int)getpid(),(int)getppid(),i);
    

    【讨论】:

      【解决方案2】:

      我认为你错过了大括号和方括号:

         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 ("After the wait ...) 将在父进程和子进程中调用。

      【讨论】:

      • 没有正确的大括号,但我不知道为什么我在所有迭代(从 i=0 到 i
      • @cleo:正如@mah 指出的那样,您忘记了在getpidgetppid 之后放置()。如果开启高警告级别,编译器会提示:cast from '__pid_t (*)()throw ()' to 'int' loses precision
      猜你喜欢
      • 1970-01-01
      • 2018-07-25
      • 2020-03-20
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多