c - 无法编译 io_uring

标签 c linux io-uring

我一直在阅读 https://kernel.dk/io_uring.pdf,我想尝试使用实际的系统调用(io_uring_setup、io_uring_enter)来检查我的理解,但我无法编译以下简单程序:

#include <kernel/io_uring.h>
#include <stdint.h>

int main() {
    struct io_uring_params p;
    int ring = io_uring_setup((__u32) 512, &p);
    return 0;
}
我收到 io_uring_setup 函数的隐式声明错误。手册页 https://manpages.debian.org/unstable/liburing-dev/io_uring_setup.2.en.html 建议包含的唯一文件是 linux/io_uring.h,但是当我查看源代码时,我没有看到 io_uring_setup 的定义。

最佳答案

(2021 年中)作为 @oakad stated in the comments the io_uring syscalls are not currently wrapped by libc 。如果用户想要调用原始 io_uring 系统调用(例如 io_uring_setup(2) 中描述的),则由他们提供额外的样板来这样做并确保他们遵守所有预期的规则......而不是手动完成所有事情,它看起来 easier to use liburing ( io_uring 包装库)。
我不清楚你为什么选择使用

#include <kernel/io_uring.h>
——这看起来不对。我系统上的标题是由
#include <linux/io_uring.h>
并且以下在我的系统上编译没有错误:
/* Needed for memset */
#include <stdio.h>
/* Needed for perror */
#include <string.h>
/* Needed to invoke the syscall */
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
/* Needed for io_uring data structures. Compilation will error with a "No such
 * file or directory" on the following include line if your kernel headers are
 * too old/don't know about io_uring. If this happens you have to manually 
 * declare all the io_uring structures/defines yourself. */
#include <linux/io_uring.h>
/* C libraries don't (yet) provide a pretty wrapper for the io_uring syscalls 
 * so create an io_uring_setup syscall wrapper ourselves */
int io_uring_setup(unsigned int entries, struct io_uring_params *p) {
    return syscall(__NR_io_uring_setup, entries, p);
}

int main() {
    int fd;
    struct io_uring_params p;

    memset(&p, 0, sizeof(p));
    fd = io_uring_setup(512, &p);

    if (fd < 0)
        perror("io_uring_setup");

    return 0;
}
然而,正如 Efficient IO with io_uring PDF 中提到的,这只是通过直接系统调用使用 io_uring 时的冰山一角。 Lord of the io_uring tutorial 有一个标题为 The Low-level io_uring Interface 的部分,它更详细地描述了用法,但使用 io_uring 看起来更容易也更安全。

关于c - 无法编译 io_uring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67552233/

相关文章:

python - TensorFlow:CentOS 上的二进制安装错误

linux - 如何使用 gnu make 工具更新文件?

linux - 解压缩到目录并从 shell 脚本在解压缩的文件夹上运行命令

linux - io_uring_queue_init 权限被拒绝

无法更新指针值

python - 使用 ctypes 在其内部包含另一个结构的结构

c - 在 C 中将整数的最后两位作为字符串返回

c++ - 如何保存返回的 float 组指针?