繁体   English   中英

在Docker容器中运行时的结果不同

[英]Different results when running inside a docker container

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

我根据libaio的文档添加了O_DIRECT标志。 它似乎可以在我的ubuntu 16.04台式机内部运行(您好已写入/ 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在开放的环境中工作,设备/文件系统必须至少“支持”它(请注意引号),并且容器的文件系统可能不支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM