c++ getpid函数
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.
如果我们正在处理这些过程,则表明在Linux中使用C语言进行相关的编程。 我们需要可以使用某些功能通过代码创建的进程ID。 因此,我们需要一些能够检索进程ID的函数和数据类型。
First of all, I would recommend to read pid_t type in C
首先,我建议阅读C语言中的pid_t类型
在C中获取进程ID的函数 (Functions to get process ids in C)
There are two functions which are used to get the process ids, the functions are:
有两个函数用于获取进程ID,这些函数是:
getpid()
getpid()
getppid()
getppid()
1)C语言中的getpid()函数 (1) getpid() function in C)
When any process is created, it has a unique id which is called its process id. This function returns the process id of the calling function.
创建任何进程时,它都有一个唯一的ID,称为其进程ID。 该函数返回调用函数的进程ID。
Syntax:
句法:
pid_t getpid();
2)C语言中的getppid()函数 (2) getppid() function in C)
This function returns the process id of the parent function.
该函数返回父函数的进程ID。
Syntax:
句法:
pid_t getppid();
Note: pid_t is the type of process id, which is an unsigned integer type of data type.
注意: pid_t是进程ID的类型,它是数据类型的无符号整数类型。
C程序演示getpid()和getppid()的示例 (C program to demonstrate example of getpid() and getppid())
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
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
输出量
The process id: 31120
The process id of parent function: 31119
Header files
头文件
stdio.h - it is used for printf() function
stdio.h-用于printf()函数
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.
sys / types.h-用于pid_t类型,即用于存储进程ID的变量的数据类型。
unistd.h - it is used for getpid() and getppid() functions
unistd.h-用于getpid()和getppid()函数
翻译自: https://www.includehelp.com/c/getpid-and-getppid-functions-in-c-linux.aspx
c++ getpid函数