epoll 使用实例
2016-01-25 16:37
183 查看
原文:/article/1667441.html
epoll网上g一大把, 就不详细叙述了.
推荐几篇好文章:
epoll精髓
epoll相关资料整理
epoll LT VS ET
epoll(7) - Linux man page
EPOLL为我们带来了什么
[cpp] view plaincopyprint?
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <poll.h>
#include <sys/types.h>
#include <error.h>
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 <sys/epoll.h>
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 =
// available for read operations
EPOLLIN | EPOLLPRI
// available for write operations
// | EPOLLOUT
// Error condition && Hang up happened
| EPOLLERR | EPOLLHUP
// Sets the Edge Triggered behaviour
| EPOLLET
// Sets the one-shot behaviour.
// must call epoll_ctl with EPOLL_CTL_MOD to re-enable
| 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 );
//
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;
}
阅读(274) | 评论(0) | 转发(0) |
0
上一篇:C++成员函数指针的应用
下一篇:C++中回调(CallBack)的使用方法
相关热门文章
linux 常见服务端口
xmanager 2.0 for linux配置
【ROOTFS搭建】busybox的httpd...
openwrt中luci学习笔记
什么是shell
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
epoll网上g一大把, 就不详细叙述了.
推荐几篇好文章:
epoll精髓
epoll相关资料整理
epoll LT VS ET
epoll(7) - Linux man page
EPOLL为我们带来了什么
[cpp] view plaincopyprint?
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <poll.h>
#include <sys/types.h>
#include <error.h>
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 <sys/epoll.h>
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 =
// available for read operations
EPOLLIN | EPOLLPRI
// available for write operations
// | EPOLLOUT
// Error condition && Hang up happened
| EPOLLERR | EPOLLHUP
// Sets the Edge Triggered behaviour
| EPOLLET
// Sets the one-shot behaviour.
// must call epoll_ctl with EPOLL_CTL_MOD to re-enable
| 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 );
//
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;
}
阅读(274) | 评论(0) | 转发(0) |
0
上一篇:C++成员函数指针的应用
下一篇:C++中回调(CallBack)的使用方法
相关热门文章
linux 常见服务端口
xmanager 2.0 for linux配置
【ROOTFS搭建】busybox的httpd...
openwrt中luci学习笔记
什么是shell
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- C++中回调(CallBack)的使用方法
- 偷Microsoft师学MFC艺:且看C++如何支持反射
- 简单的C++委托 —— 用模板类实现类成员函数的回调
- svnserve.conf:12: Option expected的问题解决方法[SVN]
- vi显示行号
- Linux命令[常用备忘]
- Java I/O学习笔记(1),同步阻塞式IO
- linux架设Apache + svn版本控制(强力推荐)
- Linux的SVN服务器升级1.4至1.6[linux svn 卸载安装]
- 浅谈 linux 多线程编程和 windows 多线程编程的异同
- SVN 上传. a 静态链接库 .o 编译后文件等问题
- Cygwin in Visual Studio
- Android 中沉浸式状态栏实现
- SVN Server安装部署攻略(Linux+SubVersion+Apache)
- 【转载】java class运行时热替换(hotswap)
- error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '
- libcpmt.lib(cerr.obj) : error LNK2001: unresolved external symbol @__security_ch
- VS2010生成exe在别的机子上运行提示“丢失MSVCR100D.dll”
- 【转】无缝世界网游服务器架构的设计思路
- error LNK2005: _DllMain@12 already defined in MSVCRTD.lib