C Program to Get Process ID and Parent Process ID

C Program to get Process ID and Parent Process ID | In Linux, each running task is known as “Process”. Kernal assigns each process running in memory a unique ID called Process ID or PID. PID distinguishes one process from another running process.

The getpid() used to get the process id of the current process. Every process has a parent process. The process ID of the parent process can find using getppid(). The getpid() and getppid() are declared under <unistd.h>. If we don’t include <unistd.h> then a warning occurs. To fix this warning <unistd.h> must be included.

Get Process ID and Parent Process ID

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

  printf("Process ID: %d\n", getpid() );
  printf("Parent Process ID: %d\n", getppid() );

  return 0;
}

Output:-

Process ID:23118
Parent Process ID:19713

Whenever we run the program a new process is created, Hence output (PID) will be different each time.

From one process we can create another process. To do this fork() library function will be used. fork() split one process into two processes:- parent process, and child process.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also Learn

Leave a Comment

Your email address will not be published. Required fields are marked *