c++ getpid函数_C Linux中的getpid()和getppid()函数

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,这些函数是:

  1. getpid()

    getpid()

  2. 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

头文件

  1. stdio.h - it is used for printf() function

    stdio.h-用于printf()函数

  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.

    sys / types.h-用于pid_t类型,即用于存储进程ID的变量的数据类型。

  3. 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函数

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值