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

I think they should have different pid's

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.