gpt4 book ai didi

c - 从管道读取值不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:38 26 4
gpt4 key购买 nike

我有一个程序,其中创建了属于同一个父亲的两个子进程。现在程序通过点击控制 C 开始,然后通过每次按下控制 Z 来运行。

目标是让 child 1 将两个数字写给 child 2,然后 child 2 将数字相除并将结果写回显示结果的 child 1。因此需要两个管道(fd 和 sd)。

我的第一个管道工作正常,所以数字正在发送...在 child 2 中(用于调试)它显示正确的数字,所以如果它有 8 和 2...显示正确答案 4在 child 2 中。现在我似乎无法得到这个“4”或任何返回给 child 1 的结果。

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>


void handleSignal(int sig)
{
if (sig == SIGTSTP)
{
return;
}
}

int main()
{
int fd[2];
int sd[2];
int pipe1 = 0;
int pipe2 = 0;
pid_t fork1 = 0;
pid_t fork2 = 0;
int num1, num2, result;
int myJump = 0;
int returnResult = 99;

signal(SIGINT, handleSignal);
printf("Waiting for interrupt\n");
pause();
signal(SIGINT, SIG_IGN);
pipe1 = pipe(fd);
pipe2 = pipe(sd);
//pipe checks been omitted...for simplicity

fork1 = fork();
//fork check been omited..for simplicity

signal(SIGTSTP, handleSignal); //wait till control Z is pressed

if (fork1 == 0)
{

dup2(fd[1], 1);
dup2(sd[0], 0);
close(fd[0]);
close(sd[1]);

while(1)
{
pause();
int randNum1 = rand() % 9 + 1;
fprintf(stderr, "Child 1: %d\n", randNum1);
printf("%d\n", randNum1);
fflush(stdout);
scanf("%d", &returnResult);
fprintf(stderr, "result in A :%d \n", returnResult);
}

}
else if (fork1 > 0)
{
fork2 = fork();

if (fork2 == 0)
{
signal(SIGTSTP, handleSignal);
dup2(fd[0], 0);
dup2(sd[1], 1);
close(fd[1]);
close(sd[0]);

if (myJump == 0)
{
pause();
scanf("%d", &num1);
printf("CHild 2: %d\n", num1);
myJump = 1;
}
if (myJump == 1)
{
while (1)
{
pause();
scanf("%d", &num2);
result = num2 / num1;
fprintf(stderr, "result from B: %d \n", result);
num1 = num2;
printf("%d \n", result);
}
}

}
else
{
wait();
}
}
else
{
printf("errror \n");
}

return 0;
}

如果有人能看出哪里出了问题,如果你要运行它...它的工作原理是先点击控件 C,然后你必须继续点击控件 Z。然后你可以看到子 A 的结果不匹配B 如下图所示

Waiting for interrupt
^C^ZChild 1: 2
result in A :99
^ZChild 1: 8
result in A :99
result from B: 4
^ZChild 1: 1
result in A :99
result from B: 0

最佳答案

管道实际上工作正常...它是失败的 scanf()。当您的第二个子进程启动时,它会调用:

  if (myJump == 0)
{
pause();
scanf("%d", &num1);
printf("CHild 2: %d\n", num1); /* <== problem here */
myJump = 1;
}

...printf() 将“CHild 2:”作为 sd[] 管道中的下一个数据。因此,当您的第一个子进程调用 scanf() 以读取第二个子进程的结果时,scanf() 会失败并保持 returnResult 不变.由于 scanf() 失败,数据将保留在流中,以后尝试 scanf() 结果也会以同样的方式失败。

如何修复它取决于你想要什么。如果您希望您的第二个子进程只返回它读取的数字作为第一次传递的结果,那么只需修改有问题的 printf() 以仅写入数字而不包含文本:

  if (myJump == 0)
{
pause();
scanf("%d", &num1);
printf("%d\n", num1); /* <== Changed: number only, no text */
myJump = 1;
}

我要补充一点,虽然上面提到的更改会解决问题,但您通常应该检查 scanf() 的返回值,看它是否成功,如果不成功,则采取适当的措施。

关于c - 从管道读取值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27736855/

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