简体   繁体   English

IPC在C中使用共享内存

[英]IPC using shared memory in C

I am implementing IPC using shared memory in C linux. 我正在使用C linux中的共享内存来实现IPC。 Here is my receiving process. 这是我的接收流程。 It's receiving correct length but not the message. 它收到正确的长度,但没有收到消息。 However sending process is properly sending it. 但是,发送过程正在正确发送它。 Please see this and let me know the error. 请查看此内容,并让我知道该错误。

//header files
#include "/home/user/msgbuf.h"
#define SHMSZ    127
int main()
{
    int shmid;
    key_t key;
    message_buf *rbuf;
    rbuf=malloc(sizeof(*rbuf));
    key = ftok("/home/user/shmem",17);

    if ((shmid = shmget(key, SHMSZ, 0666)) < 0)
    {       perror("shmget");
            exit(1);
    }
    printf("\nShared Memory Id = %d\n",shmid);
    if ((rbuf = shmat(shmid, NULL, 0)) == (message_buf *) -1)
    {       perror("shmat");
            exit(1);
    }
    printf("\nMEMORY SEGMENT ATTACHED TO THE CLIENT'S PROCESS\n");

/* Now read what the server put in the memory */
    printf("\nmsglen = %d",rbuf->msglen);  //this is correct
    rbuf->cp=malloc(rbuf->msglen);
    memcpy(&rbuf->cp,rbuf+sizeof(int),sizeof(*rbuf));
    printf("\nMESSAGE :: %s",rbuf->cp); //MESSAGE :: null
    fflush(stdout);
    shmdt(&shmid);
    printf("\nMEMORY SEGMENT %d DETACHED\n",shmid);
    return 0;
}

msgbuf.h is msgbuf.h是

typedef struct msgbuf1
{
    int msglen;
    char *cp;
}message_buf;

thanks :) 谢谢 :)

You read a char* from the shared memory region. 您从共享内存区域读取一个char *。 However, that points to a buffer allocated with malloc, in the remote process. 但是,这指向远程进程中分配了malloc的缓冲区。 As such it points to the process heap of local to that other process. 因此,它指向该其他进程本地的进程堆。

This is simply undefined behaviour. 这只是未定义的行为。

Instead, make the character buffer part of the shared memory data structure: 而是使字符缓冲区成为共享内存数据结构的一部分:

//header files
#define MAX_SH_BUFSIZE 1024
//
typedef struct msgbuf1
{
    int msglen;
    char cp[MAX_SH_BUFSIZE];
} message_buf;

The problem is with the malloc. 问题出在malloc。 malloc addresses have scope till he process not the shared memory. malloc地址具有作用域,直到他不处理共享内存为止。 You should not use malloc in shared memory. 您不应在共享内存中使用malloc。

Method 1. 方法1

Instead use a fix size character array as below: 而是使用固定大小的字符数组,如下所示:

typedef struct msgbuf1
{
    int msglen;
    char cp[MESSAGE_SIZE];
}message_buf;

Set MESSAGE_SIZE as the maximum size of data you would require. 将MESSAGE_SIZE设置为所需的最大数据大小。

Method 2. Create a sufficient size shared memory. 方法2.创建足够大小的共享内存。 Make you own malloc and free function which would provide memory from shared memory as and when require. 使您拥有malloc和free函数,这些函数将在需要时从共享内存中提供内存。 For this you need to keep track of free list, which store the free memory space in the shared memory. 为此,您需要跟踪空闲列表,这些空闲列表将可用内存空间存储在共享内存中。

You can divide your shared memory into chunks of like say 256bytes, 512bytes and 1024bytes. 您可以将共享内存分为256个字节,512个字节和1024个字节之类的块。 Now whenever you require memory the custom malloc function for shared memory shoud hold the logic to provide memory efficiently. 现在,无论何时需要内存,共享内存的自定义malloc函数都应保持逻辑以有效地提供内存。

Custom free function would free the shared memory released by program along with adding that chunk(s) in free list 自定义免费功能将释放程序释放的共享内存,并在空闲列表中添加该块

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

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