在 fork() 之后,我在我的程序中不断得到相同的 pid
I keep getting the same pid in my program after fork()
这是代码
pid_t pid;
srand(time(NULL));
for (int i=0; i < 2; i++)
{
pid = fork();
if (pid < 0)
{
std::cout<< "fork failed";
return -1;
}
else if (pid == 0)
{
std::cout<< "Process "<< i+1 << " ID: " << pid <<std::endl;
std::thread one (threadFunction, 0);
std::thread two (threadFunction, 1);
std::thread three (threadFunction, 2);
one.join();
two.join();
three.join();
}
else
{
wait (NULL);
exit(0);
}
}
循环应该创建两个不同的进程,但每当我运行它时,输出 pid 始终为 0。这是否意味着相同的过程
如果pid == 0,则代码正在执行子进程。如果 pid 不同于 0,则代码执行父进程
请参阅该人的返回值部分 http://man7.org/linux/man-pages/man2/fork.2.html
你可以用函数getpid找出孩子的真实PID
http://man7.org/linux/man-pages/man2/getpid.2.html
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- Linux获取通过execl脚本启动的程序的PID
- 通过 PID 分配信号处理程序
- Qt:当使用脚本从二进制运行时,无法找到应用程序pid
- 从C++中的可执行路径(或从hWnd或从pid)获取程序名称
- 通过C / C ++程序查找MySQL Pid