gpt4 book ai didi

c - 创建4个子进程

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:06 25 4
gpt4 key购买 nike

我想创建4个子进程。我一定是哪里做错了什么。我没有解决它。
我的代码在下面。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int i;
int pid[4];
pid[0]=fork();
pid[1]=fork();

for(i=0;i<2;i++){
if(pid[i] > 0){
printf("\nI am parent with PID= %d\n",getpid());
printf("My childs PID is %d\n",pid[i]);
}
else if(pid[i] == 0){
sleep(5);
printf("********I'm child with PID=%d\n", getpid());
printf("My parents PID is %d\n", getppid());
}
printf("PID %d terminates!!!\n", getpid());

}

return 0; }

我试着改变很多事情。子进程丢失或产生太多。
我在等你的帮助。
输出:
I am parent with PID= 2531
My childs PID is 2532
PID 2531 terminates!!!
I am parent with PID= 2531
My childs PID is 2533
PID 2531 terminates!!!
I am parent with PID= 2533
My childs PID is 2532
PID 2533 terminates!!!
********I'm child with PID=2533
My parents PID is 1
PID 2533 terminates!!!
********I'm child with PID=2534
********I'm child with PID=2532
My parents PID is 2532
PID 2534 terminates!!!
My parents PID is 1
PID 2532 terminates!!!
I am parent with PID= 2532
My childs PID is 2534
PID 2532 terminates!!!
********I'm child with PID=2534
My parents PID is 1
PID 2534 terminates!!!

最佳答案

您的代码已正确创建4个进程:)错误在for循环中。这是一个基于您的代码的程序,它只需在终端上打印正确的输出,其中可以看到您正确地创建了4个进程:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void){
int i;
int pid[4];
pid[0]=fork();
pid[1]=fork();

if (pid[0] == 0 && pid[1] == 0) {
printf("I'm the child of the child [%d, parent: %d]\n",getpid(),getppid());
} else if (pid[0] == 0 && pid[1] != 0) {
printf("I'm the child [%d, parent: %d]\n",getpid(),getppid());
} else if (pid[0] != 0 && pid[1] == 0) {
printf("I'm the child [%d, parent: %d]\n",getpid(),getppid());
} else if (pid[0] != 0 && pid[1] != 0) {
printf("I'm the father [%d, parent: %d]\n",getpid(),getppid());
wait(&i);
wait(&i);
} else {
printf("some error occurred...\n");
}
return 0;
}

我的终端输出如下:
桌面>ps
用户PID PPID PGID TTY STAT命令
伊曼纽莱帕里西1481 1480 1481 ttys000 S-巴什
桌面>/tmp
我是父亲[1745,父母:1481]
我是孩子[1747,父母:1745]
我是孩子[1746,父母:1745]
我是孩子的孩子[1748,父母:1]
希望这对你有用:)
编辑
如果要创建4个子进程(父进程加上4个子进程,其ppid是父进程之一),可以使用以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void){
int i, tmp;
pid_t pid[4];

for (i=0;i<4;i++) {
pid[i] = fork();
if (pid[i] == 0) {
break;
}
}

if (pid[0] != 0 && pid[1] != 0 && pid[2] != 0 && pid[3] != 0) {
// That's the father, it waits for all the childs
printf("I'm the father [pid: %d, ppid: %d]\n",getpid(),getppid());
for(i=0;i<4;i++) {
wait(&tmp);
}
} else {
// That's the child, it print its pid, its parent pid and exits.
printf("I'm one of the children [pid: %d, ppid: %d]\n",getpid(),getppid());
}

return 0;
}

它在我的终端上产生以下输出:
桌面>ps
用户PID PPID PGID TTY STAT命令
伊曼纽勒帕里西690 688 690 ttys000 S-bash
桌面>/tmp
我是父亲[pid:877,父母:690]
我是其中一个孩子[879,家长:877]
我是其中一个孩子[880,家长:877]
我是其中一个孩子[878,家长:877]
我是其中一个孩子[881,家长:877]
如您所见,所有子进程都具有相同的父进程;)

关于c - 创建4个子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43029412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com