과제가 빡빡하지만 막혔습니다. 오류가 발생하고 내가 잘못하고있는 것을 이해하지 못합니다.C 파이프 초기화 오류
그래서 주에서 나는 세 명의 아이들을 만듭니다. 첫 번째와 두 번째 사이에는 파이프 (pipe12
)가 있습니다. 두 번째와 세 번째 사이에는 파이프 (pipe23
)가 있습니다. 이제 첫 번째 자식 (reader
)이 읽을 준비가되면 pipe12
을 닫지 만 두 번째 자식의 읽기는 EOF
이 아닙니다. 둘째로 두 번째 자식이 pipe23
에 쓰려고 할 때 자식이 충돌합니다.
내가 파이프의 초기화에 뭔가 잘못된 것 같지만, 뭐라구?
void reader(void)
{
atexit(*reader_exit);
if((readfile = fopen(calculatorfile,"r"))==NULL)
{
printf("R send error to errorHandler"); //errpipe!
exit(0);
}
char line[50];
const char *valid_characters = "-/*\n";
while(fgets (line, sizeof line, readfile) != NULL)
{
printf("R reading ...%s",line);
char *c = line;
while(*c)
{
if(!strchr(valid_characters,*c))
{
printf("R invalid character: %c in %s",*c,line);
line[0]=0;
break;
}
c++;
}
write(pipe12[1],line,sizeof line);
}
exit(0);
}
void reader_exit(void)
{
printf("R reader exit\n");
fclose(readfile);
close(pipe12[1]);
close(tasks[childnr].errorpipe[1]);
}
그리고 둘째 아이 :
이
부모for(childnr=2; childnr>=0;childnr--)
{
tasks[childnr].pid=fork();
if(tasks[childnr].pid==-1)
{
printf("fork error\n");
exit(1);
}
else if(tasks[childnr].pid==0)
{
switch(childnr)
{
case 0:
close(pipe12[0]);
close(pipe23[0]);
close(pipe23[1]);
reader();
break;
case 1:
close(pipe12[1]);
close(pipe23[0]);
tokenizer();
break;
case 2:
close(pipe12[0]);
close(pipe12[1]);
close(pipe23[1]);
evaluator();
break;
default:
printf("childnr error\n"); //errorhandling
}
}
else
close(tasks[childnr].errorpipe[1]);
}
close(pipe12[0]);
close(pipe12[1]);
close(pipe23[0]);
close(pipe23[1]);
... continue with the parent
이 첫 번째 자식이다
void tokenizer(void)
{
atexit(*tokenizer_exit);
char buffer[50];
while(read(pipe12[0],buffer,sizeof buffer)!=EOF)
{
printf("T %s",buffer);
char *token = strtok(buffer," ");
while(token!=NULL)
{
printf("T %s\n",token);
write(pipe23[1],token,sizeof token);
token = strtok(NULL, " ");
}
sleep(2);
}
exit(0);
}
파일 설명자를'reader (pipe12 [1])'로 전달하여 독자의 일반성을 향상시킬 수 있습니다. tokenizer : tokenizer (pipe12 [0], pipe23 [1]);와 평가자 : evaluationator (pipe23 [0]);와 비슷하게. 독자에게는 파일 이름도 전달해야합니다 :'reader (calculatorfile, pipe12 [1]);'. –
고마워, 내가 할께. – Kat
하위 프로세스는 어떻게 충돌합니까? 'SIGPIPE' 또는'SIGSEGV' /'SIGBUS'? – C2H5OH