Thread: Process/childrens

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    34

    Process/childrens

    Hi, i want to create 3children processes from one "Father process" but when i test my code i see on the Shell that the "Father" create children1, children1 create children2 and children2 create children3, and i want is to create all the 3children from "Father" process, anyone can help?

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Each child needs to stop when it reaches the end of it's if ( pid == 0 ) block.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    How i can do a process that execute the shell command "ps u" every 2secs ? Its with "exec" functions? Anyone can show me how to do that?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    if ( fork() == 0 ) execl()
    Plus some manual reading, and maybe a board search, since this has been done before.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Perhaps even post on the linux board next time. fork() isn't supported on windows.

Popular pages Recent additions subscribe to a feed