簡體   English   中英

C孩子讀“給資源暫時不可用”

[英]C child read giving “resource temporarily unavailable”

所以我有一個從父進程到子進程的文件流 - 大多數時候它工作正常。 但是,當快速讀取多次時,使用fgets()將返回NULL並將錯誤設置為“資源暫時不可用”。 問題是間歇性的 - 並且運行執行讀取的腳本有時會使fgets返回NULL,有時會返回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);

} 

這意味着有人將標准輸入文件描述符設置為非阻塞。

資源暫時不可用是與EAGAIN / EWOULDBLOCK對應的錯誤消息,只有在選擇了非阻塞IO並且沒有要讀取的數據時, read()才會返回該錯誤消息。

請注意,在執行子進程之前,父進程可能會將文件描述符設置為非阻塞。

進一步調查的一些想法:

  • 如果你strace()子進程,哪個系統調用返回EAGAIN 哪個文件描述符號?

  • 什么是printf("%d\\n", fcntl(fileno(fpin), F_GETFL));的輸出printf("%d\\n", fcntl(fileno(fpin), F_GETFL)); 就在失敗的fgets()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM