×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

getpid() and getppid() functions in C Linux

getpid() and getppid() functions in C Linux: In this tutorial, we will learn about the two important functions which are used to get the process ids: getpid() and getppid() functions in C programming with Linux with the help of examples. By IncludeHelp Last updated : June 06, 2023

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

getpid() and getppid() functions in C Linux

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. The getpid() function returns the process id of the calling function, this function is defined unistd.h header file.

Syntax:

pid_t getpid();

2) getppid() function in C

The getppid() function returns the process id of the parent function. It is also defined in unistd.h header file.

Syntax:

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()

Consider the below-given C program demonstrating the example of getpid() and getppid() functions. In this program, we are declaring two variables to store the calling and parent functions process id, then we are calling these functions to get those values, and printing them.

// C program to demonstrate the example of
// getpid() and getppid() functions

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

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

Output

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

RUN 2:
The process id: 60
The process id of parent function: 59

RUN 3:
The process id: 972
The process id of parent function: 971

Explanation: 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

Note:

  • Each time when you run the program, the process id and parent process ids may be different.
  • The output may be different on different systems.

Different between getppid() and getpid() functions

Both of the functions are defined in unistd.h header file and returns the same type of data (pid_t). But they have differences. The main difference between getpid() and getppid() functions is that getpid() returns the process id of the calling function while getppid() returns the parent process id i.e., the process id of the parent function.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.