{"id":4920,"date":"2016-06-15T10:10:12","date_gmt":"2016-06-15T10:10:12","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=4920"},"modified":"2016-06-15T15:16:42","modified_gmt":"2016-06-15T15:16:42","slug":"shared-memory-system-v-posix-mmap","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=4920","title":{"rendered":"Shared Memory: System V \/ POSIX \/ mmap"},"content":{"rendered":"<p><a href=\"https:\/\/beej.us\/guide\/bgipc\/output\/html\/multipage\/shm.html\">Beej&#8217;s Guide to Unix IPC &#8211; Shared Memory Segments<\/a><br \/>\n<a href=\"https:\/\/www.cs.cf.ac.uk\/Dave\/C\/node27.html\">Programming in C &#8211; IPC: Shared Memory<\/a><br \/>\n<a href=\"http:\/\/fscked.org\/writings\/SHM\/shm-5.html\">System V IPC<\/a><br \/>\n<a href=\"https:\/\/docs.oracle.com\/cd\/E19683-01\/806-4125\/svipc-41256\/index.html\">Oracle Programming Interfaces Guide &#8211; System V Shared Memory<\/a><\/p>\n<h3>Don&#8217;t use absolute pointer<\/h3>\n<p><a href=\"http:\/\/rubenlaguna.com\/wp\/2015\/02\/22\/posix-slash-system-v-shared-memory-vs-threads-shared-memory\/\">POSIX \/ System v Shared Memory vs Threads Shared Memory<\/a><\/p>\n<p>In the table I mention the virtual memory mapping issues, what is all that about? It\u2019s 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\u2019t use absolute pointer. I think this is better understood with a picture:<\/p>\n<table>\n<tr>\n<td><a href=\"http:\/\/blog.bachi.net\/wp-content\/uploads\/2016\/06\/virtualmemory.png\" rel=\"attachment wp-att-4925\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.bachi.net\/wp-content\/uploads\/2016\/06\/virtualmemory-300x234.png\" alt=\"virtualmemory\" width=\"300\" height=\"234\" class=\"alignleft size-medium wp-image-4925\" srcset=\"https:\/\/blog.bachi.net\/wp-content\/uploads\/2016\/06\/virtualmemory-300x234.png 300w, https:\/\/blog.bachi.net\/wp-content\/uploads\/2016\/06\/virtualmemory-624x487.png 624w, https:\/\/blog.bachi.net\/wp-content\/uploads\/2016\/06\/virtualmemory.png 634w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/td>\n<\/tr>\n<\/table>\n<p>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\u2019s outside the shared memory. That is because the shared memory was mapped at 0x7000000 on process 1 but on 0xFF000000 on process 2.<\/p>\n<p>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.<\/p>\n<h3>stackoverflow<\/h3>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1123490\/why-use-sysv-or-posix-shared-memory-vs-mmap\">Why use SysV or POSIX shared memory vs mmap()?<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/5658568\/how-to-list-processes-attached-to-a-shared-memory-segment-in-linux\">How to list processes attached to a shared memory segment in linux?<\/a><\/p>\n<ul>\n<li>ipcs<\/li>\n<li>ipcmk<\/li>\n<li>ipcrm<\/li>\n<\/ul>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# ipcs \r\n\r\n------ Shared Memory Segments --------\r\nkey        shmid      owner      perms      bytes      nattch     status      \r\n0x07021999 0          root       644        1704       2                       \r\n0x00000000 98305      lightdm    600        67108864   2          dest            \r\n0x00000000 3014666    andreas    600        393216     2          dest         \r\n0x00000000 829358091  andreas    600        393216     2          dest         \r\n0x00003e77 1092059148 andreas    666        16384      0                       \r\n0x00000070 1103659029 root       644        4100       0                       \r\n0x00000071 1103691798 root       644        4100       0                       \r\n0x00000000 1094877207 andreas    600        393216     2          dest         \r\n0x00000000 1094975512 andreas    600        393216     2          dest         \r\n\r\n------ Semaphore Arrays --------\r\nkey        semid      owner      perms      nsems     \r\n0x00000070 0          root       644        1         \r\n0x00000071 32769      root       644        1         \r\n\r\n------ Message Queues --------\r\nkey        msqid      owner      perms      used-bytes   messages\r\n\r\n# cat \/proc\/sysvipc\/shm                   \r\n       key      shmid perms                  size  cpid  lpid nattch   uid   gid  cuid  cgid      atime      dtime      ctime                   rss                  swap\r\n 117578137          0   644                  1704   961  5094      2     0     0     0     0 1465985643 1465985643 1460954208                  4096                     0\r\n         0      98305  1600              67108864   829   985      2   994   991   994   991 1460957764 1460957764 1460957764              14114816                     0\r\n         0     196610  1600                393216  1173  1006      2  1000  1000  1000  1000 1460957835          0 1460957835                294912                     0\r\n         0     229379  1600                393216  1173  1006      2  1000  1000  1000  1000 1460957836          0 1460957836                393216                     0\r\n         0     753668  1600                393216  1176 31704      2  1000  1000  1000  1000 1465912954 1465912954 1460964055                327680                     0\r\n         0     622597  1600                524288  1178  1006      2  1000  1000  1000  1000 1460957836          0 1460957836                 57344                     0\r\n         0     720902  1600                393216  1269  1006      2  1000  1000  1000  1000 1460957837          0 1460957837                393216                     0\r\n         0  740392967  1600                911400  1405  1006      2  1000  1000  1000  1000 1462974727          0 1462974727                913408                     0\r\n         0  829259784  1600                393216  1571  7645      2  1000  1000  1000  1000 1465140074 1465140074 1462981930                393216                     0\r\n         0    1179657  1600                393216  1871  9967      2  1000  1000  1000  1000 1464769386 1464769386 1460964459                393216                     0\r\n         0    3014666  1600                393216  1871  9967      2  1000  1000  1000  1000 1464769386 1464769386 1460964669                393216                     0\r\n         0  829358091  1600                393216  1571  7645      2  1000  1000  1000  1000 1465140074 1465140074 1462981931                294912                     0\r\n     15991 1092059148   666                 16384 15991 15991      0  1000  1000  1000  1000 1464004251 1464004251 1464004251                  4096                     0\r\n         0 1102446605  1600                393216 23751  1006      2  1000  1000  1000  1000 1465368150          0 1465368150                393216                     0\r\n         0 1103626254  1600                 33904  1405  1006      2  1000  1000  1000  1000 1465915710          0 1465915710                 36864                     0\r\n         0   13139983  1600                393216  1183  1006      2  1000  1000  1000  1000 1460992697          0 1460992697                 90112                     0\r\n         0 1103134736  1600                524288 31704  1006      2  1000  1000  1000  1000 1465912955          0 1465912955                 40960                     0\r\n         0 1092419601  1600               8388608 15992  4833      2  1000  1000  1000  1000 1465984866 1465984866 1464004285               2383872                     0\r\n         0 1066008594  1600               7887360  1405  1006      2  1000  1000  1000  1000 1463145010          0 1463145010               7888896                     0\r\n         0 1086849043  1600                393216  1269  1006      2  1000  1000  1000  1000 1463486217          0 1463486217                393216                     0\r\n         0 1081835540  1600                393216  1176 31704      2  1000  1000  1000  1000 1465912954 1465912954 1463484456                131072                     0\r\n       112 1103659029   644                  4100  4744  5016      0     0     0     0     0 1465985147 1465985149 1465984872                     0                     0\r\n       113 1103691798   644                  4100  5014  5016      0     0     0     0     0 1465985147 1465985149 1465985125                     0                     0\r\n         0 1094877207  1600                393216 23562  7548      2  1000  1000  1000  1000 1465139904 1465139904 1464605255                393216                     0\r\n         0 1094975512  1600                393216 23562  7548      2  1000  1000  1000  1000 1465139904 1465139904 1464605255                393216                     0\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Beej&#8217;s Guide to Unix IPC &#8211; Shared Memory Segments Programming in C &#8211; IPC: Shared Memory System V IPC Oracle Programming Interfaces Guide &#8211; System V Shared Memory Don&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4920","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/4920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4920"}],"version-history":[{"count":5,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/4920\/revisions"}],"predecessor-version":[{"id":4926,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/4920\/revisions\/4926"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}