{"id":440,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:19","modified_gmt":"2020-08-22T20:26:19","slug":"this-is-the-page-title-toplevel-273","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=440","title":{"rendered":"Shared Memory"},"content":{"rendered":"\n<title>Shared Memory<\/title>\n<p>\nShared memory allows one or more processes to communicate via memory that appears\nin all of their virtual address spaces.\nThe pages of the virtual memory is referenced by page table entries in each of the\nsharing processes&#8217; page tables.\nIt does not have to be at the same address in all of the processes&#8217; virtual memory.\nAs with all System V IPC objects, access to shared memory areas is controlled via\nkeys and access rights checking.\nOnce the memory is being shared, there are no checks on how the processes use\nit.\nThey must rely on other mechanisms, for example System V semaphores, to synchronize\naccess to the memory.\n<p>\n<img decoding=\"async\" src=\"shm.gif\"><br \/>\n<p>\nSystem V IPC Shared Memory\n<p>\n<p>Each newly created shared memory area is represented by a <tt>shmid_ds<\/tt> data structure.\nThese are kept in the <tt>shm_segs<\/tt> vector.\n<p>\nThe <tt>shmid_ds<\/tt> data structure decribes how big the area of shared memory is, how\nmany processes are using it and information about how that shared memory is mapped  into\ntheir address spaces.\nIt is the creator of the shared memory that controls the access permissions to that\nmemory and whether its key is public or private.\nIf it has enough access rights it may also lock the shared memory into physical memory.\n<p>\nEach process that wishes to share the memory must attach to that virtual memory via a\nsystem call.\nThis creates a new <tt>vm_area_struct<\/tt> data structure describing the shared memory\nfor this process.\nThe process can choose where in its virtual address space the shared memory goes or\nit can let Linux choose a free area large enough.\nThe new <tt>vm_area_struct<\/tt> structure is put into the list of <tt>vm_area_struct<\/tt>\npointed at by the <tt>shmid_ds<\/tt>.\nThe <tt>vm_next_shared<\/tt> and <tt>vm_prev_shared<\/tt> pointers are used to link them together.\nThe virtual memory is not actually created during the attachment; it happens when the first\nprocess attempts to access it.\n<p>\nThe first time that a process accesses one of the pages of the shared virtual memory, a page\nfault will occur.\nWhen Linux fixes up that page fault it finds the <tt>vm_area_struct<\/tt> data structure\ndescribing it.\nThis contains pointers to handler routines for this type of shared virtual memory.\nThe shared memory page fault handling code looks in the list of page table entries for\nthis <tt>shmid_ds<\/tt> to see if one exists for this page of the shared virtual\nmemory.\nIf it does not exist, it will allocate a physical page and create a page table entry\nfor it.\n<p>\nThis entry is saved in the current process&#8217; page tables and the  <tt>shmid_ds<\/tt>..\nConsequently, when the next process that attempts to access this memory gets a\npage fault, the shared memory fault handling code will use this newly created\nphysical page for that process too.\nSo, the first process that accesses a page of the shared memory causes it to be\ncreated and thereafter access by the other processes cause that page to be added\ninto their virtual address spaces.\n<p>\nWhen processes no longer wish to share the virtual memory, they detach from it.\nSo long as other processes are still using the memory the detach only affects the\ncurrent process.\nIts <tt>vm_area_struct<\/tt> is removed from the <tt>shmid_ds<\/tt> data structure and\ndeallocated.\nThe current process&#8217;s page tables are updated to invalidate the area of virtual\nmemory that it once shared.\nWhen the last process sharing the memory detaches from it, the pages of the shared\nmemory current in physical memory are freed, as is the <tt>shmid_ds<\/tt> data structure\nfor this shared memory.\n<p>\nFurther complications arise when shared virtual memory is not locked into physical\nmemory.\nIn this case the pages of the shared memory may be swapped out to the system&#8217;s\nswap disk during periods of high memory usage.\nHow shared memory memory is swapped into and out of physical memory is described\nin the <site id=\"260\">section on memory management<\/site>.\n","protected":false},"excerpt":{"rendered":"<p>Shared Memory Shared memory allows one or more processes to communicate via memory that appears in all of their virtual address spaces. The pages of the virtual memory is referenced by page table entries in each of the sharing processes&#8217; &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=440\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-440","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=440"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/440\/revisions"}],"predecessor-version":[{"id":698,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/440\/revisions\/698"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}