Document directory
- 1. Usage
- 2. kernel implementation
Http://blog.csdn.net/walkingman321/article/details/6162055
Timerfd is a timer interface provided by Linux for user programs. This interface is based on file descriptors, so it can be used in select/poll application scenarios.
1. Usage
Timerfd provides the following interfaces for users to use.
Timerfd_create
Int timerfd_create (INT clockid, int flags );
Timerfd_create is used to create a timer file.
The clockid parameter can be clock_monotonic or clock_realtime.
The flags parameter can be 0 or o_cloexec/o_nonblock.
The function returns a file handle FD.
Timerfd_settime
Int timerfd_settime (int ufd, int flags, const struct itimerspec * utmr, struct itimerspec * otmr );
This function is used to set a new timeout and start timing.
The UFD parameter is the file handle returned by timerfd_create.
If the flags parameter is set to 1, the absolute time is set. If the flags parameter is set to 0, the relative time is set.
The utmr parameter is the time to be set.
The otmr parameter specifies the time-out period before the timer is set.
If the function returns 0, the setting is successful.
Timerfd_gettime
Int timerfd_gettime (int ufd, struct itimerspec * otmr );
This function is used to obtain the remaining time of the timer from the next timeout. If the timer has expired and the timer is in cyclic mode (struct itimerspec: it_interval is not 0 when the timeout time is set), call this function and start the timer again.
Read
When timerfd is blocked, the READ function is blocked until the timer times out.
If the return value of the function is greater than 0, it indicates that the timer times out. Otherwise, it indicates that there is no time-out (wake up by the signal, etc ).
Poll/close
Poll, close is the same as standard file operations.
2. kernel implementation
The kernel implementation code of timerfd is in kernel/fs/timerfd. C, which is based on Linux hrtimer.
Implementation of timerfd_create
Syscall_define2 (timerfd_create, Int, clockid, Int, flags)
L initialize the timer.
L call hrtimer_init to initialize a hrtimer.
L call anon_inode_getfd to allocate a dentry and get a file number FD. Meanwhile, input the timerfd file operation pointer struct file_operations timerfd_fops. Anno_inode_getfd is a help function of File System anon_inodefs. The anon file system is relatively simple. The entire file system has only one inode node. Its implementation code can be found in FS/anon_inodes.c.
Implementation of timerfd_settime
Timerfd_settime will eventually call hrtimer_start to start the timer, and its timeout function is set to timerfd_tmrproc.
Timerfd_tmrproc
Timefd_tmrproc is the timer timeout function of timerfd. When timerfd times out, this function sets a timer timeout flag to increase the number of times the timer times out (when the timer Cycle Mode is set, there may be multiple times of timeout not processed ); wake up a waiting queue to wake up possible blocked read and select statements.
Timerfd_fops
Static const struct file_operations timerfd_fops = {
. Release = timerfd_release,
. Poll = timerfd_poll,
. Read = timerfd_read,
};
The timerfd_read function is the kernel Implementation of the file operation read, and reads the number of times the timer times out. In blocking mode, this function will mount itself to the wait queue of timerfd and wake up when the timer times out.
Timerfd_poll registers the wait queue of timerfd to a poll_table to wake up the Select system call when the timer times out.
Timerfd_release
The timerfd_release function releases the resources applied in the timerfd_create function and deletes the allocated timer.