三种新的fd参加linux内核的的版别:
signalfd:2.6.22
timerfd:2.6.25
eventfd:2.6.22
三种fd的含义:
lsignalfd
传统的处理信号的方法是注册信号处理函数;因为信号是异步产生的,要处理数据的并发拜访,可重入问题。signalfd能够将信号笼统为一个文件描述符,当有信号产生时能够对其read,这样能够将信号的监听放到select、poll、epoll等监听行列中。
lTImerfd
能够完成守时器的功用,将守时器笼统为文件描述符,当守时器到期时能够对其read,这样也能够放到监听行列的主循环中。
leventfd
完成了线程之间事情告诉的方法,也能够用于用户态和内核通讯。eventfd的缓冲区巨细是sizeof(uint64_t);向其write能够递加这个计数器,read操作能够读取,并进行清零;eventfd也能够放到监听行列中,当计数器不是0时,有可读事情产生,能够进行读取。
三种新的fd都能够进行监听,当有事情触发时,有可读事情产生。
signalfd触及API:
点击(此处)折叠或翻开
#include int signalfd(int fd, const sigset_t *mask, int flags);
1
2
#include
int signalfd(int fd, const sigset_t *mask, int flags);
参数fd:假如是-1则表明新建一个,假如是一个现已存在的则表明修正signalfd所相关的信号;
参数mask:信号调集;
参数flag:内核版别2.6.27今后支撑SFD_NONBLOCK、SFD_CLOEXEC;
成功回来文件描述符,回来的fd支撑以下操作:read、select(poll、epoll)、close
l比方
#include #include #include #include #include #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { sigset_t mask; int sfd; struct signalfd_siginfo fdsi; ssize_t s; sigemptyset(&mask); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGQUIT); if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) handle_error(“sigprocmask”); sfd = signalfd(-1, &mask, 0); if (sfd == -1) handle_error(“signalfd”); for (;;) { s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo)); if (s != sizeof(struct signalfd_siginfo)) handle_error(“read”); if (fdsi.ssi_signo == SIGINT) { printf(“Got SIGINT\n”); } else if (fdsi.ssi_signo == SIGQUIT) { printf(“Got SIGQUIT\n”); exit(EXIT_SUCCESS); } else { printf(“Read unexpected signal\n”); } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include
#include
#include
#include
#include
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[])
{
sigset_t mask;
int sfd;
struct signalfd_siginfo fdsi;
ssize_t s;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
handle_error(“sigprocmask”);
sfd = signalfd(-1, &mask, 0);
if (sfd == -1)
handle_error(“signalfd”);
for (;;) {
s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
if (s != sizeof(struct signalfd_siginfo))
handle_error(“read”);
if (fdsi.ssi_signo == SIGINT) {
printf(“Got SIGINT\n”);
} else if (fdsi.ssi_signo == SIGQUIT) {
printf(“Got SIGQUIT\n”);
exit(EXIT_SUCCESS);
} else {
printf(“Read unexpected signal\n”);
}
}
}
L17-L21:将感兴趣的信号参加到sigset_t中;
L24:调用signalfd,把信号集与fd相关起来,第一个参数为-1表明新建一个signalfd,不是-1而且是一个合法的signalfd表明向其增加新的信号。
L29:堵塞等候信号的产生并读取。依据读取的成果能够知道产生了什么信号。
TImerfd触及的API
#include 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);
1
2
3
4
#include
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);
timerfd_create:创立一个timerfd;回来的fd能够进行如下操作:read、select(poll、epoll)、close
timerfd_settime:设置timer的周期,以及开端距离
timerfd_gettime:获取到期时刻。
//函数参数中数据结构如下: struct timespec { time_t tv_sec; /* Seconds */ long tv_nsec; /* Nanoseconds */ }; struct itimerspec { struct timespec it_interval; /* Interval for periodic timer */ struct timespec it_value; /* Initial expiration */ };
1
2
3
4
5
6
7
8
9
10
11
//函数参数中数据结构如下:
struct timespec
{
time_t tv_sec; /* Seconds */
long tv_nsec; /* Nanoseconds */
};
struct itimerspec
{
struct timespec it_interval; /* Interval for periodic timer */
struct timespec it_value; /* Initial expiration */
};
l比方
#include #include #include #include #include #include #include /* 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\n”); 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\n",exp, tot_exp); } exit(EXIT_SUCCESS); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include
#include
#include
#include
#include
#include
#include /* 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\n”);
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\n”,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 int eventfd(unsigned int initval, int flags);
1
2
#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 efd\n", 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\n"); exit(0); default: sleep (2); printf("parent about to read\n"); s = read(efd, &u, sizeof(uint64_t)); if ( s != sizeof(uint64_t) ) { if (errno = EAGAIN) { printf("Parent read value %d\n", s); return 1; } handle_error("parent read failed"); } printf("parent read %d , %llu (0x%llx) from efd\n", s, (unsigned long long)u, (unsigned long long) u); exit(0); case -1: handle_error("fork "); } return 0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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 efd\n”, 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\n”);
exit(0);
default:
sleep (2);
printf(“parent about to read\n”);
s = read(efd, &u, sizeof(uint64_t));
if ( s != sizeof(uint64_t) ) {
if (errno = EAGAIN) {
printf(“Parent read value %d\n”, s);
return 1;
}
handle_error(“parent read failed”);
}
printf(“parent read %d , %llu (0x%llx) from efd\n”,
s, (unsigned long long)u, (unsigned long long) u);
exit(0);
case -1:
handle_error(“fork “);
}
return 0;
}