首页 > 解决方案 > 通过共享内存进行进程间通信

问题描述

我正在尝试使用共享内存进行进程间通信。这是结构定义。

#define SHM_SIZE (519430400)
test_ht *CommandLine_Buffer;

typedef struct
{
    int             a;
    int             testnum;
    st_cl           cli[3];
}test_ht;

typedef struct
{
    tHT             CliInfo;
} st_cl;

typedef struct
{
    int x;
} tHT;

Shared memory is created as below

    int fd;
    int ret_v;
    void* addr;
    int i;

    fd = shm_open("SharedBuf.shm", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    ret_v = ftruncate(fd, SHM_SIZE);
    SharedBuffer = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SEEK_SET);

    I access the structure members as below

    CommandLine_Buffer->cli[i].CliInfo.x

在这里我可以读写atestnum跨进程。共享内存创建成功。同时我可以访问

CommandLine_Buffer->cli[0].CliInfo.x and CommandLine_Buffer->cli[1].CliInfo.x

我无法得到CommandLine_Buffer->cli[2].CliInfo.x

调试此的任何提示或任何想法为什么无法读取最后一个位置。

标签: cipcshared-memory

解决方案


推荐阅读