Skip to content
Related Articles

Related Articles

Improve Article
getppid() and getpid() in Linux
  • Difficulty Level : Easy
  • Last Updated : 26 Sep, 2017

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.




    // 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;
    }

    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.




    // 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;
    }

    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.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :