进程的时候会复制这个句柄到新的进程,并继承所有的状态。
(ps:也就是说,在write之后没有read,但是又write新的数据,那么读取的是这两次的8个字节的和,在read之后再write,可以完成read和write之间的交互)
一个例子:
#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; }
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.