cleo
cleo

Reputation: 165

probleme with the trace of Fork()

I have this example of fork() I need to do a trace!

#include <unistd.h>

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

My solution for to do the trace is, something is wrong, I don't know how to do the trace:

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

This is what i get in the Terminal. I can see clearly that it is not correct, but i don't know how to resolve this problem:

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

Thanks.

Upvotes: 0

Views: 168

Answers (2)

Yun Huang
Yun Huang

Reputation: 4386

I think you missed the braces and brackets:

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

In your codes, the second printf ("After the wait ...) will be called in both parent process and child process.

Upvotes: 1

mah
mah

Reputation: 39807

Your argument to printf() is getpid and getppid -- you're passing in the address of these functions, when it appears you intend to print the results of calling the functions. Call them...

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

Upvotes: 1

Related Questions