如何解决频繁 copy 的问题 → mmap 内存映射 解决。 submit queue 中的节点和 complete queue 中的节点共用一块内存,而不是把 submit queue 中的节点 copy 到 complete queue 中 。 如何做到线程安全 → 无锁环形队列 解决。
三、liburing 安装
git clone https://github.com/axboe/liburing.git
cd liburing
./configure
make
sudo make install
io_uring __tcp_server_27">四、io_uring 实现 tcp server
io_uring 的 EVENT_READ
表示数据已经读出来了 。epoll 的 EPOLLIN
表示数据可以读了 。
# include <stdio.h>
# include <liburing.h>
# include <netinet/in.h>
# include <string.h>
# include <unistd.h> # define EVENT_ACCEPT 0
# define EVENT_READ 1
# define EVENT_WRITE 2 struct conn_info { int fd; int event;
} ;
int init_server ( unsigned short port) { int sockfd = socket ( AF_INET, SOCK_STREAM, 0 ) ; struct sockaddr_in serveraddr; memset ( & serveraddr, 0 , sizeof ( struct sockaddr_in ) ) ; serveraddr. sin_family = AF_INET; serveraddr. sin_addr. s_addr = htonl ( INADDR_ANY) ; serveraddr. sin_port = htons ( port) ; if ( - 1 == bind ( sockfd, ( struct sockaddr * ) & serveraddr, sizeof ( struct sockaddr ) ) ) { perror ( "bind" ) ; return - 1 ; } listen ( sockfd, 10 ) ; return sockfd;
} # define ENTRIES_LENGTH 1024
# define BUFFER_LENGTH 1024 int set_event_recv ( struct io_uring * ring, int connfd, void * buf, size_t len, int flags) { struct io_uring _sqe * sqe = io_uring _get_sqe( ring) ; struct conn_info accept_info = { . fd = connfd, . event = EVENT_READ, } ; io_uring _prep_recv( sqe, connfd, buf, len, flags) ; memcpy ( & sqe-> user_data, & accept_info, sizeof ( struct conn_info ) ) ;
} int set_event_send ( struct io_uring * ring, int connfd, void * buf, size_t len, int flags) { struct io_uring _sqe * sqe