| by Arround The Web | No comments

Getpid() Function in C Language

When we open a process or a program opens a thread, the system reserves the necessary resources for its execution and assigns with it a unique identifier. A process identifier consists of an integer that the system assigns in the same way that it assigns an identifier to a file.

Each process in the system has a unique ID which we can see with various commands in the console such as “top”. This command displays a list of the current processes that run on the system with the PID, user, and some basic data about CPU and memory usage for each process.

In the C programming language, the process and thread identifiers are used to refer to them in the system call functions that manage the processes or threads. So, the PID is an information that we must have to use these functions and manage or work with the processes and threads.

In this Linuxhint article, you’ll learn how to use the getpid() function to retrieve the identifier of the process that calls the function. We’ll look at a theoretical part that shows the syntax of the function and explain how it works. We’ll also show you the method that this function calls, as well as its output argument and the data type it uses to store the identifiers. We’ll then apply what we learned by implementing getpid() in a practical example. This includes code snippets and images that show how to use this function and how to insert the headers and declare the variables that are needed to use the function.

Syntax of the Getpid() Function:

pid_t getpid (void);

Description of the Getpid() Function in C Language

The getpid() function gets the identifier of the process from which it’s called. This function is sent empty and returns an unsigned integer containing the process identifier.

Getpid() is one of a set of functions that is defined in the “unistd.h” header to get the identifiers of processes and threads. To use them, we need to add this header to the code as follows:

#include <unistd.h>

The “pid_t” type that is used by this function is a “typedef” that is defined in the “types.h” header in the “sys” folder. To use getpid(), we must also include this file as shown in the following:

#include <sys/types.h>

How to Get the PID of a Process with the Getpid() Function

In this example, we create a simple application that calls the getpid() function, displays the returned PID on the command console, and then goes into an infinite loop. Then, we search for the process and monitor it with a second command console.

To do this, we take an empty file with the “.c” extension and add the “stdio.h”, “unistd.h”, and “types.h” headers to it. Then, we open a main() function in which we declare a variable of the “pid_t” type and insert the name ID as identifier.

After inserting the headers, creating the main() function, and declaring the variable that we use to store the identifier, we call the getpid() function and pass the ID variable as the output argument. We then print the process identifier ID to the screen using the printf() function in the “%i” output format. To keep the process active and to be able to monitor it, we put the program in an infinite loop which you can exit with “Ctrl+c”. You can find the complete code for this example in the following image:

 

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

void main ()
{
pid_t id;
id = getpid();
printf("The PID is: %i\n", id );
while (1){

        sleep(5);
}

}

Next, let’s look at the compilation and execution of this code. As you can see, the getpid() function gets the identifier of the process that called it, and the printf() function prints its result to the screen.

In the following image, the upper console shows the PID of the process that is returned by the getpid() function, while the lower console shows the running process which you can see with the ~$ top -p (pid) command:

Conclusion

In this Linuxhint article, we showed you how to use the getpid() function to get the PID of a process. We looked at the syntax of this function and a theoretical part that explains how it works, the calling method, the type of output argument, and the data that is used to store the identifier.

Then, we developed a practical example in which we clearly explained how to insert the headers that this function uses, declare the data type to store the result, and obtain the process identifier. Getting the identifier of a process or thread is a necessary step to work with them since the functions that are designed to manage them use the PID in their input arguments.

Share Button

Source: linuxhint.com

Leave a Reply