проблема с трассировкой Fork () - PullRequest
0 голосов
/ 20 февраля 2012

У меня есть этот пример 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)|||

Спасибо.

Ответы [ 2 ]

1 голос
/ 20 февраля 2012

Ваш аргумент printf() равен getpid и getppid - вы передаете адрес этих функций, когда появляется сообщение о том, что вы намереваетесь напечатать результаты вызова функций. Позвони им ...

printf("PID After the fork child === %d  & father === %d (i = %d) \n\n",(int)getpid(),(int)getppid(),i);
1 голос
/ 20 февраля 2012

Я думаю, что вы пропустили скобки и скобки:

   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 ...) будет вызываться как в родительском, так и в дочернем процессах.

...