eventfd(一)
生活随笔
收集整理的這篇文章主要介紹了
eventfd(一)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
函數(shù)原型:?創(chuàng)建的時候可以傳入一個計數(shù)器的初始值initval。 第二個參數(shù)flags在linux 2.6.26之前的版本是沒有使用的,必須初始化為0,在2.6.27之后的版本flag才被使用。
#include <sys/eventfd.h> int eventfd(unsigned int initval, int flags);分析:
flags 可以是以下值的 OR 運算結(jié)果,用以改變 eventfd 的行為。
- EFD_CLOEXEC (since Linux 2.6.27) 文件被設置成 O_CLOEXEC,創(chuàng)建子進程 (fork) 時不繼承父進程的文件描述符。
- EFD_NONBLOCK (since Linux 2.6.27) 文件被設置成 O_NONBLOCK,執(zhí)行 read / write 操作時,不會阻塞。
- EFD_SEMAPHORE (since Linux 2.6.30) 提供類似信號量語義的 read 操作,簡單說就是計數(shù)值 count 遞減 1。
在 Linux 2.6.26 版本之前,沒有使用參數(shù) flags,必須指定為 0。
操作方法
read: 讀取計數(shù)器中的值
- 如果計數(shù)器中的值大于0
- 設置了EFD_SEMAPHORE標志位,則返回1,且計數(shù)器中的值也減去1。
- 沒有設置EFD_SEMAPHORE標志位,則返回計數(shù)器中的值,且計數(shù)器置0。
- 如果計數(shù)器中的值為0
- 設置了EFD_NONBLOCK標志位就直接返回-1。
- 沒有設置EFD_NONBLOCK標志位就會一直阻塞直到計數(shù)器中的值大于0。
write: 向計數(shù)器中寫入值
- 如果寫入值的和小于0xFFFFFFFFFFFFFFFE,則寫入成功
- 如果寫入值的和大于0xFFFFFFFFFFFFFFFE
- 設置了EFD_NONBLOCK標志位就直接返回-1。
- 如果沒有設置EFD_NONBLOCK標志位,則會一直阻塞直到read操作執(zhí)行
close: 關(guān)閉文件描述符,eventfd 對象引用計數(shù)減 1,若減為 0,則釋放 eventfd 對象資源。
測試代碼:
#include <sys/eventfd.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h>#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 <num>...\n", argv[0]);exit(EXIT_FAILURE);}efd = eventfd(0, 0);if (efd == -1)handle_error("eventfd");switch (fork()){case 0:for (j = 1; j < argc; j++){printf("Child writing %s to efd\n", argv[j]);u = atoi(argv[j]);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:sleep(2);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 (0x%llx) from efd\n",(unsigned long long) u, (unsigned long long) u);exit(EXIT_SUCCESS);case -1:handle_error("fork");} }輸出結(jié)果:
?
?2. 測試代碼:
#include <sys/eventfd.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h>#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 <num>...\n", argv[0]);exit(EXIT_FAILURE);}efd = eventfd(0, EFD_NONBLOCK);if (efd == -1)handle_error("eventfd");switch (fork()){case 0:for (j = 1; j < argc; j++){printf("Child writing %s to efd\n", argv[j]);}printf("Child completed write loop\n");exit(EXIT_SUCCESS);default:sleep(2);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 (0x%llx) from efd\n",(unsigned long long) u, (unsigned long long) u);exit(EXIT_SUCCESS);case -1:handle_error("fork");} }輸出結(jié)果:
?
3.?
#include <sys/eventfd.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h>#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 <num>...\n", argv[0]);exit(EXIT_FAILURE);}efd = eventfd(0, EFD_SEMAPHORE);if (efd == -1)handle_error("eventfd");switch (fork()){case 0:for (j = 1; j < argc; j++){printf("Child writing %s to efd\n", argv[j]);u = atoi(argv[j]);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:sleep(2);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 (0x%llx) from efd\n",(unsigned long long) u, (unsigned long long) u);exit(EXIT_SUCCESS);case -1:handle_error("fork");} }輸出結(jié)果:
參考資料:
- Linux進程間通信-eventf
- 通過實例來理解 eventfd 函數(shù)機制
- ?
。
總結(jié)
以上是生活随笔為你收集整理的eventfd(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成都欢乐谷日场票可以玩到晚上吗
- 下一篇: 阴道炎会引起不孕症吗