eh
Here I want to wirte data to pipe filedescriptors in the child progress.
And read data form pipe in the parent progress.
because i'm learning the ET mode,so I write 10 characters to the pipe, the sleep 2 seconds, and read 5 characters from the pipe in the same time.
however the result is
res 1
aaaa
I want to know how can I do to make the result like
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;
}
This scenario is explicitly mentioned in the epoll
manual page : You use edge triggering, read only part of the data, and then wait again which will block.
One suggested solution is to make the descriptor non-blocking (which you forget to do) and read in a loop until read
returns with the EAGAIN
error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.