understanding fork() ???

This is a discussion on understanding fork() ??? within the C Programming forums, part of the General Programming Boards category; Hi all , this may be a weird question , but please I need your help !!! how this C ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    understanding fork() ???

    Hi all , this may be a weird question , but please I need your help !!!

    how this C program work ?

    Code:
    int main() {
            int p1 , p2;
            p1 = fork();
            p2 = fork();
            printf("end\n");
    }

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    If you instrument the code to see what is going on...

    Code:
    output:
    Starting process:  7033
    end of process: 7033
    I'm a brand new process:  7034   My daddy was : 7033
    I'm a brand new process:  7035   My daddy was : 7033
    end of process: 7035
    end of process: 7034
    I'm a brand new process:  7036   My daddy was : 7034
    end of process: 7036
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    void whoami(pid_t *);  /* show the process id's of the parent and the current process */
    
    int main(int argc , char *argv[]) {            
            int p1 , p2;    
            pid_t pids[10]={0};        
            pids[0]=getpid();  /* pid of parent process */  
            printf("Starting process: %5d\n",pids[0]);
            whoami(pids);
            p1 = fork();    
            whoami(pids);
            p2 = fork();    
            whoami(pids);
            printf("end of process:%5d\n",getpid());
            return 0;
    }                 
    void whoami(pid_t * pids){  /* this looks to see if the process is a new one */
            pid_t this_process;
            int found=0;
            pid_t *pidptr;
            this_process=getpid();
            for(found=0,this_process=getpid(),pidptr=pids;*pidptr && !found;pidptr++)
            	 if(this_process==*pidptr) found=1;
            if(!found){
                     *pidptr=this_process;
                     printf("I'm a brand new process: %5d ",this_process);
                     printf("  My daddy was :%5d\n", getppid());
            }
          
    }

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    thank you very much , that was a great help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 PM
  2. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 03:16 AM
  3. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 02:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21