0

Im having some trouble in forking. All i want to do is to create a child process and under the child process i need to create a grand child..here's my code

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main(int argc, char *argcv[])
{
int val;
val =fork();

if(val==0){
           printf("Child process running\n");
           printf("Child:%d PID:%d\n",val,getpid());

           val=fork();
           if(val==0){
               printf("Grand child created\n");
               printf("Grand child:%d PID:%d\n",val,getpid());


                           }
                }
else if(val>0){
                    printf("Parent process running\n");
                    printf("Parent:%d PID:%d\n",val,getpid()); 
                    }
else{
       printf("Error\n");
        }
return 0;
}

Once i run the program the grand child and the child process has a different PID. what am i doing wrong? thanks guy

Edited by mike_2000_17: Fixed formatting

2
Contributors
2
Replies
6
Views
8 Years
Discussion Span
Last Post by herms14
0

I think they should have different pid's

waht do you mean by different pid's/ should i declare a new fork? what shoud i do to create a new grand child

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.