How to get the process ID in C language? (code example)

青灯夜游
Release: 2019-03-06 13:56:32
Original
7861 people have browsed it

We can use the getppid() and getpid() functions to obtain the process ID. They are both built-in functions defined in the unistd.h library and can return the process ID. The following article will give you a brief introduction to the getppid() and getpid() functions, and learn how to obtain the process ID using C language in the Linux system. I hope it will be helpful to you.

How to get the process ID in C language? (code example)

getpid() function

When any process is created, it has a unique id, called is the process ID. The getpid() function returns the process ID of the calling process. [Video tutorial recommendation: C Language Tutorial]

Basic syntax:

pid_t getpid(void);
Copy after login

Return type: The getpid() function returns the process ID of the current process; it never throws no errors occur and therefore always succeeds.

getppid() function

getppid() function: Returns the process ID of the parent process of the calling process.

Description: If the calling process was created by the fork() function, and the parent process still exists when the getppid() function is called, this function returns the process ID of the parent process. Otherwise, this function returns the value 1, which is the process ID of the init process.

Basic syntax:

pid_t getppid(void);
Copy after login

Return type: The getppid() function returns the process ID of the parent process of the current process; it never throws any error, so it always succeeds.

Note: pid_t is the type of process ID, which is an unsigned integer data type.

Code example (C language):

Let’s take a look at how to obtain the calling process ID and parent process ID using C language in the Linux system .

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
//存储调用函数进程ID的变量
pid_t process_id;
//存储父函数进程ID的变量
pid_t p_process_id;
//getpid() - 将返回调用函数的进程ID
process_id = getpid();
//getppid() - 将返回父函数的进程ID
p_process_id = getppid();
//输出进程ID
printf("调用函数的进程ID:%d\n",process_id);
printf("父函数的进程ID:%d\n",p_process_id);
return 0;
}
Copy after login

Output:

调用函数的进程ID:31120
父函数的进程ID:31119
Copy after login

Description: Header file

● stdio.h: used for printf() function

● sys/types.h: For the pid_t type, the data type of the variable used to store the process ID.

●unistd.h: used for getpid() and getppid() functions

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to get the process ID in C language? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template