You are on page 1of 4

10/10/2020 getppid() and getpid() in Linux - GeeksforGeeks

Time to unleash full potential! Experience unlimited, unhindered learning with GeeksforGeeks
Premium! Only 1000 seats available, Grab Yours Now!

getppid() and getpid() in Linux


Last Updated: 26-09-2017

Both getppid() and getpid() are inbuilt functions de ned 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
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/getppid-getpid-linux/ 1/4
10/10/2020 getppid() and getpid() in Linux - GeeksforGeeks

NOTE: At some instance of time, it is not necessary that child process will execute rst
or parent process will be rst 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 lenames.
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

We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/getppid-getpid-linux/ 2/4
10/10/2020 getppid() and getpid() in Linux - GeeksforGeeks

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 nd anything incorrect, or you want to share more information
about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with
the DSA Self Paced Course at a student-friendly price and become industry ready.

Recommended Posts:

Article Tags : C Linux-Unix CPP-Library system-programming

Practice Tags : C


Be the First to upvote.

1.5
To-do Done Based on 2 vote(s)

Improve Article
We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/getppid-getpid-linux/ 3/4
10/10/2020 getppid() and getpid() in Linux - GeeksforGeeks

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


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

5th Floor, A-118,


 Sector-136, Noida, Uttar Pradesh - 201305
 feedback@geeksforgeeks.org

Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

Practice Contribute
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks , Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/getppid-getpid-linux/ 4/4

You might also like