????????
#include <sys/timerfd.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h> /* Definition of uint64_t */
#define handle_error(msg)
do { perror(msg); exit(EXIT_FAILURE); } while (0)
void printTime()
{
struct timeval tv;
gettimeofday(&tv?? NULL);
printf("printTime: current time:%ld.%ld "?? tv.tv_sec?? tv.tv_usec);
}
int main(int argc?? char *argv[])
{
struct timespec now;
if (clock_gettime(CLOCK_REALTIME?? &now) == -1)
handle_error("clock_gettime");
struct itimerspec new_value;
new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
new_value.it_value.tv_nsec = now.tv_nsec;
new_value.it_interval.tv_sec = atoi(argv[2]);
new_value.it_interval.tv_nsec = 0;
int fd = timerfd_create(CLOCK_REALTIME?? 0);
if (fd == -1)
handle_error("timerfd_create");
if (timerfd_settime(fd?? TFD_TIMER_ABSTIME?? &new_value?? NULL) == -1)
handle_error("timerfd_settime");
printTime();
printf("timer started ");
for (uint64_t tot_exp = 0; tot_exp < atoi(argv[3]);)
{
uint64_t exp;
ssize_t s = read(fd?? &exp?? sizeof(uint64_t));
if (s != sizeof(uint64_t))
handle_error("read");
tot_exp += exp;
printTime();
printf("read: %llu; total=%llu "??exp?? tot_exp);
}
exit(EXIT_SUCCESS);
}
????????L25-L29????????????????????????????????
????L32???????????fd??CLOCK_REALTIME????????????????????????????CLOCK_MONOTONIC????????????????????????????
????L35???????????????
????L44??????????????????????????δ????????????????綨?????2????????10??????????????????5??
???????????У?????????rt??(g++ -lrt timerfd.cc -o timerfd)
????[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 <sys/eventfd.h>
????int eventfd(unsigned int initval?? int flags);
???????????eventfd???????????????????fd????????????????п???????????read????????????write?????????????????fd??????????2?????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??
????????????????????????????2?????
????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 ????
????????????????????????μ?????????????е?????
????????
#include <sys/eventfd.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#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 efd "?? 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 loop ");
exit(0);
default:
sleep (2);
printf("parent about to read ");
s = read(efd?? &u?? sizeof(uint64_t));
if ( s != sizeof(uint64_t) ) {
if (errno = EAGAIN) {
printf("Parent read value %d "?? s);
return 1;
}
handle_error("parent read failed");
}
printf("parent read %d ?? %llu (0x%llx) from efd "??
s?? (unsigned long long)u?? (unsigned long long) u);
exit(0);
case -1:
handle_error("fork ");
}
return 0;
}