Home » C programming language

getpid() and getppid() functions in C Linux

getpid() and getppid() functions in Linux with example - in this tutorial, we are going to learn that how to get the calling process id and parent process Id in C programming with Linux?
Submitted by IncludeHelp, on June 01, 2018

If we are working on the processes, signals related programming using C language in Linux; we require process ids which can be created through the code by using some functions. Therefore, we need some of the functions, data types which are able to retrieve the process ids.

First of all, I would recommend to read pid_t type in C

Functions to get process ids in C

There are two functions which are used to get the process ids, the functions are:

  1. getpid()
  2. getppid()

1) getpid() function in C

When any process is created, it has a unique id which is called its process id. This function returns the process id of the calling function.


    pid_t getpid();

2) getppid() function in C

This function returns the process id of the parent function.


    pid_t getppid();

Note: pid_t is the type of process id, which is an unsigned integer type of data type.

C program to demonstrate example of getpid() and getppid()

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
	//variable to store calling function's process id
	pid_t process_id;
	//variable to store parent function's process id
	pid_t p_process_id;

	//getpid() - will return process id of calling function
	process_id = getpid();
	//getppid() - will return process id of parent function
	p_process_id = getppid();

	//printing the process ids
	printf("The process id: %d\n",process_id);
	printf("The process id of parent function: %d\n",p_process_id);

	return 0;


    The process id: 31120
    The process id of parent function: 31119

Header files

  1. stdio.h - it is used for printf() function
  2. sys/types.h - it is used for pid_t type, that is the data type of the variables which are using to store the process ids.
  3. unistd.h - it is used for getpid() and getppid() functions


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.