[root@localhost appTest]# ./timerfd 5 2 10
printTime: current time:1357391736.146196 timer started
printTime: current time:1357391741.153430 read: 1; total=1
printTime: current time:1357391743.146550 read: 1; total=2
printTime: current time:1357391745.151483 read: 1; total=3
printTime: current time:1357391747.161155 read: 1; total=4
printTime: current time:1357391749.153934 read: 1; total=5
printTime: current time:1357391751.157309 read: 1; total=6
printTime: current time:1357391753.158384 read: 1; total=7
printTime: current time:1357391755.150470 read: 1; total=8
printTime: current time:1357391757.150253 read: 1; total=9
printTime: current time:1357391759.149954 read: 1; total=10
[root@localhost appTest]#
第一个参数5为第一次定时器到期间隔,第二个参数2为定时器的间隔,第三个参数为定时器到期10次则退出。程序运行(5+2*10)S退出。
详细信息可以:man timerfd_create
eventfd涉及API:
点击(此处)折叠或打开
-
#include
-
int eventfd(unsigned int initval, int flags);
创建一个eventfd,这是一个计数器相关的fd,计数器不为零是有可读事件发生,read以后计数器清零,write递增计数器;返回的fd可以进行如下操作:read、write、select(poll、epoll)、close。
这个函数会创建一个 事件对象 (eventfd object), 用来实现,进程(线程)间的等待/通知(wait/notify) 机制. 内核会为这个对象维护一个64位的计数器(uint64_t)。并且使用第一个参数(initval)初始化这个计数器。调用这个函数就会返回一个新的文件描述符(event object)。2.6.27版本开始可以按位设置第二个参数(flags)。有如下的一些宏可以使用:
lEFD_NONBLOCK
功能同open(2)的O_NONBLOCK,设置对象为非阻塞状态,如果没有设置这个状态的话,read(2)读eventfd,并且计数器的值为0 就一直堵塞在read调用当中,要是设置了这个标志, 就会返回一个 EAGAIN 错误(errno = EAGAIN)。效果也如同 额外调用select(2)达到的效果。
lEFD_CLOEXEC
这个标识被设置的话,调用exec后会自动关闭文件描述符,防止泄漏。如果是2.6.26或之前版本的内核,flags 必须设置为0。
创建这个对象后,可以对其做如下操作:
1) write: 将缓冲区写入的8字节整形值加到内核计数器上。
2) read: 读取8字节值, 并把计数器重设为0. 如果调用read的时候计数器为0, 要是eventfd是阻塞的, read就一直阻塞在这里,否则就得到 一个EAGAIN错误。如果buffer的长度小于8那么read会失败, 错误代码被设置成 EINVAL。
3) poll select epoll
4) close: 当不需要eventfd的时候可以调用close关闭, 当这个对象的所有句柄都被关闭的时候,内核会释放资源。 为什么不是close就直接释放呢, 如果调用fork 创建
进程的时候会复制这个句柄到新的进程,并继承所有的状态。
l例子
点击(此处)折叠或打开
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#define handle_error(msg)
-
do { perror(msg); exit(1); } while (0)
-
int main( int argc, char **argv ){
-
uint64_t u;
-
ssize_t s;5 int j;
-
if ( argc < 2 ) {
-
fprintf(stderr, "input in command argument");
-
exit(1);
-
}
-
-
int efd;
-
if ( (efd = eventfd(0, EFD_NONBLOCK)) == -1 )
-
handle_error("eventfd failed");
-
-
-
switch (fork()) {
-
case 0:
-
for( j = 1; j < argc; j ++ ) {
-
printf("Child writing %s to efdn", argv[j] );
-
-
u = strtoull(argv[j], NULL, 0); /* analogesly atoi */
-
s = write(efd, &u, sizeof(uint64_t));/*append u to counter */
-
if ( s != sizeof(uint64_t) )
-
handle_error("write efd failed");
-
-
}
-
printf("child completed write loopn");
-
-
exit(0);
-
default:
-
sleep (2);
-
-
printf("parent about to readn");
-
s = read(efd, &u, sizeof(uint64_t));
-
if ( s != sizeof(uint64_t) ) {
-
if (errno = EAGAIN) {
-
printf("Parent read value %dn", s);
-
return 1;
-
}
-
handle_error("parent read failed");
-
}
-
printf("parent read %d , %llu (0x%llx) from efdn",
-
s, (unsigned long long)u, (unsigned long long) u);
-
exit(0);
-
-
case -1:
-
handle_error("fork ");
-
}
-
return 0;
-
}
(编辑:应用网_丽江站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|