Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1676344
  • 博文数量: 347
  • 博客积分: 9328
  • 博客等级: 中将
  • 技术积分: 2680
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 23:45
文章分类

全部博文(347)

文章存档

2016年(1)

2013年(4)

2012年(207)

2011年(85)

2010年(50)

分类: LINUX

2012-06-27 18:14:37

原文:http://blog.csdn.net/force_eagle/article/details/4348017

epoll网上g一大把, 就不详细叙述了.

推荐几篇好文章:

epoll精髓

epoll相关资料整理

epoll LT VS ET

EPOLL为我们带来了什么

[cpp]
  1. #include    
  2. #include    
  3. #include    
  4. #include    
  5. #include    
  6. #include    
  7. #include    
  8. #include    
  9. #include    
  10.   
  11. void call_poll(void)  
  12. {  
  13.     struct pollfd fds;  
  14.     int32_t timeout_msecs = 5000;  
  15.     int err;  
  16.       
  17.     fds.fd = 1;  
  18.     fds.events = POLLIN | POLLPRI ;  
  19.     err = poll( &fds, 1, timeout_msecs );  
  20.     if ( err > 0 ) {  
  21.         printf("Data is available now./n");  
  22.     }  
  23.     else if ( err == 0 ) {  
  24.         printf("No data within five seconds./n");  
  25.     }  
  26.     else  {  
  27.         perror( "poll()" );  
  28.     }  
  29.   
  30. }  
  31. #include    
  32.   
  33. void call_epoll(void)  
  34. {  
  35.     int epfd;  
  36.     struct epoll_event ev_stdin;  
  37.     int err;  
  38.   
  39.     epfd = epoll_create(1);  
  40.     if ( epfd < 0 ) {  
  41.         perror( "epoll_create()" );  
  42.         return ;  
  43.     }  
  44.   
  45.     bzero( &ev_stdin, sizeofstruct epoll_event) );  
  46.     ev_stdin.events =   
  47.         //  available for read operations   
  48.         EPOLLIN | EPOLLPRI   
  49.         // available for write operations   
  50.         // | EPOLLOUT    
  51.         // Error condition && Hang up happened    
  52.         | EPOLLERR | EPOLLHUP   
  53.         // Sets the Edge Triggered behaviour    
  54.         | EPOLLET   
  55.         // Sets the one-shot behaviour.    
  56.         // must call epoll_ctl with EPOLL_CTL_MOD to re-enable   
  57.         | EPOLLONESHOT   
  58.         ;  
  59.   
  60.     err = epoll_ctl( epfd, EPOLL_CTL_ADD, 1, &ev_stdin );  
  61.     if ( err < 0 ) {  
  62.         perror( "epoll_ctl()" );  
  63.         goto _out;  
  64.     }  
  65.     err = epoll_wait( epfd, &ev_stdin, 1, 5000 );  
  66.     if ( err < 0 ) {  
  67.         perror( "epoll_wait()" );  
  68.     }  
  69.     else if ( err == 0 ) {  
  70.         printf("No data within five seconds./n");  
  71.     }  
  72.     else {  
  73.          printf("Data is available now./n");  
  74.     }  
  75.     //err = epoll_ctl( epfd, EPOLL_CTL_DEL, 1, &ev_stdin );   
  76.     //   
  77.     err = epoll_ctl( epfd, EPOLL_CTL_DEL, 1, &ev_stdin );  
  78.     if ( err < 0 ) {  
  79.         perror( "epoll_ctl()" );  
  80.     }  
  81. _out:  
  82.     close( epfd );  
  83. }  
  84. int main ()  
  85. {  
  86.     call_epoll();  
  87.     return 0;  
  88. }  

 

阅读(806) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~