一、接口介绍
getpid():获取当前进程号
getppid():获取父进程号
代码示例
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
pid_t p1 = -1, p2 = -1;
printf("hello.\n");
p1 = getpid();
printf("pid = %d.\n", p1);
p2 = getppid();
printf("parent id = %d.\n", p2);
return 0;
}
日志打印
@R720:~/LinuxStudy/process$ ./test
hello.
pid = 42125.
parent id = 36858.
@R720:~/LinuxStudy/process$ ps | grep 42125
@R720:~/LinuxStudy/process$ ps | grep 36858
36858 pts/17 00:00:00 bash