Linux 自 2.6 开始,可以使用timerfd来管理定时器
提供的基于文件描述符的定时器接口
#include <sys/timerfd.h>
int timerfd_create(int clockid, int flags);
int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *old_value);
int timerfd_gettime(int fd, struct itimerspec *curr_value);
由于基于文件描述符,使得该接口可以支持 select(2),poll(2) 等异步接口,使得定时器的实现和使用更加的方便,更重要的是,支持 fork(2),exec(2) 这样多进程的语义,因此,可以用在多线程环境之中,它们的使用比 POSIX timer [ 2 ]更加的灵活,其根本原因在于定时器的管理统一到了 unix/linux 基本哲学之一 ---- “一切皆文件”之下。
自Linux内核2.6版本起,timerfd提供了一种基于文件描述符的高效定时器管理方式,支持select和poll等异步接口,且兼容fork和exec,适用于多线程环境。该接口遵循一切皆文件的UNIX哲学,使定时器的使用更为灵活。
19

被折叠的 条评论
为什么被折叠?



