seeking help about how to map shm to a C structure

Mr xtang:
Thanks for your concern. In openqnx, I saw mario’s post:

I like to make the share memory map to a C structure. That way all
programs just use the C structure as reference for the data layout in
the shared memory, adding a mutext/rwl/etc to that structure becomes
trivial.

Now I want to use this way: The following is my code:

#include<_pack1.h>

typedef struct{
int8_t sub1;
int16_t sub2;
int32_t sub3;
} DATA;
.
.
DATA *p;
.
.
fd=shm_open(…);
.
p=mmap(0,sizeof(DATA), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
.
p->sub1 = 0x4a;
p->sub2 = 0x4a3b;
p->sub3 = 0x4aff6b2e;
.
.

(in another process):
.
.
printf(“02x,02x,02x”,p->sub1, p->sub2, p->sub3);
.

is my method right? If not ok, kindly give me some guidance

many thanks

Somewhat there. You method to lay the structure into a shared memory is okey, but you need to “share” the same memory between the 2 process.

Try to look at shm_open(), the lib reference of shm_open() have an example how to create a named shared object, and access it from another process.

To your sample, you second process also need a “shm_open()”, and mmap() to let you be able to access that shared object.