原文:http://blog.csdn.net/force_eagle/article/details/4348017
epoll网上g一大把, 就不详细叙述了.
推荐几篇好文章:
epoll精髓
epoll相关资料整理
epoll LT VS ET
EPOLL为我们带来了什么
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- void call_poll(void)
- {
- struct pollfd fds;
- int32_t timeout_msecs = 5000;
- int err;
-
- fds.fd = 1;
- fds.events = POLLIN | POLLPRI ;
- err = poll( &fds, 1, timeout_msecs );
- if ( err > 0 ) {
- printf("Data is available now./n");
- }
- else if ( err == 0 ) {
- printf("No data within five seconds./n");
- }
- else {
- perror( "poll()" );
- }
-
- }
- #include
-
- void call_epoll(void)
- {
- int epfd;
- struct epoll_event ev_stdin;
- int err;
-
- epfd = epoll_create(1);
- if ( epfd < 0 ) {
- perror( "epoll_create()" );
- return ;
- }
-
- bzero( &ev_stdin, sizeof( struct epoll_event) );
- ev_stdin.events =
-
- EPOLLIN | EPOLLPRI
-
-
-
- | EPOLLERR | EPOLLHUP
-
- | EPOLLET
-
-
- | EPOLLONESHOT
- ;
-
- err = epoll_ctl( epfd, EPOLL_CTL_ADD, 1, &ev_stdin );
- if ( err < 0 ) {
- perror( "epoll_ctl()" );
- goto _out;
- }
- err = epoll_wait( epfd, &ev_stdin, 1, 5000 );
- if ( err < 0 ) {
- perror( "epoll_wait()" );
- }
- else if ( err == 0 ) {
- printf("No data within five seconds./n");
- }
- else {
- printf("Data is available now./n");
- }
-
-
- err = epoll_ctl( epfd, EPOLL_CTL_DEL, 1, &ev_stdin );
- if ( err < 0 ) {
- perror( "epoll_ctl()" );
- }
- _out:
- close( epfd );
- }
- int main ()
- {
- call_epoll();
- return 0;
- }
阅读(866) | 评论(0) | 转发(0) |