函數原型:?創建的時候可以傳入一個計數器的初始值initval。 第二個參數flags在linux 2.6.26之前的版本是沒有使用的,必須初始化為0,在2.6.27之后的版本flag才被使用。
#include <sys/eventfd.h>
int eventfd(unsigned int initval, int flags);
分析:
flags 可以是以下值的 OR 運算結果,用以改變 eventfd 的行為。
- EFD_CLOEXEC (since Linux 2.6.27) 文件被設置成 O_CLOEXEC,創建子進程 (fork) 時不繼承父進程的文件描述符。
- EFD_NONBLOCK (since Linux 2.6.27) 文件被設置成 O_NONBLOCK,執行 read / write 操作時,不會阻塞。
- EFD_SEMAPHORE (since Linux 2.6.30) 提供類似信號量語義的 read 操作,簡單說就是計數值 count 遞減 1。
在 Linux 2.6.26 版本之前,沒有使用參數 flags,必須指定為 0。
操作方法
read: 讀取計數器中的值
- 如果計數器中的值大于0
- 設置了EFD_SEMAPHORE標志位,則返回1,且計數器中的值也減去1。
- 沒有設置EFD_SEMAPHORE標志位,則返回計數器中的值,且計數器置0。
- 如果計數器中的值為0
- 設置了EFD_NONBLOCK標志位就直接返回-1。
- 沒有設置EFD_NONBLOCK標志位就會一直阻塞直到計數器中的值大于0。
write: 向計數器中寫入值
- 如果寫入值的和小于0xFFFFFFFFFFFFFFFE,則寫入成功
- 如果寫入值的和大于0xFFFFFFFFFFFFFFFE
- 設置了EFD_NONBLOCK標志位就直接返回-1。
- 如果沒有設置EFD_NONBLOCK標志位,則會一直阻塞直到read操作執行
close: 關閉文件描述符,eventfd 對象引用計數減 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");}
}
輸出結果:
?
?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");}
}
輸出結果:
?
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");}
}
輸出結果:
參考資料:
- Linux進程間通信-eventf
- 通過實例來理解 eventfd 函數機制
- ?
。