Last active
August 15, 2023 15:42
-
-
Save usvi/556906ef7764a6d8b2bda5853e3a06e2 to your computer and use it in GitHub Desktop.
Epoll test with arbitrary event data fd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <semaphore.h> | |
#include <pthread.h> | |
#include <sys/epoll.h> | |
// gcc -Wall epoll_test.c -o epoll_test -lpthread | |
// ./epoll_test | |
#define PIPE_READ (0) | |
#define PIPE_WRITE (1) | |
#define MAP_TO_FD (123) | |
sem_t gx_main_sem; | |
int gai_pipe[2]; | |
static void* pvThreadBody(void* pv_arg_data) | |
{ | |
int i_epoll_fd = -1; | |
int i_wait_ret = -1; | |
struct epoll_event x_epoll_temp_event; | |
i_epoll_fd = epoll_create1(0); | |
memset(&x_epoll_temp_event, 0, sizeof(struct epoll_event)); | |
x_epoll_temp_event.events = EPOLLIN; | |
x_epoll_temp_event.data.fd = MAP_TO_FD; | |
printf("Doing epoll_ctl with arg fd=%d and event data fd=%d\n", | |
gai_pipe[PIPE_READ], x_epoll_temp_event.data.fd); | |
epoll_ctl(i_epoll_fd, EPOLL_CTL_ADD, gai_pipe[PIPE_READ], &x_epoll_temp_event); | |
memset(&x_epoll_temp_event, 0, sizeof(struct epoll_event)); | |
sem_post(&gx_main_sem); | |
i_wait_ret = epoll_wait(i_epoll_fd, &x_epoll_temp_event, 1, -1); | |
printf("Epoll wait result %d, event data fd=%d\n", | |
i_wait_ret, x_epoll_temp_event.data.fd); | |
return NULL; | |
} | |
int main() | |
{ | |
pthread_t x_evl_thread; | |
sem_init(&gx_main_sem, 0, 0); | |
pipe(gai_pipe); | |
pthread_create(&x_evl_thread, NULL, pvThreadBody, NULL); | |
sem_wait(&gx_main_sem); | |
printf("Writing to pipe, write fd=%d, read fd=%d\n", | |
gai_pipe[PIPE_WRITE], gai_pipe[PIPE_READ]); | |
write(gai_pipe[PIPE_WRITE], "Hello\n", strlen("Hello\n")); | |
pthread_join(x_evl_thread, NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment