c - 共享内存和memcpy问题

标签 c shared-memory mmap memcpy

我正在学习用 C 语言(linux)使用共享内存进行编程。我需要在使用 fork() 创建的多个进程之间共享一些结构。不幸的是,当我尝试初始化新共享的地址空间时,我在 memcpy 调用中收到一个无提示错误(控制台中没有输出)。

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

#define ROWS 10000
#define COLS 15000

struct mm_shared {
    int test;
    unsigned char matrix[ROWS][COLS];
};

int main(void) {

    int fd;
    if ((fd = shm_open("/mm", O_CREAT | O_RDWR, 0777)) == -1) {
        printf(stderr, "shm_open failed! %d - %s\n", errno, strerror(errno));
    }

    if (ftruncate(fd, sizeof(struct mm_shared) == -1)) {
        printf(stderr, "ftruncate failed! %d - %s\n", errno, strerror(errno));
    }

    struct mm_shared * shared;
    if ((shared = mmap(NULL, sizeof(struct mm_shared), PROT_READ
            | PROT_WRITE, MAP_SHARED, fd, 0)) == -1) {
        printf("mmap failed! %d, %s\n", errno, strerror(errno));
    }
    struct mm_shared * init = (struct mm_shared *) malloc(sizeof(struct mm_shared));

    memcpy(shared, init, sizeof(struct mm_shared)); <-- here lies the problem!


    shm_unlink("/mm");
    return EXIT_SUCCESS;
}

调试共享指针时,调试信息(eclipse调试器)显示:

Failed to execute MI command:
-data-evaluate-expression (shared)->test
Error message from debugger back end:
Cannot access memory at address 0x7ffff7ffc000

不知道这是否有帮助。另外,我想问我在结构中存储大矩阵的方法是否正确(它应该分配在堆中,对吗?因为即使矩阵本身不是指针,我也会得到指向结构的指针)。

任何有关此问题的帮助将不胜感激!

最佳答案

经过一个小时的调试和审核周期终于发现了错误,就是这个

second argument for ftruncate seems to be evaluating to wrong value
if (ftruncate(fd, sizeof(struct mm_shared) == -1)) {

更改为

if (ftruncate(fd, sizeof(struct mm_shared)) == -1) {

这是经过修改的代码

int main(void) {

    int fd;
    if ((fd = shm_open("/mm", O_CREAT | O_RDWR, 0777)) == -1) {
        fprintf(stderr, "shm_open failed! %d - %s\n", errno, strerror(errno)); // should be fprintf
    exit(1);// possibly exit here
    }

    //if (ftruncate(fd, sizeof(struct mm_shared) == -1)) {
    if (ftruncate(fd, sizeof(struct mm_shared)) == -1) {
        fprintf(stderr, "ftruncate failed! %d - %s\n", errno, strerror(errno)); // should be fprintf
    goto out;   // remove all before leaving    
    }

    struct mm_shared * shared;
    if ((shared = mmap(NULL, sizeof(struct mm_shared) , PROT_READ
            | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED ) {  // Change -1 to MAP_FALED
        fprintf(stderr, "mmap failed! %d, %s\n", errno, strerror(errno)); // should be fprintf and stderr 
    goto out;   // remove all before leaving
    }
    struct mm_shared * init = (struct mm_shared *) malloc(sizeof(struct mm_shared));

    //memcpy(shared, init, sizeof(struct mm_shared)); //<-- here lies the problem!
    memcpy(init, shared, sizeof(struct mm_shared)); //<-- here lies the problem!

out:
    shm_unlink("/mm");
    return EXIT_SUCCESS;
}

关于c - 共享内存和memcpy问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32675298/

相关文章:

c - 从函数 : Error 返回字符串指针

尝试从服务器 tcp 读回时客户端挂起

linux - 通过 sys/shm.h 共享多个变量

c - 映射文件并索引到内存中以进行读/写

c - Linux:是否可以在进程之间共享代码?

linux - mmap 总线错误

c - 如何生成随机的 0 和 1 但它们在 C 中出现的概率为 80-20?

c - cmake中静态链接.lib文件

c - OpenMP:单一的共享变量,等待构造

c++ - 内核模式 - 通过共享内存进行用户模式通信,无需使用系统线程