c - 如何在C中获取子进程PID
问题描述
为了获得子进程的PID,我正在这样做。
pid_t pid;
pid=fork();
if(pid==0){
//child's work
}else{
printf("The child's PID is %d",pid);
}
我想pid
从父母那里打印孩子的!那么printf
pid
我是否需要使用可以getpid()
吗?
解决方案
我认为这总结了这个问题:
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
printf("In child => Own pid : %d\n", getpid());
printf("In child => Parent's pid : %d\n", getppid());
}
else{
printf("In Parent => Child's pid is %d\n", pid);
printf("In Parent => Own pid : %d\n", getpid());
}
return 0;
}
推荐阅读
- python - 如何使用 scipy.optimize.minimize() 指定目标函数最小化的参数?
- python - Python 线性回归预测错误 - 数组问题
- gcc - 获取 LTO 位码的 GCC 标志
- r - 与 R 的文本相关性
- c - 为什么 SDL2 渲染器不向我的窗口绘制任何内容?
- presto - AWS Athena 使用嵌套数组,尝试在数组中搜索字段
- macos - 我应该在哪个目录中安装 Google Cloud 的 SDK(以及一般的 SDK)
- java - 从 gradle 项目中生成可执行文件
- python - 使用带有 kivy 按钮的 random.choice
- typescript - 使用 ExpressJS 和 TypeScript 检索查询参数的正确语法是什么?