{"id":198432,"date":"2024-11-14T18:21:27","date_gmt":"2024-11-14T10:21:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/198432\/"},"modified":"2024-11-14T18:21:27","modified_gmt":"2024-11-14T10:21:27","slug":"%e3%80%8clinux-%e7%b7%9a%e7%a8%8b%e7%9a%84%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98%e4%bd%bf%e7%94%a8%e3%80%8d-linux-%e7%b7%9a%e7%a8%8b-%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/198432\/","title":{"rendered":"\u300cLinux \u7dda\u7a0b\u7684\u5171\u4eab\u5167\u5b58\u4f7f\u7528\u300d (linux \u7dda\u7a0b \u5171\u4eab\u5167\u5b58)"},"content":{"rendered":"<h1 id=\"linux-%e7%b7%9a%e7%a8%8b%e7%9a%84%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98%e4%bd%bf%e7%94%a8-tIgeOBEkGb\">Linux \u7dda\u7a0b\u7684\u5171\u4eab\u5167\u5b58\u4f7f\u7528<\/h1>\n<p>\u5728\u591a\u7dda\u7a0b\u7de8\u7a0b\u4e2d\uff0c\u5171\u4eab\u5167\u5b58\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u9032\u7a0b\u9593\u901a\u4fe1\uff08IPC\uff09\u6a5f\u5236\u3002Linux \u4f5c\u70ba\u4e00\u500b\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u64cd\u4f5c\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u5be6\u73fe\u7dda\u7a0b\u4e4b\u9593\u7684\u5171\u4eab\u5167\u5b58\u3002\u672c\u6587\u5c07\u63a2\u8a0e Linux \u7dda\u7a0b\u7684\u5171\u4eab\u5167\u5b58\u4f7f\u7528\uff0c\u5305\u62ec\u5176\u539f\u7406\u3001\u512a\u52e2\u3001\u4ee5\u53ca\u5982\u4f55\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u5be6\u73fe\u3002<\/p>\n<h2 id=\"%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-tIgeOBEkGb\">\u5171\u4eab\u5167\u5b58\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5171\u4eab\u5167\u5b58\u5141\u8a31\u591a\u500b\u7dda\u7a0b\u6216\u9032\u7a0b\u8a2a\u554f\u540c\u4e00\u584a\u5167\u5b58\u5340\u57df\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6578\u64da\u8907\u88fd\u7684\u958b\u92b7\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u8207\u5176\u4ed6 IPC \u6a5f\u5236\uff08\u5982\u7ba1\u9053\u3001\u6d88\u606f\u968a\u5217\u7b49\uff09\u76f8\u6bd4\uff0c\u5171\u4eab\u5167\u5b58\u7684\u901f\u5ea6\u66f4\u5feb\uff0c\u56e0\u70ba\u6578\u64da\u4e0d\u9700\u8981\u5728\u5167\u5b58\u548c\u5167\u6838\u4e4b\u9593\u9032\u884c\u8907\u88fd\u3002<\/p>\n<h2 id=\"linux-%e4%b8%ad%e7%9a%84%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98%e5%af%a6%e7%8f%be-tIgeOBEkGb\">Linux \u4e2d\u7684\u5171\u4eab\u5167\u5b58\u5be6\u73fe<\/h2>\n<p>\u5728 Linux \u4e2d\uff0c\u5171\u4eab\u5167\u5b58\u4e3b\u8981\u901a\u904e POSIX \u5171\u4eab\u5167\u5b58\u548c System V \u5171\u4eab\u5167\u5b58\u5169\u7a2e\u65b9\u5f0f\u4f86\u5be6\u73fe\u3002<\/p>\n<h3 id=\"1-posix-%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98-tIgeOBEkGb\">1. POSIX \u5171\u4eab\u5167\u5b58<\/h3>\n<p>POSIX \u5171\u4eab\u5167\u5b58\u4f7f\u7528 <code>shm_open<\/code> \u548c <code>mmap<\/code> \u51fd\u6578\u4f86\u5275\u5efa\u548c\u6620\u5c04\u5171\u4eab\u5167\u5b58\u5340\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;sys\/mman.h&gt;\n#include &lt;unistd.h&gt;\n\n#define SHM_NAME \"\/my_shm\"\n#define SHM_SIZE 4096\n\nint main() {\n    int shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);\n    ftruncate(shm_fd, SHM_SIZE);\n    char *ptr = mmap(0, SHM_SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);\n    \n    sprintf(ptr, \"Hello, Shared Memory!\");\n    printf(\"%sn\", ptr);\n    \n    munmap(ptr, SHM_SIZE);\n    close(shm_fd);\n    shm_unlink(SHM_NAME);\n    return 0;\n}\n<\/code><\/pre>\n<h3 id=\"2-system-v-%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98-tIgeOBEkGb\">2. System V \u5171\u4eab\u5167\u5b58<\/h3>\n<p>System V \u5171\u4eab\u5167\u5b58\u4f7f\u7528 <code>shmget<\/code>\u3001<code>shmat<\/code> \u548c <code>shmdt<\/code> \u51fd\u6578\u4f86\u5275\u5efa\u548c\u4f7f\u7528\u5171\u4eab\u5167\u5b58\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/shm.h&gt;\n\n#define SHM_SIZE 4096\n\nint main() {\n    int shm_id = shmget(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | 0666);\n    char *ptr = shmat(shm_id, NULL, 0);\n    \n    sprintf(ptr, \"Hello, System V Shared Memory!\");\n    printf(\"%sn\", ptr);\n    \n    shmdt(ptr);\n    shmctl(shm_id, IPC_RMID, NULL);\n    return 0;\n}\n<\/code><\/pre>\n<h2 id=\"%e5%85%b1%e4%ba%ab%e5%85%a7%e5%ad%98%e7%9a%84%e5%84%aa%e5%8b%a2-tIgeOBEkGb\">\u5171\u4eab\u5167\u5b58\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>\u5171\u4eab\u5167\u5b58\u5141\u8a31\u591a\u500b\u7dda\u7a0b\u76f4\u63a5\u8a2a\u554f\u540c\u4e00\u584a\u5167\u5b58\uff0c\u907f\u514d\u4e86\u6578\u64da\u8907\u88fd\u7684\u958b\u92b7\u3002<\/li>\n<li><strong>\u4f4e\u5ef6\u9072\uff1a<\/strong>\u7531\u65bc\u4e0d\u9700\u8981\u9032\u884c\u4e0a\u4e0b\u6587\u5207\u63db\uff0c\u4f7f\u7528\u5171\u4eab\u5167\u5b58\u7684\u5ef6\u9072\u76f8\u5c0d\u8f03\u4f4e\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u53ef\u4ee5\u6839\u64da\u9700\u8981\u52d5\u614b\u8abf\u6574\u5171\u4eab\u5167\u5b58\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-tIgeOBEkGb\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5118\u7ba1\u5171\u4eab\u5167\u5b58\u5177\u6709\u8a31\u591a\u512a\u52e2\uff0c\u4f46\u5728\u4f7f\u7528\u6642\u4e5f\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u540c\u6b65\u554f\u984c\uff1a<\/strong>\u591a\u500b\u7dda\u7a0b\u540c\u6642\u8a2a\u554f\u5171\u4eab\u5167\u5b58\u6642\uff0c\u5fc5\u9808\u4f7f\u7528\u4e92\u65a5\u9396\u6216\u5176\u4ed6\u540c\u6b65\u6a5f\u5236\u4f86\u907f\u514d\u6578\u64da\u7af6\u722d\u3002<\/li>\n<li><strong>\u5167\u5b58\u7ba1\u7406\uff1a<\/strong>\u9700\u8981\u78ba\u4fdd\u5728\u4e0d\u518d\u4f7f\u7528\u5171\u4eab\u5167\u5b58\u6642\u91cb\u653e\u8cc7\u6e90\uff0c\u4ee5\u907f\u514d\u5167\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tIgeOBEkGb\">\u7e3d\u7d50<\/h2>\n<p>Linux \u7dda\u7a0b\u7684\u5171\u4eab\u5167\u5b58\u4f7f\u7528\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u9032\u7a0b\u9593\u901a\u4fe1\u65b9\u5f0f\uff0c\u9069\u5408\u9700\u8981\u5feb\u901f\u6578\u64da\u4ea4\u63db\u7684\u61c9\u7528\u5834\u666f\u3002\u7121\u8ad6\u662f\u4f7f\u7528 POSIX \u9084\u662f System V \u5171\u4eab\u5167\u5b58\uff0c\u958b\u767c\u8005\u90fd\u80fd\u5920\u5145\u5206\u5229\u7528\u5176\u6027\u80fd\u512a\u52e2\u3002\u5728\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u6642\uff0c\u4e86\u89e3\u9019\u4e9b\u6280\u8853\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Linux\u7dda\u7a0b\u4e2d\u7684\u5171\u4eab\u5167\u5b58\u4f7f\u7528\uff0c\u4e86\u89e3\u5982\u4f55\u9ad8\u6548\u5730\u5728\u591a\u7dda\u7a0b\u61c9\u7528\u4e2d\u5be6\u73fe\u6578\u64da\u5171\u4eab\u8207\u540c\u6b65\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-198432","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=198432"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198432\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=198432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=198432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=198432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}