gpt4 book ai didi

c - 如何在C中制作父子双向管道

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:20 25 4
gpt4 key购买 nike

我正在尝试做一个双向管道,父级向子级发送 n 个数字 (int),子级返回它们的两倍。我不知道我的错误是什么?我扫描了数字 n 是 parent ,通过 fd1[1] 发送它,然后继续发送这 n 个数字让 child 加倍。在 child 身上,我读到数字 n,然后对于我读到的每个数字,我加倍并发回。

int main(){
int pid,n,c,p,k,nbread;
char buf1[2], buf2[2];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid=fork();
if(pid==0){
close(fd1[1]);
close(fd2[0]);
read(fd1[0],buf2,sizeof(int));
n = atoi(buf2);
for(int i = 0; i<n;i++){
nbread = read(fd1[0],buf2,sizeof(int));
sleep(3);
if(nbread == -1)
exit(1);
c = atoi(buf2);
c = c*2;
sprintf(buf2,"%d",c);
write(fd2[1],buf2, sizeof(int));
}
close(fd1[0]);
close(fd2[1]);
}
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d",&p);
sprintf(buf1,"%d",p);
write(fd1[1],buf1,sizeof(int));
sleep(3);
for(int i=0;i<n;i++){
sprintf(buf1,"%d",i);
write(fd1[1],buf1,sizeof(int));
read(fd2[0],buf1,sizeof(int));
printf("number is: %s",buf1);
}
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;}

最佳答案

修复父循环以测试 p 而不是 n 修复了主要问题。确保缓冲区足够大也是一个好主意。写入整个缓冲区是可以的,但不一定是理想的。

此代码有效;它有更多的调试输出。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
int pid, n, c, p, k, nbread;
char buf1[12], buf2[12];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if (pid == 0)
{
close(fd1[1]);
close(fd2[0]);
read(fd1[0], buf2, sizeof(buf2));
n = atoi(buf2);
printf("Child read %d\n", n);
for (int i = 0; i < n; i++)
{
printf("child dozes...\n");
sleep(3);
printf("child wakes...\n");
nbread = read(fd1[0], buf2, sizeof(buf2));
if (nbread == -1)
{
fprintf(stderr, "child exits after read failure\n");
exit(1);
}
c = atoi(buf2);
c = c * 2;
sprintf(buf2, "%d", c);
write(fd2[1], buf2, sizeof(buf2));
printf("Child wrote [%s]\n", buf2);
}
close(fd1[0]);
close(fd2[1]);
printf("Child done\n");
exit(0);
}
else
{
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d", &p);
sprintf(buf1, "%d", p);
write(fd1[1], buf1, sizeof(buf1));
printf("Parent wrote [%s]\n", buf1);
printf("parent dozes...\n");
sleep(3);
printf("parent wakes...\n");
for (int i = 0; i < p; i++)
{
sprintf(buf1, "%d", i);
write(fd1[1], buf1, sizeof(buf1));
printf("parent wrote [%s]\n", buf1);
read(fd2[0], buf2, sizeof(buf2));
printf("number is: %s\n", buf2);
}
close(fd1[1]);
close(fd2[0]);
wait(NULL);
}
return 0;
}

示例输出:

Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6

该代码将子代码和父代码放入单独的 ifelse block 中。它不会检测 pipe()fork() 中的故障,这是次优的。 child exit(0) 不再重要。

关于c - 如何在C中制作父子双向管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53313194/

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