通过共享内存进行进程间通讯

时间:2018-09-28 06:01:54

标签: c ipc shared-memory

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

#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

任何调试此技巧或任何想法为何无法读取最后位置的提示。

0 个答案:

没有答案