日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

eventfd(三)

發(fā)布時間:2023/11/30 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 eventfd(三) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. 測試代碼:?

//https://www.jianshu.com/p/d7ebac8dc9f8 #include <stdio.h> #include <unistd.h> #include <stdint.h> #include <pthread.h> #include <sys/eventfd.h> #include <sys/epoll.h>int event_fd = -1;void *read_thread(void *dummy) {uint64_t inc = 1;int ret = 0;int i = 0;for (; i < 2; i++) {ret = write(event_fd, &inc, sizeof(uint64_t));if (ret < 0) {perror("child thread write event_fd fail.");} else {printf("child thread completed write %llu (0x%llx) to event_fd\n", (unsigned long long) inc, (unsigned long long) inc);}sleep(4);} }int main(int argc, char *argv[]) {int ret = 0;pthread_t pid = 0;event_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);if (event_fd < 0) {perror("event_fd create fail.");}ret = pthread_create(&pid, NULL, read_thread, NULL);if (ret < 0) {perror("pthread create fail.");}uint64_t counter;int epoll_fd = -1;struct epoll_event events[16];if (event_fd < 0){printf("event_fd not inited.\n");}epoll_fd = epoll_create(8);if (epoll_fd < 0){perror("epoll_create fail:");}struct epoll_event read_event;read_event.events = EPOLLIN;read_event.data.fd = event_fd;ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, event_fd, &read_event);if (ret < 0) {perror("epoll_ctl failed:");}while (1) {printf("main thread epoll is waiting......\n");ret = epoll_wait(epoll_fd, events, 16, 2000);printf("main thread epoll_wait return ret : %d\n", ret);if (ret > 0) {int i = 0;for (; i < ret; i++) {int fd = events[i].data.fd;if (fd == event_fd) {uint32_t epollEvents = events[i].events;if (epollEvents & EPOLLIN) {ret = read(event_fd, &counter, sizeof(uint64_t));if (ret < 0) {printf("main thread read fail\n");} else {printf("main thread read %llu (0x%llx) from event_fd\n", (unsigned long long) counter, (unsigned long long) counter);}} else {printf("main thread unexpected epoll events on event_fd\n");}}}} else if (ret == 0) {printf("main thread epoll_wait timed out. continue epoll\n");} else {perror("main thread epoll_wait error.");}} }

輸出結(jié)果:

wufan@Frank-Linux:~/Linux/test$ ./epoll_eventfd main thread epoll is waiting...... // main 線程阻塞在讀端 child thread completed write 1 (0x1) to event_fd // 第一次寫入后阻塞 4 秒 main thread epoll_wait return ret : 1 // 第一次寫完后,立即喚醒 main 線程去進行讀操作 main thread read 1 (0x1) from event_fd // main 線程讀到了數(shù)據(jù) main thread epoll is waiting...... // main 線程阻塞又在讀端,超時時間為 2 秒 main thread epoll_wait return ret : 0 // main 線程阻塞等待時間到,返回 main thread epoll_wait timed out. continue epoll main thread epoll is waiting...... // main 線程阻塞又在讀端,超時時間為 2 秒 child thread completed write 1 (0x1) to event_fd // // 第二次寫入后阻塞 4 秒 main thread epoll_wait return ret : 1 // 第二次寫完后,立即喚醒 main 線程去進行讀操作 main thread read 1 (0x1) from event_fd // main 線程讀到了數(shù)據(jù) main thread epoll is waiting...... // main 線程阻塞又在讀端,超時時間為 2 秒 main thread epoll_wait return ret : 0 // main 線程阻塞等待時間到,返回 main thread epoll_wait timed out. continue epoll main thread epoll is waiting...... // main 線程阻塞又在讀端,超時時間為 2 秒 只要沒有寫入數(shù)據(jù),就會在這個死循環(huán)中阻塞 -> 超時 -> 阻塞...

?

總結(jié)

以上是生活随笔為你收集整理的eventfd(三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。