gpt4 book ai didi

c - 带 fork 的管道和子创建

转载 作者:行者123 更新时间:2023-11-30 14:50:39 25 4
gpt4 key购买 nike

这是我正在尝试解决的问题的代码。如果有人知道如何解决这个问题,请帮忙。我试图用与我们将创建的 child 一样多的管道来创建无限个 child ,但我失去了一些带有管道的 child ,我不知道为什么。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
void main() {
int n;
pid_t p;
int pfd[2];
int val=0;
int i;

printf("Enter the number of child you want:");
scanf("%d",&n);

pipe(pfd);

while(1) {
for(i=1;i<=n;i++) {

p=fork();
if(p>0){
close(pfd[0]);
sleep(5);
val++;
printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getppid(),getpid());
write(pfd[0],&val,sizeof(val));
close(pfd[1]);
//wait(NULL);
//exit(EXIT_SUCCESS);
}
else {
close(pfd[1]);
val++;
//printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getpid(),getppid());
//while(read(pfd[0],&val,sizeof(val))>0)
read(pfd[0],&val,sizeof(val));
//printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getpid(),getppid());
write(pfd[0],&val,sizeof(val));
close(pfd[0]);
_exit(EXIT_SUCCESS);
}
}
}
printf("Message %d: last child's pid is %d and my father is : %d \n", n,getpid(),getppid());
}

输出是:输入您想要的 child 数量:3

Message 1: Process 1 has process id 678 and his child's id is : 15041

Message 2: Process 2 has process id 678 and his child's id is : 15041

Message 3: Process 3 has process id 678 and his child's id is : 15041

Message 4: Process 1 has process id 678 and his child's id is : 15041

Message 5: Process 2 has process id 678 and his child's id is : 15041

Message 6: Process 3 has process id 678 and his child's id is : 15041

Message 7: Process 1 has process id 678 and his child's id is : 15041

Message 8: Process 2 has process id 678 and his child's id is : 15041

Message 9: Process 3 has process id 678 and his child's id is : 15041

Message 10: Process 1 has process id 678 and his child's id is : 15041

Message 11: Process 2 has process id 678 and his child's id is : 15041

Message 12: Process 3 has process id 678 and his child's id is : 15041

所以我的问题是进程ID没有改变,我找不到原因

最佳答案

So my problem is that the process id's are not changing and I can't find why

因为

printf("Message %d: Process %d has process id %d and its child has process id %d \n",val,i,getppid(),getpid());

是由父进程执行的,所以getppid(父进程的父进程的pid)是始终相同,getpid 也保持不变。唯一改变的是 val 和 `i'。

您只需创建一次管道,然后将其用于所有 fork ,同时关闭唯一创建的管道然后再次 fork ,这不会有好结果。

您必须在进行 fork 之前创建管道。另外,您正在关闭父进程上管道的读取端,以及关闭端的写入阅读结束。你必须在书写端写。

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

int main(void)
{
pid_t p;
int pfd[2];
int i;
int val = 0;
int n = 3;

for(i=1;i<=n;i++) {
if(pipe(pfd) < 0)
{
perror("pipe");
continue;
}

p = fork();

if(p < 0)
{
perror("fork");
continue;
}

if(p)
{
// PARENT PROC
close(pfd[0]);
val++;
printf("Message %d: Parent process has process id %d and its child has process id %d\n", val, getpid(), p);
write(pfd[1], &val, sizeof val);
close(pfd[1]);
int status;
waitpid(p, &status, 0);

if(WIFEXITED(status))
printf("Child %d exit status: %d\n", i, WEXITSTATUS(status));
else
printf("Child %d did not exit normally\n", i);
} else {
// CHILD PROC

close(pfd[1]);
if(read(pfd[0], &val, sizeof val) < 0)
{
printf("Child %d: could not read from pipe\n", i);
_exit(EXIT_FAILURE);
}

printf("Child %d, message: %d. Child pid: %d, parent pid: %d\n", i, val, getpid(), getppid());

close(pfd[0]);
_exit(EXIT_SUCCESS);
}
}

return 0;
}

这会打印

Message 1: Parent process has process id 9829 and its child has process id 9830
Child 1, message: 1. Child pid: 9830, parent pid: 9829
Child 1 exit status: 0
Message 2: Parent process has process id 9829 and its child has process id 9831
Child 2, message: 2. Child pid: 9831, parent pid: 9829
Child 2 exit status: 0
Message 3: Parent process has process id 9829 and its child has process id 9832
Child 3, message: 3. Child pid: 9832, parent pid: 9829
Child 3 exit status: 0

最后一件事:main 的正确原型(prototype)是:

  • int main(void);
  • int main(int argc, char *argv[]);
  • int main(int argc, char **argv);

您的void main()不正确。

关于c - 带 fork 的管道和子创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48917988/

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