簡體   English   中英

為什么父進程在ETmode下無法從pipe讀取數據

[英]why the parent progress can't read the data from pipe in the ETmode

在這里,我想將數據寫入子進程中的 pipe 文件描述符。

並在父進程中讀取 pipe 的數據。

因為我正在學習ET模式,所以我將10個字符寫入pipe,睡眠2秒,同時從pipe讀取5個字符。

然而結果是

res 1
aaaa

我想知道我該怎么做才能使結果像

res 1
aaaa
res 1
bbbb
...
make it can read the later charactes.
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
#include<sys/epoll.h>
#include<iostream>
#include<fcntl.h>
#include"wrap.h"
#define MAXLINE 10

using namespace std;

int setnonblocking(int fd){
    int old_option = fcntl(fd, F_GETFL);
    int new_option = old_option|O_NONBLOCK;
    fcntl(fd, F_SETFL, new_option);
    return old_option;
}


int main(int argc, char* argv[]){
    int efd, i;
    int pfd[2];
    pid_t pid;
    char writebuf[MAXLINE], ch = 'a', readbuf[MAXLINE];
    pipe(pfd);
    pid = fork();

    if(pid == 0){       // child progress
        Close(pfd[0]);  //close read port
        while(true){
            for(i = 0;i<MAXLINE/2;i++){
                writebuf[i] = ch;
            }
            writebuf[i-1] = '\n';
            ch++;

            for(;i<MAXLINE;i++){
                writebuf[i] = ch;
            }
            writebuf[i-1] = '\n';
            ch++;

            Write(pfd[1], writebuf, sizeof(writebuf));
            setnonblocking(pfd[1]);
            sleep(2);
        }
        Close(pfd[1]);
    }
    else if(pid > 0){
        struct epoll_event event;
        struct epoll_event resevent[10];
        int res, len;

        Close(pfd[1]);              //close read-end of pipe
        efd = epoll_create(10);

        event.events = EPOLLIN|EPOLLET;
        // event.events = EPOLLIN;

        event.data.fd = pfd[0];
        epoll_ctl(efd, EPOLL_CTL_ADD, pfd[0], &event);
        setunonblocking(pfd[0]);
        while(1){
            res = epoll_wait(efd, resevent, 10, -1);
            printf("res %d\n", res);
            
            if(resevent[0].data.fd == pfd[0]){
                len = Read(pfd[0], readbuf, MAXLINE/2); 
                Write(STDOUT_FILENO, readbuf, len);
            }
        }

        Close(pfd[0]);
        Close(efd);
    }else{
        perror("fork");
        exit(-1);
    }
    return 0;
}

這種情況在epoll手冊頁中明確提到:您使用邊緣觸發,只讀取部分數據,然后再次等待會阻塞。

一種建議的解決方案是使描述符成為非阻塞(您忘記這樣做)並在循環中讀取,直到read返回EAGAIN錯誤。

暫無
暫無

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

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