getppid() and getpid() in Linux

Both getppid() and getpid() are inbuilt functions defined in unistd.h library.

  1. getppid() : returns the process ID of the parent of the calling process. If the calling process was created by the fork() function and the parent process still exists at the time of the getppid function call, this function returns the process ID of the parent process. Otherwise, this function returns a value of 1 which is the process id for init process.
    Syntax:

    pid_t getppid(void);
    

    Return type: getppid() returns the process ID of the parent of the current process. It never throws any error therefore is always successful.



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ Code to demonstrate getppid()
    #include <iostream>
    #include <unistd.h>
    using namespace std;
      
    // Driver Code
    int main()
    {
        int pid;
        pid = fork();
        if (pid == 0)
        {
            cout << "\nParent Process id : " 
                 << getpid() << endl;
            cout << "\nChild Process with parent id : " 
                 << getppid() << endl;
        }
        return 0;
    }

    chevron_right

    
    

    Output(Will be different on different systems):

    Parent Process id of current process : 3849
    Child Process with parent id : 3851
    

    NOTE: At some instance of time, it is not necessary that child process will execute first or parent process will be first allotted CPU, any process may get CPU assigned, at some quantum time. Moreover process id may differ during different executions.

  2. getpid() : returns the process ID of the calling process. This is often used by routines that generate unique temporary filenames.
    Syntax:

    pid_t getpid(void);
    

    Return type: getpid() returns the process ID of the current process. It never throws any error therefore is always successful.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ Code to demonstrate getpid()
    #include <iostream>
    #include <unistd.h>
    using namespace std;
      
    // Driver Code
    int main()
    {
        int pid = fork();
        if (pid == 0)
            cout << "\nCurrent process id of Process : " 
                 << getpid() << endl;
        return 0;
    }

    chevron_right

    
    

    Output (Will be different on different systems):

    Current process id of Process : 4195
    

This article is contributed by Pushpanjali Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.