부모 프로세스가 4 개의 정수 변수 (a, b, c 및 x)에 대한 사용자 입력을 받아서 하위 프로세스로 파이프 처리하려고합니다. 자식 프로세스는 ax^2 + bx + c를 평가하고 결과 r을 부모에게 파이프합니다.C에서 파이프를 통한 부모 - 자식 통신
내가 지금 가지고있는 코드를 사용하면 결과는 1이됩니다. print 서술문의 동작은 x가 읽히고 있다고 생각하게 만듭니다. 그러나 x * x를 r으로하려고해도, 올바른 솔루션을 얻지 못합니다. 나는 무엇을 잘못 할 수 있 었는가?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#define PIPE_INPUT 0
#define PIPE_OUTPUT 1
#define INDENT 20
void testError(int err, const char * str){
if (err < 0){
perror(str);
exit(EXIT_FAILURE);
}
}
int main(){
int fd_toChild[2];
int fd_toParent[2];
int buff[20];
pid_t pid;
testError(pipe(fd_toChild), "pipe");
testError(pipe(fd_toParent), "pipe");
testError(pid = fork(), "fork");
int a;
int b;
int c;
int x;
int r;
if (pid > 0){
// this is the parent
int status;
while(1){
close(fd_toChild[PIPE_INPUT]);
close(fd_toParent[PIPE_OUTPUT]);
printf("This program will evaluate a polynomial a(x^2) + bx + c. \n");
printf("Please input an integer value for a: ");
scanf("%d", &a);
write(fd_toChild[PIPE_OUTPUT], &a, sizeof(a));
printf("Parent writes value %d to a \n", a);
printf("Please input an integer value for b: ");
scanf("%d", &b);
write(fd_toChild[PIPE_OUTPUT], &b, sizeof(b));
printf("Parent writes value %d to b \n", b);
printf("Please input an integer value for c: ");
scanf("%d", &c);
write(fd_toChild[PIPE_OUTPUT], &c, sizeof(c));
printf("Parent writes value %d to c \n", c);
printf("Please input an integer value for x: ");
scanf("%d", &x);
write(fd_toChild[PIPE_OUTPUT], &x, sizeof(x));
printf("Parent writes value %d to x \n", x);
close(fd_toChild[PIPE_OUTPUT]);
close(fd_toParent[PIPE_INPUT]);
read(fd_toParent[PIPE_INPUT], buff, sizeof(r));
pid = wait(&status);
fprintf(stdout, "Parent received back the result %d.\n", r);
printf("The polynomial evaluates to %d \n", r);
close(fd_toParent[PIPE_OUTPUT]);
break;
}
}
else{
// this is child
close(fd_toChild[PIPE_OUTPUT]);
close(fd_toParent[PIPE_INPUT]);
while(1){
close(fd_toChild[PIPE_OUTPUT]);
close(fd_toParent[PIPE_INPUT]);
read(fd_toChild[PIPE_INPUT], buff, sizeof(a));
printf("Child reads value %d from a \n", a);
read(fd_toChild[PIPE_INPUT], buff, sizeof(b));
printf("Child reads value %d from b \n", b);
read(fd_toChild[PIPE_INPUT], buff, sizeof(c));
printf("Child reads value %d from c \n", c);
read(fd_toChild[PIPE_INPUT], buff, sizeof(x));
printf("Child reads value %d from x \n", x); r = a * x * x + b * x + c;
printf("x is %d", x); printf("r is %d", r);
write(fd_toParent[PIPE_OUTPUT], buff, sizeof(r));
fprintf(stdout, "%*sChild is all done\n", INDENT, "");
close(fd_toChild[PIPE_INPUT]);
}
}
return EXIT_SUCCESS;
}
:
나는 코드의 작업 버전을 표시하기 위해 여러 편집을했다. 부모 프로세스 : 먼저'fd_toParent [PIPE_INPUT]'을 닫고 그 바로 다음부터 읽으려고합니다. 자식 프로세스 : 루프의 끝에서'fd_toChild [PIPE_INPUT]'을 닫으면 루프의 모든 추가 반복이 예상대로 작동하지 않습니다. 이러한 모든 유형의 오류는 읽기/쓰기 리턴 코드를 확인하면 쉽게 발견 할 수 있습니다. – gudok와우. 감사! –
위 코드가 다른 문제도 가지고 있습니다. @gudok은'pipe'를 닫는 것에 대해서는 옳지 만'buff'을 읽지 만 다른 초기화되지 않은 변수들에서 출력하려고 시도합니다. – tijko