一个小例子演示下epoll和管道的使用
2014-09-18 14:40
477 查看
#include <iostream> #include <unistd.h> #include <errno.h> #include <sys/epoll.h> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int main() { struct epoll_event ev; //事件临时变量 const int MAXEVENTS = 1024; //最大事件数 struct epoll_event events[MAXEVENTS]; //监听事件数组 int ret,pid; int pipe_fd[2]; if((ret=pipe(pipe_fd))<0) { cout<<"create pipe fail:"<<ret<< ",errno:" << errno <<endl; return -1; } ev.data.fd = pipe_fd[0]; //设置监听文件描述符 ev.events = EPOLLIN|EPOLLET; //设置要处理的事件类型 int epfd=epoll_create(MAXEVENTS); ret=epoll_ctl(epfd,EPOLL_CTL_ADD,pipe_fd[0],&ev); if (ret != 0) { cout << "epoll_ctl fail:" << ret << ",errno:" << errno << endl; close(pipe_fd[0]); close(pipe_fd[1]); close(epfd); return -1; } if((pid=fork())>0) { int count=epoll_wait(epfd,events,MAXEVENTS,5000); char r_buf[100]; for(int i=0;i<count;i++) { if((events[i].data.fd==pipe_fd[0])&&(events[0].events&EPOLLIN)) { int r_num=read(pipe_fd[0],r_buf,100); printf("read num is %d bytes data from the pipe,value is %d \n",r_num,atoi(r_buf)); } } close(pipe_fd[1]); close(pipe_fd[0]); close(epfd); cout<<"parent close read fd[0],wirte fd[1] and epfd over"<<endl; } else if(pid==0) { close(pipe_fd[0]);//read cout<<"sub close read fd[0] over,sub does't read"<<endl; char w_buf[100]; strcpy(w_buf,"1234"); if(write(pipe_fd[1],w_buf,5)!=-1)//you can remove this line for learn printf("sub write over \n"); close(pipe_fd[1]);//write printf("sub close write fd[1] over \n"); } return 0; }子进程写,父进程读,epoll用于监听。
相关文章推荐
- epoll的一个使用例子
- 一个epoll/aio/eventfd结合使用的简单例子_sunfly_新浪博客
- 一个epoll/aio/eventfd结合使用的简单例子_sunfly_新浪博客
- epoll使用的一个小例子,非常实用
- epoll使用的一个小例子,非常实用
- 下面的例子演示了100个线程同时向一个银行账户中存入1元钱,在没有使用同步机制和使用同步机制情况下的执行情况。
- 一个epoll/aio/eventfd结合使用的简单例子
- 一个使用PJSUA-API编写的简单例子,演示了SIP注册,呼叫,语音交互(少于200行代码)【译】
- 一个epoll/aio/eventfd结合使用的简单例子
- 同一个进程使用pipe(管道)的例子
- 如何使用epoll? 一个C语言的简单例子 - asdfjkl210 - ITeye技术网站
- 一个简单的epoll使用例子
- 一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。
- 如何使用epoll? 一个C语言的简单例子 - asdfjkl210 - ITeye技术网站
- 一个简单的epoll使用例子
- 如何使用epoll?一个完整的C例子
- 使用JSP + JAVABEAN + XML 开发的一个例子
- 通过一个简单的例子,介绍如何使用LDAP(转)
- 在MFC下使用OpenGL的一个简单的例子
- 一个使用spring的最简单的例子。