각 루프에서 작성된 하위 프로세스를 사용하여 지수 테일러 확장을 평가하는이 c 프로그램을 실행하면이 합계의 자릿수를 부모에게 반환하고, 불량 파일 설명자 오류, 나는 왜 그런지 모르지만 루프에있는 파이프를 열고 닫는 방법에 문제가있을 수 있다고 생각합니다.파이프의 파일 설명자에 오류가 있습니다.
내 코드는 다음과 같습니다
#include<unistd.h>
#include<stdio.h>
#include<sys/wait.h>
#include<stdlib.h>
int main()
{int i; pid_t id;
int count=1;
int fact=1;
int N;
double S=1;double s1=0; double div;double x;
int p[2];
printf("Input x and N : \n");
scanf("%lf %d",&x,&N);
if(pipe(p) == -1)
{perror("Error opening pipe");
return -1; }
else{
for(i=1;i<=N;i++)
{ id=fork();
if(id==0)
{ close(p[0]);
while(count<i)
{ fact*=(fact+1);
x*=x;
count++;
}
div=x/fact;
if(write(p[1],&div,sizeof(double))!=sizeof(double))
perror("file writing error");
exit(0);
}
else {
wait(NULL);
close(p[1]); \\ ***<-- Problem here***
if(read(p[0],&s1,sizeof(double))!=sizeof(double))
{
perror("file become empty");
return -1;}
S+=s1;
}}}
printf("Sum = %lf",S);
return 1;}
여전히 읽기 및 쓰기 모두에서 동일한 오류 메시지가 표시됩니다. –