{"id":261,"date":"2025-08-18T17:55:03","date_gmt":"2025-08-18T09:55:03","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=261"},"modified":"2025-11-24T16:38:25","modified_gmt":"2025-11-24T08:38:25","slug":"%e5%9f%ba%e4%ba%8e%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98%e5%ae%9e%e7%8e%b0%e8%bf%9b%e7%a8%8b%e9%97%b4%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=261","title":{"rendered":"\u57fa\u4e8e\u5171\u4eab\u5185\u5b58\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1"},"content":{"rendered":"\n<p>\u4ed3\u5e93\u5730\u5740<a href=\"https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/Shm\">https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/Shm<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5171\u4eab\u5185\u5b58\u7684\u539f\u7406\u4e0eOS\u7ba1\u7406<\/h2>\n\n\n\n<p>\u5171\u4eab\u5185\u5b58\u662f\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4e00\u79cd\u9ad8\u6548\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u673a\u5236\u3002\u6240\u6709\u7684\u5e95\u5c42\u7ba1\u7406\u5de5\u4f5c\u90fd\u7531\u64cd\u4f5c\u7cfb\u7edf\u81ea\u52a8\u5b8c\u6210\uff0c\u5305\u62ec\u5185\u5b58\u5206\u914d\u3001\u6620\u5c04\u3001\u6743\u9650\u63a7\u5236\u548c\u8d44\u6e90\u56de\u6536\u3002\u6211\u4eec\u53ea\u9700\u8981\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u5b8c\u6210\u521b\u5efa\u3001\u8fde\u63a5\u3001\u8bfb\u5199\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u53d6\u6d88\u5173\u8054\u5173\u7cfb\u540e\uff0cOS\u4f1a\u81ea\u52a8\u91ca\u653e\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u6620\u5c04\uff0c\u4f46\u5171\u4eab\u5185\u5b58\u672c\u8eab\u7684\u7269\u7406\u7a7a\u95f4\u53ea\u6709\u5728\u660e\u786e\u5220\u9664\u540e\u624d\u4f1a\u88ab\u91ca\u653e\u3002<br>\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5\u53ef\u4ee5\u88ab\u591a\u4e2a\u8fdb\u7a0b\u540c\u65f6\u6620\u5c04\u548c\u8bbf\u95ee\uff0c\u5b9e\u73b0\u6570\u636e\u5171\u4eab\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u7cfb\u7edf\u8c03\u7528\u4e0e\u53c2\u6570\u8bf4\u660e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5e38\u7528\u7cfb\u7edf\u8c03\u7528<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>shmget(key, size, shmflg)<\/code>\uff1a\u521b\u5efa\u6216\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"97\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-12.png\" alt=\"\" class=\"wp-image-263\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-12.png 446w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-12-300x65.png 300w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>shmat(shmid, addr, flag)<\/code>\uff1a\u5c06\u5171\u4eab\u5185\u5b58\u6620\u5c04\u5230\u8fdb\u7a0b\u865a\u62df\u5730\u5740\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"323\" height=\"129\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-13.png\" alt=\"\" class=\"wp-image-264\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-13.png 323w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-13-300x120.png 300w\" sizes=\"auto, (max-width: 323px) 100vw, 323px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>shmdt(addr)<\/code>\uff1a\u89e3\u9664\u6620\u5c04\u5173\u7cfb\uff08detach\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"341\" height=\"65\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-14.png\" alt=\"\" class=\"wp-image-265\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-14.png 341w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-14-300x57.png 300w\" sizes=\"auto, (max-width: 341px) 100vw, 341px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>shmctl(shmid, IPC_RMID, 0)<\/code>\uff1a\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"276\" height=\"133\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-15.png\" alt=\"\" class=\"wp-image-266\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u547d\u4ee4\u884c\u5de5\u5177<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ipcs -m<\/code>\uff1a\u67e5\u770b\u5f53\u524d\u7cfb\u7edf\u6240\u6709\u5171\u4eab\u5185\u5b58\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"198\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-16.png\" alt=\"\" class=\"wp-image-267\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-16.png 761w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-16-300x78.png 300w\" sizes=\"auto, (max-width: 761px) 100vw, 761px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ipcrm -m <shmid><\/code>\uff1a\u5220\u9664\u6307\u5b9a\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong><br>\u5373\u4f7f\u8fdb\u7a0b\u9000\u51fa\uff0c\u53ea\u8981\u6ca1\u6709\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5\uff0c\u8d44\u6e90\u4f1a\u4e00\u76f4\u5b58\u5728\uff0c\u751f\u547d\u5468\u671f\u968f\u5185\u6838\u3002<\/p>\n\n\n\n<p><strong>\u4e09\u3001\u4ee3\u7801\u4e3e\u4f8b<\/strong><\/p>\n\n\n\n<p>\u7528\u4e86\u5171\u4eab\u5185\u5b58\u4f5c\u4e3a\u6570\u636e\u4ea4\u6362\u533a\uff0cserver \u8fdb\u7a0b\u8d1f\u8d23\u521b\u5efa\u5171\u4eab\u5185\u5b58\u5e76\u8bfb\u53d6\u6570\u636e\uff0cclient \u8fdb\u7a0b\u8d1f\u8d23\u8fde\u63a5\u5171\u4eab\u5185\u5b58\u5e76\u5199\u5165\u6570\u636e\u3002<br>\u4e3a\u4fdd\u8bc1\u540c\u6b65\uff0c\u914d\u5408 FIFO \u7ba1\u9053\u5b9e\u73b0\u5524\u9192\u901a\u77e5<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5171\u4eab\u5185\u5b58\u7ba1\u7406\uff08Shm.hpp\uff09<br>Shm \u7c7b\u5c01\u88c5\u4e86\u5171\u4eab\u5185\u5b58\u7684\u521b\u5efa\u3001\u8fde\u63a5\u3001\u6620\u5c04\u548c\u9500\u6bc1\u3002<br>server \u7528 CREATER \u6a21\u5f0f\u521b\u5efa\uff0cclient \u7528 USER \u6a21\u5f0f\u8fde\u63a5\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u90e8\u5206\u4ee3\u7801\u4e3e\u4f8b\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  void Attach()\n    {\n        _start_mem = shmat(_shmid, nullptr, 0);\n        if ((long long)_start_mem < 0)\n        {\n            ERR_EXIT(\"shmat\");\n        }\n        printf(\"attach success\\n\");\n    }\n    void Detach()\n    {\n        int n = shmdt(_start_mem);\n        if (n == 0)\n        {\n            printf(\"detach success\\n\");\n        }\n    }\nvoid CreateHelper(int flg)\n    {\n        printf(\"key: 0x%x\\n\", _key);\n        \/\/ \u5171\u4eab\u5185\u5b58\u7684\u751f\u547d\u5468\u671f\uff0c\u968f\u5185\u6838\n        _shmid = shmget(_key, _size, flg);\n        if (_shmid < 0)\n        {\n            ERR_EXIT(\"shmget\");\n        }\n        printf(\"shmid: %d\\n\", _shmid);\n    }\nShm(const std::string &#038;pathname, int projid, const std::string &#038;usertype)\n        : _shmid(gdefaultid),\n          _size(gsize),\n          _start_mem(nullptr),\n          _usertype(usertype)\n    {\n        _key = ftok(pathname.c_str(), projid);\n        if (_key < 0)\n        {\n            ERR_EXIT(\"ftok\");\n        }\n        if (_usertype == CREATER)\n            Create();\n        else if (_usertype == USER)\n            Get();\n        else\n        {\n        }\n        Attach();\n    }\n    void *VirtualAddr()\n    {\n        printf(\"VirtualAddr: %p\\n\", _start_mem);\n        return _start_mem;\n    }\n    int Size()\n    {\n        return _size;\n    }\n    void Attr()\n    {\n        struct shmid_ds ds;\n        int n = shmctl(_shmid, IPC_STAT, &#038;ds); \/\/ ds:\u8f93\u51fa\u578b\u53c2\u6570\n        printf(\"shm_segsz: %ld\\n\", ds.shm_segsz);\n        printf(\"key: 0x%x\\n\", ds.shm_perm.__key);\n    }\n\n  <\/code><\/pre>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u9519\u8bef\u6b22\u996e\u6307\u51fa\uff01\uff01<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p><canvas width=\"0\" height=\"155\"><\/canvas><canvas width=\"0\" height=\"155\"><\/canvas><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ed3\u5e93\u5730\u5740https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/ma&#8230;<\/p>\n","protected":false},"author":1,"featured_media":263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":849,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions\/849"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/263"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}