c - 这个程序中的posix_memalign是什么意思?

标签 c memory posix

我读过一个使用 DIRECT_IO 来插入一个寄存器(prof 结构)的程序。我怀疑我是否在这里正确使用了 posix_memalign 。我知道分配的内存块必须对齐才能使用 DIRECT_IO 方法。

我的程序是(正在运行,但是请告诉我任何建议):

#define _GNU_SOURCE /* for O_DIRECT */

#include <stdio.h>
#include <sys/types.h>  /* required by open() */
#include <unistd.h>     /* open(), write() */
#include <fcntl.h>      /* open() and fcntl() */
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <malloc.h>

#define PAGE_SIZE 4096
#define LENGTH  50

typedef struct {
    int nusp;
    char first_name[LENGTH];
    char last_name[LENGTH];
    char department[LENGTH];
    int year_of_begin;
} prof;

int disk_open() {
    int flag;
    int ret;

    flag = O_CREAT | O_RDWR | O_DIRECT;

    if ((ret = open("test.header", flag, S_IRUSR | S_IWUSR)) < 0) {
        printf("was impossible to create the file");
        return -1;
    }
    return ret;
}

int main() {
    int cmd;
    prof d;
    int arq;
    uint8_t *buf, *loc;
    size_t bufsize;

    do {
        printf("Digite:\n");
        printf("****\n(1) insert\n(2) see register\n(3) leave\n****\n");
        scanf("%d", &cmd);
        switch (cmd) {
            case 1:
                printf("ID: ");
                scanf("%d", &(d.nusp));

                printf("NAME: ");
                scanf("%s", (d.first_name));

                printf("LASTNAME: ");
                scanf("%s", (d.last_name));

                printf("DEPARTAMENT: ");
                scanf("%s", (d.department));

                printf("YEAR: ");
                scanf("%d", &(d.year_of_begin));

                bufsize = sizeof (int) +
                        sizeof (d.first_name) +
                        sizeof (d.last_name) +
                        sizeof (d.department) +
                        sizeof (int);
                printf("bufsize = %d\n", bufsize);
                //buf = (uint8_t*) malloc(bufsize);
                if(posix_memalign((void**)&buf, PAGE_SIZE, bufsize)) {
                    printf("allocation failed\n");
                }

                loc = buf;

                memcpy(loc, &(d.nusp), sizeof (int));
                loc += sizeof (int);

                memcpy(loc, d.first_name, sizeof (d.first_name));
                loc += sizeof (d.first_name);

                memcpy(loc, d.last_name, sizeof (d.first_name));
                loc += sizeof (d.last_name);

                memcpy(loc, d.department, sizeof (d.department));
                loc += sizeof (d.department);

                memcpy(loc, &(d.year_of_begin), sizeof (int));
                loc += sizeof (int);

                arq = disk_open();
                printf("bytes written: %d\n", write(arq, buf, PAGE_SIZE));

                free(buf);

                close(arq);
                break;

            case 2:
                arq = disk_open();

                bufsize = sizeof (int) +
                        sizeof (d.first_name) +
                        sizeof (d.last_name) +
                        sizeof (d.department) +
                        sizeof (int);
                //buf = (uint8_t*) malloc(bufsize);
                 posix_memalign((void**)&buf, PAGE_SIZE, bufsize);

                printf("read %d bytes\n", read(arq, buf, PAGE_SIZE));

                memcpy(&(d.nusp), buf, sizeof (int));
                buf += sizeof (int);

                memcpy(d.first_name, buf, sizeof (d.first_name));
                buf += sizeof (d.first_name);

                memcpy(d.last_name, buf, sizeof (d.last_name));
                buf += sizeof (d.last_name);

                memcpy(d.department, buf, sizeof (d.department));
                buf += sizeof (d.department);

                memcpy(&(d.year_of_begin), buf, sizeof (int));
                buf += sizeof (int);

                printf("ID: %d\n", d.nusp);
                printf("NAME: %s\n", d.first_name);
                printf("LAST NAME: %s\n", d.last_name);
                printf("DEPARTAMENT: %s\n", d.department);
                printf("YEAR: %d\n", d.year_of_begin);
                printf("\n");

                close(arq);
                break;

            default:
                printf("Leaving....");
                break;
        }
    } while (cmd != 3);
    return 0;
}

所以,我的问题是: 在posix_memalign((void**)&buf, PAGE_SIZE, bufsize); ,我是否在 4096 字节的 block 内存(即我的页面大小)中分配了 158 字节,这意味着我浪费了 3938 字节?发生这种情况是因为我必须写入/读取对齐的 block 内存,在本例中是 4096(2 的幂),对吗?

如果我的 PAGE_SIZE 等于 bufsize(在本例中考虑 bufsize 和 PAGE_SIZE 的大小等于 4096 字节),会发生什么?这是否意味着我没有浪费内存和磁盘中的字节?

如果我想在Windows中使用DIRECT_IO,可以吗?如果是,我必须使用哪个库/函数来分配对齐的 block 内存?

最佳答案

您将返回一 block 保证与PAGE_SIZE对齐的内存块;而已。不要假设最后有 3938 字节的空间未使用 - 智能分配器可以轻松地为您提供页面对齐内存板中的第一个条目,并保留其余部分用于其他 158 字节分配。

您仅使用 PAGE_SIZE 磁盘 block 的 158 字节,浪费了磁盘上的大量空间,事实上,您的写入实际上是将未定义的数据放入磁盘中 block - 从 158 到 4096 的所有内容都从未分配给您,您不应该尝试读取或写入该内存。

如果 PAGE_SIZE == bufsize,那么您就不会浪费任何磁盘空间。

another question ,它解释了如何获得与 O_DIRECT 标志类似的行为。

关于c - 这个程序中的posix_memalign是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36089301/

相关文章:

c - 按值传递-不按我想要的方式执行

c++ - 读/写保护内存?

c - 我怎样才能释放内存?

Linux/POSIX : Why doesn't fork() fork *all* threads

c - 为什么 read 不起作用但 fgets 在我的程序中工作正常?

c - 获取要从字符串中打印的符号数

c++ - 以任何方式多次打开共享内存是否不好?

c - 如何在 c 中结束重定向的 execlp

c - 获取小时/分钟/秒,无需用户更改系统小时

c - block 作用域缓冲区在 c 中的 block 之外是否有效