c - 在 docker 容器内运行时的不同结果

标签 c linux docker aio

我正在尝试基于这个 libaio 示例运行一些代码: https://oxnz.github.io/2016/10/13/linux-aio/#example-1

我根据 libaio 的文档添加了 O_DIRECT 标志。 它似乎可以在我的 ubuntu 16.04 台式机中运行(hello 已写入/tmp/test)。

但是,当我在 docker 容器中编译和运行相同的示例时,没有任何内容写入文件。在 gdb 中运行时,我可以看到 io_getevents 读取了一个事件,结果设置为 -22 (EINVAL)。

有什么想法吗?

这是我修改后的代码

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <libaio.h>

int main() {
    io_context_t ctx;
    struct iocb iocb;
    struct iocb * iocbs[1];
    struct io_event events[1];
    struct timespec timeout;
    int fd;

    fd = open("/tmp/test", O_WRONLY | O_CREAT | O_DIRECT) ;
    if (fd < 0) err(1, "open");

    memset(&ctx, 0, sizeof(ctx));
    if (io_setup(10, &ctx) != 0) err(1, "io_setup");

    const char *msg = "hello";
    io_prep_pwrite(&iocb, fd, (void *)msg, strlen(msg), 0);
    iocb.data = (void *)msg;

    iocbs[0] = &iocb;

    if (io_submit(ctx, 1, iocbs) != 1) {
        io_destroy(ctx);
        err(1, "io_submit");
    }

    while (1) {
        timeout.tv_sec = 0;
        timeout.tv_nsec = 500000000;
    int ret = io_getevents(ctx, 0, 1, events, &timeout);
        printf("ret=%d\n", ret);
    if (ret == 1) {
            close(fd);
            break;
        }
        printf("not done yet\n");
        sleep(1);
    }
    io_destroy(ctx);

    return 0;
}

最佳答案

容器内的文件系统可能与主机的文件系统不同(在现代设置中可能是 overlayfs 但在旧系统上可能是 aufs ).对于打开的O_DIRECT,设备/文件系统必须至少“支持”它(注意引号),而您的容器的文件系统可能不支持。

关于c - 在 docker 容器内运行时的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51199443/

相关文章:

c++ - 如何在gtkrc中捕获GTK焦点状态?

c - if(数学函数)中的范围错误

html - 使用 sed 从 html 中提取 pdf

linux - 重新连接后 USB 调制解调器多个 ttyACMx 发生变化

docker - 从容器内获取 docker 图像摘要/哈希

客户端和服务器+排序

c - 在 C 中运行时与应用程序交互

linux - 如何从 ContainerS 调用/运行容器中的 Shell 脚本?

java - Micronaut 和 Java Mail API

docker - 在Docker中使用Data Volume Container作为单数据库 "backend"有什么好处?