#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/epoll.h>
int main(int argc, char *argv[]){
int fd[2];
pipe(fd);
pid_t pid = 0;
pid=fork();
if(pid == 0)
{
close(fd[0]);
char buf[5];
char ch='a';
while(1)
{
sleep(3);
memset(buf,ch++,sizeof(buf));
write(fd[1],buf,strlen(buf)+1);
}
}else if(pid > 0)
{
close(fd[1]);
// 创建树
int epfd = epoll_create(1);
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd[0];
struct epoll_event evs[1];
epoll_ctl(epfd,EPOLL_CTL_ADD,fd[0],&ev);
//监听
while(1)
{
int n = epoll_wait(epfd,evs,1,-1);
if (n == 1) {
char buf[1024] = {0};
int ret = read(fd[0],buf,sizeof(buf));
if(ret <= 0)
{
close(fd[0]);
epoll_ctl(epfd,EPOLL_CTL_DEL,fd[0],&ev);
break;
}else
printf("buf = %s\n",buf);
}
}
}
return 0;
}