所以我有一个来自父进程到子进程的文件流,并且大多数情况下它都正常工作。然而,当快速多次从中读取时,使用fgets()将返回NULL并设置错误为“资源暂时不可用”。问题是不定期的,运行进行读取的脚本有时会使fgets返回NULL,有时则不会。
有人能帮我停止这个错误吗?谢谢!
编辑:这是一些代码..我不确定什么其他代码会有帮助?有相当多的代码。
有人能帮我停止这个错误吗?谢谢!
编辑:这是一些代码..我不确定什么其他代码会有帮助?有相当多的代码。
// this is the bit that gets a line from the child
if( fgets( line, MAX_LINE_LENGTH, fpin ) == NULL ) {
if( ferror(fpin) ) {
perror("error on stream fpin");
}
free( line );
return FAIL;
}
按照要求,以下是打开管道并处理子进程的代码。
// set up pipes
int readPipe[2]; // child -> parent communication
int writePipe[2]; // parent -> child communication
int errorPipe[2]; // child -> parent, to check for errors in exec
/* create pipe */
pipe( readPipe ); // error if return val < 1 for any
pipe( writePipe );
pipe( errorPipe );
pid_t pid; /* process id when we fork */
pid = fork(); /* create new child */
if( pid == 0 ) { /* pid == 0 indicates child process */
// close unused fds
close( PARENT_READ );
close( PARENT_WRITE );
close( errorPipe[0] );
dup2( CHILD_READ, 0 ); // replace stdin with pipe in
dup2( CHILD_WRITE, 1 ); // replace stdout with pipe out
/* replace child process with program to run */
execvp(args[0], args);
// if we get here, exec failed so inform the parent
char *error_message = "exec failed";
write( errorPipe[1], error_message, strlen(error_message)+1 );
exit(-1);
}
fpin
是如何打开的?包括那些(PARENT|CHILD)_(READ|WRITE)
的定义也可能有所帮助。 - caf