Beej’s Guide to Unix IPC – Shared Memory Segments
Programming in C – IPC: Shared Memory
System V IPC
Oracle Programming Interfaces Guide – System V Shared Memory
Don’t use absolute pointer
POSIX / System v Shared Memory vs Threads Shared Memory
In the table I mention the virtual memory mapping issues, what is all that about? It’s about being able to store pointers on the shared memory and whenever that make sense. As I point out the memory can be mapped at different virtual addresses in different processes which means that you can’t use absolute pointer. I think this is better understood with a picture:
In the picture process 1 has stored the 0x70000100 address in the shared memory, that points to somewhere in the shared memory for process 1 but when process 2 reads that address it points to a completely different data in that process that it’s outside the shared memory. That is because the shared memory was mapped at 0x7000000 on process 1 but on 0xFF000000 on process 2.
This means that when using POSIX shared memory you will have to refrain from storing pointers and need to store offsets or indexes instead (ptrdiff_t, etc). That, in turns, means that you have to engineer your data structures for shared memory.
stackoverflow
Why use SysV or POSIX shared memory vs mmap()?
How to list processes attached to a shared memory segment in linux?
# ipcs
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x07021999 0 root 644 1704 2
0x00000000 98305 lightdm 600 67108864 2 dest
0x00000000 3014666 andreas 600 393216 2 dest
0x00000000 829358091 andreas 600 393216 2 dest
0x00003e77 1092059148 andreas 666 16384 0
0x00000070 1103659029 root 644 4100 0
0x00000071 1103691798 root 644 4100 0
0x00000000 1094877207 andreas 600 393216 2 dest
0x00000000 1094975512 andreas 600 393216 2 dest
------ Semaphore Arrays --------
key semid owner perms nsems
0x00000070 0 root 644 1
0x00000071 32769 root 644 1
------ Message Queues --------
key msqid owner perms used-bytes messages
# cat /proc/sysvipc/shm
key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap
117578137 0 644 1704 961 5094 2 0 0 0 0 1465985643 1465985643 1460954208 4096 0
0 98305 1600 67108864 829 985 2 994 991 994 991 1460957764 1460957764 1460957764 14114816 0
0 196610 1600 393216 1173 1006 2 1000 1000 1000 1000 1460957835 0 1460957835 294912 0
0 229379 1600 393216 1173 1006 2 1000 1000 1000 1000 1460957836 0 1460957836 393216 0
0 753668 1600 393216 1176 31704 2 1000 1000 1000 1000 1465912954 1465912954 1460964055 327680 0
0 622597 1600 524288 1178 1006 2 1000 1000 1000 1000 1460957836 0 1460957836 57344 0
0 720902 1600 393216 1269 1006 2 1000 1000 1000 1000 1460957837 0 1460957837 393216 0
0 740392967 1600 911400 1405 1006 2 1000 1000 1000 1000 1462974727 0 1462974727 913408 0
0 829259784 1600 393216 1571 7645 2 1000 1000 1000 1000 1465140074 1465140074 1462981930 393216 0
0 1179657 1600 393216 1871 9967 2 1000 1000 1000 1000 1464769386 1464769386 1460964459 393216 0
0 3014666 1600 393216 1871 9967 2 1000 1000 1000 1000 1464769386 1464769386 1460964669 393216 0
0 829358091 1600 393216 1571 7645 2 1000 1000 1000 1000 1465140074 1465140074 1462981931 294912 0
15991 1092059148 666 16384 15991 15991 0 1000 1000 1000 1000 1464004251 1464004251 1464004251 4096 0
0 1102446605 1600 393216 23751 1006 2 1000 1000 1000 1000 1465368150 0 1465368150 393216 0
0 1103626254 1600 33904 1405 1006 2 1000 1000 1000 1000 1465915710 0 1465915710 36864 0
0 13139983 1600 393216 1183 1006 2 1000 1000 1000 1000 1460992697 0 1460992697 90112 0
0 1103134736 1600 524288 31704 1006 2 1000 1000 1000 1000 1465912955 0 1465912955 40960 0
0 1092419601 1600 8388608 15992 4833 2 1000 1000 1000 1000 1465984866 1465984866 1464004285 2383872 0
0 1066008594 1600 7887360 1405 1006 2 1000 1000 1000 1000 1463145010 0 1463145010 7888896 0
0 1086849043 1600 393216 1269 1006 2 1000 1000 1000 1000 1463486217 0 1463486217 393216 0
0 1081835540 1600 393216 1176 31704 2 1000 1000 1000 1000 1465912954 1465912954 1463484456 131072 0
112 1103659029 644 4100 4744 5016 0 0 0 0 0 1465985147 1465985149 1465984872 0 0
113 1103691798 644 4100 5014 5016 0 0 0 0 0 1465985147 1465985149 1465985125 0 0
0 1094877207 1600 393216 23562 7548 2 1000 1000 1000 1000 1465139904 1465139904 1464605255 393216 0
0 1094975512 1600 393216 23562 7548 2 1000 1000 1000 1000 1465139904 1465139904 1464605255 393216 0