Page MenuHomeFreeBSD
Authored By
vico.chern_qq.com
Sep 4 2023, 3:28 AM
Size
1 KB
Referenced Files
None
Subscribers
None

timerfd.c

#include <signal.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <poll.h>
#include <pthread.h>
int
main(int argc, char **argv)
{
int counter = 0;
int n = 0;
int err = 0;
int tmfd = 0;
struct itimerspec its;
struct pollfd pd;
printf("TFD_CLOEXEC=%x, TFD_NONBLOCK=%x.\n", TFD_CLOEXEC, TFD_NONBLOCK);
tmfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
//tmfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
if (tmfd < 0) {
printf("tmfd=%d\n", tmfd);
return -1;
}
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = 1;
its.it_value.tv_nsec = 1;
err = timerfd_settime(tmfd, 0, &its, NULL);
printf("err=%d...\n", err);
pd.fd = tmfd;
pd.events = POLL_IN;
pd.revents = 0;
do {
pd.revents = 0;
printf("Polling...\n");
n = poll(&pd, 1, -1);
if (n < 0)
continue;
++counter;
printf("Get one: #%d, n=%d, revents=0x%x\n", counter, n, pd.revents);
timerfd_settime(tmfd, 0, &its, NULL);
} while(1);
}

File Metadata

Mime Type
text/x-c
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
6305949
Default Alt Text
timerfd.c (1 KB)

Event Timeline