How to split a pointer structure between two unrelated shared memory processes in C?

I have a structure that looks like this:

typedef struct shared_data_t
{
    char *key;
    char *message;
}shared_data;

I need to share this structure with another unrelated process. To achieve this, I use POSIX shared memory with shm_open () / mmap (). However, my target process does not receive general data and its skill with SIGSEGV, which is obvious. It will be great if someone helps me with this, especially what happens when pointers are shared between two shared memory processes (using shm_open and mmap).

For a structure like

typedef struct shared_data_t
{
    char key[8];
    char message[32];
}shared_data;

it works great!

+4
source share
2

Linux : shmat:

       Using shmat() with shmaddr equal to NULL is the preferred, portable way
       of attaching a shared memory segment.  Be aware that the shared  memory
       segment  attached in this way may be attached at different addresses in
       different processes.  Therefore, any  pointers  maintained  within  the
       shared  memory must be made relative (typically to the starting address
       of the segment), rather than absolute.
+4

key message. , 0x1000 0x1040. , , 0x7000. . , , 0x9000. . , key message 0x1000 0x1040. . , .

, key message , ( mmap , , ), , key message. . char (, ptrdiff_t), . .

, - , shared_data: , , . , , .

+3

Source: https://habr.com/ru/post/1527316/


All Articles