Thread: getppid problems

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    7

    Question getppid problems

    Hi everyone, programming newbie here =D

    I'm in need of a helping hand from you experts; the topic is the "getppid" function

    first, this is the simple code i'm running:
    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <sys/types.h>
    # include <unistd.h>
    
    int main ()  {
    
    pid_t child;
    
    printf ("MAIN program process' pid: %d.\n", (int) getpid () );
    child = fork () ;
    
    if (child==-1) {
       printf ("Error");
       exit (0);
       }
    else {
       if (child!==0) {
          printf ("PARENT process running, pid is: %d.\n", (int) getpid() );
          printf ("PARENT process running, son's pid is: %d.\n", (int) child );
          }
       else {
          printf ("CHILD process running, pid is: %d.\n", (int) getpid() );
          printf ("CHILD process running, parent's pid is: %d.\n", (int) getppid() );
          }
       }
    
    return (0);
    
    }
    1. I first wrote this on Windows, and when I tried to compile it a linker error was displayed, claiming "getppid" was an undeclared function. I think that happened because of the way processes work on Windows (no precise hierarchy), am I right or I forgot something?


    2. I tried to recompile this on Ubuntu (run on a virtual machine). Here's one of the possibile outputs:
    MAIN program process' pid: 1329.
    PARENT process running, pid is: 1329.
    PARENT process running, pid is: 1330.
    type <Return> to continue
    CHILD process running, pid is: 1330.
    CHILD process running, parent's pid is: 1.
    Why is the last result "1" instead of "1329"? Isn't "getppid" supposed to return the pid of the process' parent?
    Is that because of the virtual machine (which is still running with Windows)? I mean, if I had run it on a "pure" UNIX envinronment, would the result have been correct? Or, again, did I wrote something the wrong way?

    Thanks in advance to everyone who's going to answer =) !

    (As you surely noticed, English's not my first language. If everything sounds unclear, point it ut and i'll try to explain myself better!)

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think I can answer part 2. Your parent process forks the child, prints some output and returns 0, exiting the program. Since the fork and execution of the child process takes a little time, you end up leaving your child process orphaned. By the time it gets around to printing it's parent process, it has been reaped by init (who takes in all orphans), and init's pid is always 1. Look into the wait() or waitpid() functions to make sure the parent doesn't finish before it's children do their thing.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by qarion92 View Post
    Hi everyone, programming newbie here =D
    1. I first wrote this on Windows, and when I tried to compile it a linker error was displayed, claiming "getppid" was an undeclared function. I think that happened because of the way processes work on Windows (no precise hierarchy), am I right or I forgot something?
    On windows platforms there won't be a getppid() call for the very reason you cited... Windows is not Linux.

    On windows platforms you have 2 choices...
    Code:
    #include "process.h"
    
    int x = _getpid();  // return only current process id.
    Be aware this is NOT standard C-99 and may not work on all compilers... it is known to work on PellesC.

    --or--

    Code:
    #include <windows.h>
    
    DWORD x = GetCurrentProcessID();
    I suspect the first simply calls the second.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    7
    How quick you were =) !

    Suggestions tested and working, thanks a lot guys.
    (Glad to know I can still push the moment to fully convert to UNIX later in the future...)

  5. #5
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    If you're getting a pid of 1, then you are reading the pid of the init system process. Init is the parent to every (main) parent process in the system. (In Linux)

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    7
    Yes and now I guess that it has to be cause of the ending of the parent process before the son's one. Thanks =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  2. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  3. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  4. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 08:07 PM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:23 AM

Tags for this Thread