Home » C Program to Get Process ID and Parent Process ID

C Program to Get Process ID and Parent Process ID

Learn about C Program to Get Process ID and Parent Process ID in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Each ongoing job in Linux is referred to as a “Process.” Kernel assigns a unique ID to each process executing in memory, known as the Process ID or PID. PID separates one running process from another.

The getpid() function is used to obtain the process id of the currently running process. There is a parent process for every process. getppid returns the process ID of the parent process (). The functions getpid() and getppid() are defined in <unistd.h>. If we don’t include <unistd.h>, we get a warning. To resolve this warning, include <unistd.h>.

C Program to Get Process ID and Parent Process ID

#include<stdio.h>
#include<unistd.h>
int main()
{

  printf("Process ID: %d\n", getpid() );//function is called to get process id of process
  printf("Parent Process ID: %d\n", getppid());//to get process id of parent process

  return 0;
}

output:

Process ID:23118

Parent Process ID:19713

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program to Print Hello World Without Using Semicolon(Command-line Arguments)
Using Macros C Program to Print Hello World without Using a Semicolon