getpid and getppid

This is a discussion on getpid and getppid within the Linux Programming forums, part of the Platform Specific Boards category; Hi iam new to c programming and i would like help in understanding this code: Code: #include <stdio.h> #include <unistd.h> ...

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    getpid and getppid

    Hi iam new to c programming and i would like help in understanding this code:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    int main(void)
    {
      if(fork())
        fork();
    
      printf("I am pid %d!\n", getpid());
    
      return 0;
    }
    What i basically dont understand is why is printing 3 pid values.

    I am pid 16095!
    I am pid 16096!
    I am pid 16094!

    Can some one help me out why it it prints this 3 values and not one? and what this values represent?

    thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Well, I don't know much about fork() or getpid(), but I suspect the three pids represent the original process and the two calls to fork the process.
    Sent from my iPadŽ

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    fork() returns 0 to the child (so it doesn't call fork again), and a non-zero pid to the parent (so it does call fork again).
    So that's two fork calls, and the original program, all printing a pid.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    This thread should go to Linux programming section!

    After you fork(), you have two DIFFERENT processes (not threads). So examining your code
    Code:
    int main(void)
    {
     /* you have on process */
      if(fork())
        fork();   
    /* you have three processes */
      printf("I am pid %d!\n", getpid());
    
      return 0;
    }
    or
    Code:
                process1
                   |
                 fork
                   |
           --------+---------
           |                |
           v                v
        process1         process2
        (parent)         (child)
        printf()         printf()
        return 0;        return 0;
    The above is a normal fork(),in your program you fork() two times! So, you have one parent and two children. The numbers are PIDs of parent and children processes.

    I suggest you to use switch or else-if statements to distingusish the processes.
    Last edited by fnoyan; 09-06-2006 at 12:30 AM.

  5. #5
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Forking a child off?

    This is an output of how my program should look?

    ./prog2a: I'm a process.
    ./prog2a: My PID is 3888 and my parent's PID is 3881
    ./prog2a: Forking a child off.
    Child: I have been created!
    Child: My PID is 3889 and my parents's PID is 3888
    ./prog2a: I created a child and its PID is 3889

    what should i do? first have one PID which i assume it will be 3888 and then prints its parent 3881. How do i do a forking off?
    Last edited by wise_ron; 09-06-2006 at 12:32 AM.

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Well, what does "forking off" mean?

  7. #7
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    meaning of fork

    according to man fork

    The fork() function shall create a new process. The new process (child process) shall be an exact copy of the calling process (parent process)

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Quote Originally Posted by fnoyan
    Well, what does "forking off" mean?
    I think he's telling you to "fork off"...

    Anyway, as I said, I don't know much about fork, so I'm just plain guessing here. Based off of the order of your output, I'd say, your program ran down something like this:

    1. You get to the conditional in the main process, it calls fork(), for breaks the process into the new process that now runs from the fork point.
    2. The conditional in that new process is now void, there for the second fork isn't called. It goes down to that processes printf()... it prints the pid.
    3. That process ends, fork resolves (successfully), the condition returns true, it calls the second fork. That new process goes down to it's printf(), prints it's pid. Resolves.
    4. Now your back in your original process, it gets to it's printf(), prints it's pid. End program.

    Now... as I said, I know nothing about fork. If I had to guess, I'd say I'm either 100% correct here or 100% wrong here. So don't take my word for it, take someone elses opinion on my post for it.
    Last edited by SlyMaelstrom; 09-06-2006 at 12:46 AM.
    Sent from my iPadŽ

  9. #9
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Forking off?

    Forking a child off i believe is adding a value to the PID because it increases.

    this is how the output of my program should look like

    Code:
    ./prog2a: I'm a process.
    ./prog2a: My PID is 3888 and my parent's PID is 3881
    ./prog2a: Forking a child off.
    Child: I have been created!
    Child: My PID is 3889 and my parents's PID is 3888
    ./prog2a: I created a child and its PID is 3889

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Quote Originally Posted by wise_ron
    Forking a child off
    Ok, now that kind of talk is just uncalled for.

    Anyway, I editted my last post to actually... say something useful, perhaps.
    Sent from my iPadŽ

  11. #11
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    As I know there is no standart for the new PID given to the child. I think it depends on the implementation of the fork system call.

    I recommend you to read section 4 of "Advanced Linux Programming". Here is the link
    http://www.advancedlinuxprogramming....04-threads.pdf

    Your program should like like this
    Code:
    pid_t pid, ppid;
    pid=fork();
    if (pid==0)
    {
    /* here is child, you can print the PID of parent by examining the value returned by getppid()*/
    } else if (pid>0)
    {
    /* At this point the valule of "pid" is PID of child. Directly print it. Also getpid() gives the same value as given by getppid() which has just been used in first part of else-if */
    } else {
    /* pid<0, there is something wrong. use perror to report the error */
    }

  12. #12
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    hey thanks

    thank you very much

  13. #13
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    hi everyone

    thanks once again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 01:14 PM
  2. Process/childrens
    By Yannick in forum C Programming
    Replies: 4
    Last Post: 10-02-2008, 01:02 PM
  3. Question related to getpid and getppid
    By g_p in forum C Programming
    Replies: 4
    Last Post: 12-18-2006, 10:35 AM

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