From 544420827e20952df190f84856a2ff82b3b2b16c Mon Sep 17 00:00:00 2001 From: fadhil riyanto Date: Fri, 27 Sep 2024 20:06:24 +0700 Subject: add epoll Signed-off-by: fadhil riyanto --- config.h | 6 ++++++ main.c | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 config.h diff --git a/config.h b/config.h new file mode 100644 index 0000000..c5f8568 --- /dev/null +++ b/config.h @@ -0,0 +1,6 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#define MAX_ACCEPT_WORKER 5 + +#endif \ No newline at end of file diff --git a/main.c b/main.c index 53d916a..2a6738f 100644 --- a/main.c +++ b/main.c @@ -9,10 +9,15 @@ #include #include #include +#include #include #include #include #include +#include +#include + +#include "config.h" #define dbgchr(x) log_info("%c", x) @@ -36,8 +41,15 @@ struct runtime_opts { }; +struct epoll_fd_queue { + struct epoll_event *eventmode; + int i; +}; + struct server_ctx { int tcpfd; + int epoll_fd; + struct epoll_fd_queue *epoll_fd_queue; volatile int *need_exit_ptr; }; @@ -134,12 +146,34 @@ static int server_reg_sigaction(void) } + +static void setup_epoll(struct server_ctx *srv_ctx) +{ + srv_ctx->epoll_fd = epoll_create1(EPOLL_CLOEXEC); +} + static int enter_eventloop(struct server_ctx *srv_ctx) { - while(!*srv_ctx->need_exit_ptr) { + setup_epoll(srv_ctx); + struct epoll_fd_queue *epoll_fd_queue = (struct epoll_fd_queue *)malloc( + sizeof(struct epoll_event) * MAX_ACCEPT_WORKER + sizeof(int)); + + srv_ctx->epoll_fd_queue = epoll_fd_queue; + + for(int i = 0; i < MAX_ACCEPT_WORKER; i++) { + if (fork() == 0) { + + _exit(0); + } } + while(wait(NULL) > 0); + + close(srv_ctx->epoll_fd); + free(epoll_fd_queue); + + return 0; } -- cgit v1.2.3