鱼C论坛

 找回密码
 立即注册
查看: 2576|回复: 5

多进程问题

[复制链接]
发表于 2022-10-5 18:25:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我想让子进程创建子进程的子进程

  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<unistd.h>

  4. void main(void){
  5.     pid_t childpid;
  6.     for(int i = 1; i < 4; i++){
  7.         if(childpid = fork())
  8.             break;
  9.     }

  10.     printf("This is process %d, with parent %d, i %d \n", getpid(), getppid(), i);
  11. }
复制代码


输出的子进程的父进程pid 异常是为什么
question.png

出来的子进程的父进程pid全部相同

出来的子进程的父进程pid全部相同
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-5 18:44:45 | 显示全部楼层
您可能会有兴趣了解一下 Linux 进程收养相关的内容,可能与此有一定关联
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-5 20:59:58 | 显示全部楼层
父进程不等待子进程结束吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-5 21:55:02 | 显示全部楼层
人造人 发表于 2022-10-5 20:59
父进程不等待子进程结束吗?

这样子应该没问题吧  我只是想一系列的创造子进程
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-5 22:26:42 | 显示全部楼层
英俊男孩建坤 发表于 2022-10-5 21:55
这样子应该没问题吧  我只是想一系列的创造子进程
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>

  4. int main(void) {
  5.     pid_t childpid;
  6.     size_t i;
  7.     for(i = 1; i < 4; ++i) {
  8.         if((childpid = fork())) break;
  9.         sleep(1);
  10.     }
  11.     //sleep(1);
  12.     printf("This is process %d, with parent %d, i %zu \n", getpid(), getppid(), i);
  13.     return 0;
  14. }
复制代码

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>

  4. int main(void) {
  5.     pid_t childpid;
  6.     size_t i;
  7.     for(i = 1; i < 4; ++i) {
  8.         if((childpid = fork())) break;
  9.         //sleep(1);
  10.     }
  11.     sleep(1);
  12.     printf("This is process %d, with parent %d, i %zu \n", getpid(), getppid(), i);
  13.     return 0;
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-5 22:32:20 | 显示全部楼层
sleep的位置不对,改成下面这样
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>

  4. int main(void) {
  5.     pid_t childpid;
  6.     size_t i;
  7.     for(i = 1; i < 4; ++i) {
  8.         if((childpid = fork())) break;
  9.         //sleep(3);
  10.     }
  11.     printf("This is process %d, with parent %d, i %zu \n", getpid(), getppid(), i);
  12.     sleep(1);
  13.     return 0;
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 06:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表