Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Process/childrens (http://www.go4expert.com/forums/process-childrens-t14243/)

Yannick 28Sep2008 21:44

Process/childrens
 
Hi, with a "Father" process i want to create 3 children process, i tryed to use that code but i think that is wrong cause i see that in my code "Father create" the children1 process next children1 create a children2 process, they are all not from the "Father" process...Anyone can help me?

Code:

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

int main(){
       
        int filho1,filho2,filho3;

        filho1 = fork();
        if (filho1 == 0){
                printf("[%d] Sou o filho1!\n", getpid());
                printf("[%d] O meu pai : %d\n", getpid(), getppid());
        }

        filho2 = fork();
        if (filho2 == 0){
                printf("[%d] Sou o filho2!\n", getpid());
                printf("[%d] O meu pai : %d\n", getpid(), getppid());
        }

        filho3 = fork();
        if (filho3 == 0){
                printf("[%d] Sou o filho3!\n", getpid());
                printf("[%d] O meu pai : %d\n", getpid(), getppid());
        }

        return 0;
}


xpi0t0s 30Sep2008 06:07

Re: Process/childrens
 
What happens if, say, filho1 != 0?

teacher 13Apr2011 08:41

Re: Process/childrens
 
sorry for late reply but i think many guys faces this problem..

so just put exit(EXIT_SUCCESS) in each if block at the end.

so each process will get terminated after doing its job.

do not forget to include header file <stdlib.h>

like for eg
Code: cpp

if (filho1 == 0){
        printf("[%d] Sou o filho1!\n", getpid());
        printf("[%d] O meu pai : %d\n", getpid(), getppid());
exit(EXIT_SUCCESS)
}



All times are GMT +5.5. The time now is 19:19.