Linux 新的事件等待/响应机制eventfd

常用的进程/线程间通信机制有管道、信号量、消息队列、信号、共享内存、socket等等,其中主要作为进程/线程间通知/等待的有管道pipe和socket。从Linux 2.6.27版本开始增加了eventfd,主要用于进程或者线程间的通信(如通知/等待机制的实现)。

注:eventfd用于进程/线程间通信,效率比pipe高

(1) eventfd()函数介绍:创建一个文件描述符efd,用于事件通知,该文件描述符能被用户空间当作一个事件等待/响应机制
(2) efd可以像普通的文件描述符一样,用epoll_wait进行监听:当epoll_wait检测到efd可读时,说明当前线程被其他线程通知notify
(2) efd的全部缓冲区大小只有定长8byte

 *     #include <sys/eventfd.h>
 *     int eventfd(unsigned int initval, int flags);
 * 参数说明:
 *      initval,初始化计数器的值。
 *      flags, EFD_NONBLOCK,设置socket为非阻塞。
 *             EFD_CLOEXEC,执行fork的时候,在父进程中的描述符会自动关闭,子进程中的描述符保留。

先来看看eventfd函数的用法,直接上示例:

[~/test]# ./efd 1 2 3
Parent about to read      父进程,阻塞在read,等待子进程向eventfd中写入数据
Child writing 1 to efd
Child writing 2 to efd
Child writing 3 to efd
Child completed write loop
Parent read 6 (0x6) from efd  父进程,被唤醒,read = 1 + 2 + 3 = 6

#include 
#include 
#include 
#include 
#include    /* Definition of uint64_t */

#define handle_error(msg) \
    do {                        \
        perror(msg);            \
        exit(EXIT_FAILURE);     \
    } while (0)

int
main(int argc, char *argv[])
{
    int efd, j;
    uint64_t u;
    ssize_t s;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s ...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    efd = eventfd(0, 0);  // EFD_CLOEXEC   EFD_NONBLOCK   EFD_SEMAPHORE 
    if (efd == -1)
        handle_error("eventfd");

    switch (fork()) {
    case 0:    // 子进程
        sleep(2);
        for (j = 1; j < argc; j++) {
            printf("Child writing %s to efd\n", argv[j]);
            u = strtoull(argv[j], NULL, 0);
            s = write(efd, &u, sizeof(uint64_t));
            if (s != sizeof(uint64_t))
                handle_error("write");
        }
        printf("Child completed write loop\n");

        exit(EXIT_SUCCESS);
    default:   // 父进程
        while(1)
        {
            printf("Parent about to read\n");
            s = read(efd, &u, sizeof(uint64_t));
            if (s != sizeof(uint64_t))
                handle_error("read");
            printf("Parent read %llu  from efd\n", (unsigned long long) u);
            exit(EXIT_SUCCESS);
        }
    case -1:
        handle_error("fork");
    }
}
#include   
#include   
#include   
#include   
#include   
#include   
#include   

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;  
        
        //将efd添加到epoll中,监听读事件
        ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, efd, &read_event);  
        if (ret < 0)  
        {  
            perror("epoll ctl failed:");  
            goto fail;  
        }  
    }  

    while (1)  
    {  
        //发生阻塞,直到efd可读事件到来
        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); //创建fd用于事件通知  
    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

上述例子,首先使用eventfd创建描述符efd,并在线程里面使用epoll管理这个描述符efd,当在主线程中write时,线程中的epoll返回,描述符可读。

不难发现,通过eventfd创建的描述符efd,读/写大小为sizeof(uint_64)数据,就可以完成两个线程间的唤醒。比如上述例子,由于epoll_wait()的等待,pthread_create出来的线程阻塞,在主线程中,通过往eventfd中write数据,使描述符可读,epoll返回,这就达到了唤醒的目的。

你可能感兴趣的:(Unix环境高级编程(第3版))