Skip to content
systemcoding.dev
Networking

Initialise a ring with io_uring_queue_init

Create an io_uring instance with io_uring_queue_init in liburing 2.5 on Linux 6.8, check the negative errno return, and release it with io_uring_queue_exit.

1 min read
Summarize in AI

This is how every io_uring program starts. io_uring_queue_init() asks the kernel for a submission queue and a completion queue of the size you give, and maps both into your process.

ring_init.cc
#include <stdio.h>
#include <string.h>
#include <liburing.h>

int main(void)
{
    struct io_uring ring;
    int rc = io_uring_queue_init(64, &ring, 0);
    if (rc < 0) {
        fprintf(stderr, "queue_init: %s\n", strerror(-rc));
        return 1;
    }
    printf("sq entries=%u cq entries=%u\n",
           ring.sq.ring_entries, ring.cq.ring_entries);
    io_uring_queue_exit(&ring);
    return 0;
}
Text
sq entries=64 cq entries=128

The call returns 0 or a negative errno value, not -1 with errno set, so print it with strerror(-rc). The entry count is rounded up to a power of two. The completion queue defaults to twice the submission queue, which is why the second number is 128; a burst of completions then has room even when you submit slowly.

Build with cc ring_init.c -luring, using liburing 2.5. A kernel older than 5.1 fails with ENOSYS, and a container with a seccomp profile that blocks the io_uring_setup call fails with EPERM, since several distributions restrict it. The flags argument takes setup options such as IORING_SETUP_SQPOLL. See io_uring_queue_init(3).

Taggedclinux
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.