getpid() and getppid() function in C
In this article, we will discuss the getpid() and getppid() functions in C with their syntax and examples.
What is the getpid() function?
The getpid() function in C is part of the unistd.h header and is used to retrieve the Process ID (PID) of the currently running process. PID is a unique identifier assigned to each process by the operating system, and it is used to distinguish and manage processes on the system.
Syntax:
It has the following syntax:
#include <unistd.h>
pid_t getpid(void);
Return Type:
The function returns a pid_t data type, which is an integer type representing the process ID. On most systems, pid_t is defined as int, but it could be another integer type on some platforms.
Function Description:
The getpid() function doesn't take any arguments; it is a parameterless function. When it is called, it returns the PID of the currently running process.
Process ID (PID):
The PID is an unsigned integer value that uniquely identifies a process in the operating system. It is assigned by the operating system when a process is created and remains constant throughout the process's lifetime. When a process terminates, its PID can be reused for a new process, but during a process's existence, its PID stays unchanged.
Use of getpid():
The getpid() function is commonly used in system-level programming to obtain the PID of the current process. Some common use cases include:
Identifying the process itself: A process may need to identify itself for logging or other purposes.
Creating child processes: When creating child processes, the PID is used to differentiate between the parent and child processes.
Inter-process communication: In some inter-process communication (IPC) mechanisms, PIDs are used as identifiers for communication channels or message routing.
Example:
Let's take a program to illustrate the getpid() function in C.
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t pid = getpid();
printf("Process ID: %d\n", pid);
return 0;
}
Output:
![getpid() and getppid() function in C](https://static.tutorialandexample.com/c/getpid-and-getppid-function-in-c1.png)
Explanation:
In this example, we include the necessary header files: unistd.h for the getpid() function and stdio.h for the printf() function that is used to print the process ID.
pid_t is a data type that represents a process ID. It's defined in the unistd.h header file.
Inside the main() function, we declare a variable pid of type pid_t to store the process ID. After that, we call the getpid() function, which doesn't take any arguments and returns the PID (process ID) of the currently running process.
The retrieved PID is stored in the variable pid. Finally, we print the process ID using the printf() function.
When you run this code, it will display the process ID of the currently running process. You'll get a different PID each time you run this program since each execution creates a new process.
What is the getppid() function?
In C, the getppid() function is used to retrieve the parent process ID (PPID) of the current process. It is part of the Unix and Unix-like operating systems, and it's available in the unistd.h header.
Parent Process ID (PPID):
Every process in an operating system has a unique process ID (PID) assigned to it. The PPID is the PID of the parent process that created the current process. When a new process is created, the operating system assigns it a new PID and sets the PPID to the PID of the process that spawned it. This way, processes form a tree-like structure, where the original process (usually the shell or terminal) acts as the root of the process tree, and all subsequent processes are its descendants.
Syntax:
It has the following syntax:
#include <unistd.h>
pid_t getppid(void);
Example:
Let's take a program to illustrate the getppid() function in C.
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t ppid = getppid();
printf("Parent Process ID: %d\n", ppid);
return 0;
}
Output:
![getpid() and getppid() function in C](https://static.tutorialandexample.com/c/getpid-and-getppid-function-in-c2.png)
Explanation:
Header Files:
The code includes two header files: unistd.h and stdio.h. The unistd.h is required to access system calls and functions related to process IDs, and stdio.h is necessary for standard input/output functions like printf().
Main function:
The code contains the main() function, which is the entry point of the program. It is where the execution of the program starts.
Variable Declaration:
A variable ppid of type pid_t (which is typically an integer type) is declared to store the parent process ID. The getppid() function is called to retrieve the parent process ID, and the result is assigned to the ppid variable.
Parent Process ID Retrieval:
The getppid() function is used to obtain the parent process ID (PPID) of the current process. This function doesn't take any arguments and returns the PPID as a value of type pid_t.
Printing the Parent Process ID:
The retrieved PPID stored in the ppid variable is printed to the console using the printf() function. Print an integer value, and ppid is passed as an argument to be printed.
The main() function returns an integer value of 0, indicating successful execution. When a C program completes execution, it returns an exit status to the calling environment, which is typically an operating system. A return value of 0 usually indicates that the program executed successfully, while a non-zero value indicates an error or some specific condition.