From 25e092e4b6757e43b6360ea96d692fcd3129fd8d Mon Sep 17 00:00:00 2001 From: wenchao1024 <87457873+wenchao1024@users.noreply.github.com> Date: Mon, 17 Jan 2022 21:20:26 +0800 Subject: [PATCH] =?UTF-8?q?Create=20=E7=A4=BA=E4=BE=8B=E7=A8=8B=E5=BA=8F:?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E9=98=9F=E5=88=97=E8=BD=AE=E8=AF=A2.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- io_uring/示例程序:提交队列轮询.c | 145 +++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 io_uring/示例程序:提交队列轮询.c diff --git a/io_uring/示例程序:提交队列轮询.c b/io_uring/示例程序:提交队列轮询.c new file mode 100644 index 0000000..750bdf4 --- /dev/null +++ b/io_uring/示例程序:提交队列轮询.c @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include + +#define BUF_SIZE 512 +#define FILE_NAME1 "/tmp/io_uring_sq_test.txt" +#define STR1 "What is this life if, full of care,\n" +#define STR2 "We have no time to stand and stare." + +void print_sq_poll_kernel_thread_status() { + + if (system("ps --ppid 2 | grep io_uring-sq" ) == 0) + printf("Kernel thread io_uring-sq found running...\n"); + else + printf("Kernel thread io_uring-sq is not running.\n"); +} + +int start_sq_polling_ops(struct io_uring *ring) { + int fds[2]; + char buff1[BUF_SIZE]; + char buff2[BUF_SIZE]; + char buff3[BUF_SIZE]; + char buff4[BUF_SIZE]; + struct io_uring_sqe *sqe; + struct io_uring_cqe *cqe; + int str1_sz = strlen(STR1); + int str2_sz = strlen(STR2); + + fds[0] = open(FILE_NAME1, O_RDWR | O_TRUNC | O_CREAT, 0644); + if (fds[0] < 0 ) { + perror("open"); + return 1; + } + + memset(buff1, 0, BUF_SIZE); + memset(buff2, 0, BUF_SIZE); + memset(buff3, 0, BUF_SIZE); + memset(buff4, 0, BUF_SIZE); + strncpy(buff1, STR1, str1_sz); + strncpy(buff2, STR2, str2_sz); + + int ret = io_uring_register_files(ring, fds, 1); + if(ret) { + fprintf(stderr, "Error registering buffers: %s", strerror(-ret)); + return 1; + } + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "Could not get SQE.\n"); + return 1; + } + io_uring_prep_write(sqe, 0, buff1, str1_sz, 0); + sqe->flags |= IOSQE_FIXED_FILE; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "Could not get SQE.\n"); + return 1; + } + io_uring_prep_write(sqe, 0, buff2, str2_sz, str1_sz); + sqe->flags |= IOSQE_FIXED_FILE; + + io_uring_submit(ring); + + for(int i = 0; i < 2; i ++) { + int ret = io_uring_wait_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "Error waiting for completion: %s\n", + strerror(-ret)); + return 1; + } + /* Now that we have the CQE, let's process the data */ + if (cqe->res < 0) { + fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res)); + } + printf("Result of the operation: %d\n", cqe->res); + io_uring_cqe_seen(ring, cqe); + } + + print_sq_poll_kernel_thread_status(); + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "Could not get SQE.\n"); + return 1; + } + io_uring_prep_read(sqe, 0, buff3, str1_sz, 0); + sqe->flags |= IOSQE_FIXED_FILE; + + sqe = io_uring_get_sqe(ring); + if (!sqe) { + fprintf(stderr, "Could not get SQE.\n"); + return 1; + } + io_uring_prep_read(sqe, 0, buff4, str2_sz, str1_sz); + sqe->flags |= IOSQE_FIXED_FILE; + + io_uring_submit(ring); + + for(int i = 0; i < 2; i ++) { + int ret = io_uring_wait_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "Error waiting for completion: %s\n", + strerror(-ret)); + return 1; + } + /* Now that we have the CQE, let's process the data */ + if (cqe->res < 0) { + fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res)); + } + printf("Result of the operation: %d\n", cqe->res); + io_uring_cqe_seen(ring, cqe); + } + printf("Contents read from file:\n"); + printf("%s%s", buff3, buff4); +} + +int main() { + struct io_uring ring; + struct io_uring_params params; + + if (geteuid()) { + fprintf(stderr, "You need root privileges to run this program.\n"); + return 1; + } + + print_sq_poll_kernel_thread_status(); + + memset(¶ms, 0, sizeof(params)); + params.flags |= IORING_SETUP_SQPOLL; + params.sq_thread_idle = 2000; + + int ret = io_uring_queue_init_params(8, &ring, ¶ms); + if (ret) { + fprintf(stderr, "Unable to setup io_uring: %s\n", strerror(-ret)); + return 1; + } + start_sq_polling_ops(&ring); + io_uring_queue_exit(&ring); + return 0; +}