阅读量:96
getppid() 是一个 Linux 系统中的系统调用,用于获取当前进程的父进程 ID
#include
#include
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s \n" , argv[0]);
return 1;
}
pid_t target_pid = atoi(argv[1]);
pid_t parent_pid = getppid(target_pid);
if (parent_pid == -1) {
perror("getppid");
return 1;
}
printf("Parent process ID of PID %d is %d\n", target_pid, parent_pid);
return 0;
}
将上述代码保存为 getppid_parent.c,然后使用以下命令编译:
gcc getppid_parent.c -o getppid_parent
运行编译后的程序,并传递一个进程 ID 作为参数:
./getppid_parent
其中 是你想要查询的进程 ID。程序将输出该进程的父进程 ID。