这两天公司代码中用到了epoll。然后在跟同事闲扯的过程中发现了Linux中有eventfd。两者虽然名字看起来差不多,但是相关性倒是不多。
为了弄明白这两个东西到底在内核上是怎么实现的,这两天将内核这两个部分的相关代码看了下,也终于明白了这两个东西的实现机制。
后续几篇博客我尽量将这两个东西的工作原理阐述清楚,但是自己的语言表达能力比较差,也只能是尽量了。
今天这篇博客首先是介绍两者的使用方式,恰巧两者能够在一个程序中搞定,我就写了下面的小程序来展示两者的功能。
- #include <stdio.h>
-
#include <unistd.h>
-
#include <sys/time.h>
-
#include <stdint.h>
-
#include <pthread.h>
-
#include <sys/eventfd.h>
-
#include <sys/epoll.h>
-
-
int efd = -1;
-
-
void *read_thread(void *dummy)
-
{
-
int ret = 0;
-
uint64_t count = 0;
-
int ep_fd = -1;
-
struct epoll_event events[10];
-
-
if (efd < 0)
-
{
-
printf("efd not inited.\n");
-
goto fail;
-
}
-
-
ep_fd = epoll_create(1024);
-
if (ep_fd < 0)
-
{
-
perror("epoll_create fail: ");
-
goto fail;
-
}
-
-
{
-
struct epoll_event read_event;
-
-
read_event.events = EPOLLHUP | EPOLLERR | EPOLLIN;
-
read_event.data.fd = efd;
-
-
ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, efd, &read_event);
-
if (ret < 0)
-
{
-
perror("epoll ctl failed:");
-
goto fail;
-
}
-
}
-
-
while (1)
-
{
-
ret = epoll_wait(ep_fd, &events[0], 10, 5000);
-
if (ret > 0)
-
{
-
int i = 0;
-
for (; i < ret; i++)
-
{
-
if (events[i].events & EPOLLHUP)
-
{
-
printf("epoll eventfd has epoll hup.\n");
-
goto fail;
-
}
-
else if (events[i].events & EPOLLERR)
-
{
-
printf("epoll eventfd has epoll error.\n");
-
goto fail;
-
}
-
else if (events[i].events & EPOLLIN)
-
{
-
int event_fd = events[i].data.fd;
-
ret = read(event_fd, &count, sizeof(count));
-
if (ret < 0)
-
{
-
perror("read fail:");
-
goto fail;
-
}
-
else
-
{
-
struct timeval tv;
-
-
gettimeofday(&tv, NULL);
-
printf("success read from efd, read %d bytes(%llu) at %lds %ldus\n",
-
ret, count, tv.tv_sec, tv.tv_usec);
-
}
-
}
-
}
-
}
-
else if (ret == 0)
-
{
-
/* time out */
-
printf("epoll wait timed out.\n");
-
break;
-
}
-
else
-
{
-
perror("epoll wait error:");
-
goto fail;
-
}
-
}
-
-
fail:
-
if (ep_fd >= 0)
-
{
-
close(ep_fd);
-
ep_fd = -1;
-
}
-
-
return NULL;
-
}
-
-
int main(int argc, char *argv[])
-
{
-
pthread_t pid = 0;
-
uint64_t count = 0;
-
int ret = 0;
-
int i = 0;
-
-
efd = eventfd(0, 0);
-
if (efd < 0)
-
{
-
perror("eventfd failed.");
-
goto fail;
-
}
-
-
ret = pthread_create(&pid, NULL, read_thread, NULL);
-
if (ret < 0)
-
{
-
perror("pthread create:");
-
goto fail;
-
}
-
-
for (i = 0; i < 5; i++)
-
{
-
count = 4;
-
ret = write(efd, &count, sizeof(count));
-
if (ret < 0)
-
{
-
perror("write event fd fail:");
-
goto fail;
-
}
-
else
-
{
-
struct timeval tv;
-
-
gettimeofday(&tv, NULL);
-
printf("success write to efd, write %d bytes(%llu) at %lds %ldus\n",
-
ret, count, tv.tv_sec, tv.tv_usec);
-
}
-
-
sleep(1);
-
}
-
-
fail:
-
if (0 != pid)
-
{
-
pthread_join(pid, NULL);
-
pid = 0;
-
}
-
-
if (efd >= 0)
-
{
-
close(efd);
-
efd = -1;
-
}
-
return ret;
-
}
- gcc main.c -Werror -Wall -lpthread
最后执行效果为
- success write to efd, write 8 bytes(4) at 1328805612s 21939us
-
success read from efd, read 8 bytes(4) at 1328805612s 21997us
-
success write to efd, write 8 bytes(4) at 1328805613s 22247us
-
success read from efd, read 8 bytes(4) at 1328805613s 22287us
-
success write to efd, write 8 bytes(4) at 1328805614s 22462us
-
success read from efd, read 8 bytes(4) at 1328805614s 22503us
-
success write to efd, write 8 bytes(4) at 1328805615s 22688us
-
success read from efd, read 8 bytes(4) at 1328805615s 22726us
-
success write to efd, write 8 bytes(4) at 1328805616s 22973us
-
success read from efd, read 8 bytes(4) at 1328805616s 23007us
-
epoll wait timed out.
eventfd具体与pipe有点像,用来完成两个线程之间事件触发,但是同事说现在已经支持到进程级别,现在我还没有验证过。能够用来作为线程之间简单通讯,类似于pthread_cond_t。
epoll则是linux提供的一种多路复用技术,完成与select,poll等一样的功能,完成对多个文件描述符进行等待。本文上述代码仅仅用到了一个文件描述符。
epoll比select的优势网络随处可见,这就不多说了。
应用程序就已经写到这了,对于内核里面具体是怎么搞的,将会在以后的博客中尽量解释清楚。
阅读(296) | 评论(0) | 转发(0) |