Process/childrens

Discussion in 'C' started by Yannick, Sep 28, 2008.

  1. Yannick

    Yannick New Member

    Joined:
    Sep 28, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What happens if, say, filho1 != 0?
     
  3. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    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:
    if (filho1 == 0){
    		printf("[%d] Sou o filho1!\n", getpid());
    		printf("[%d] O meu pai é: %d\n", getpid(), getppid());
    exit(EXIT_SUCCESS);	
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice