{"id":35359,"date":"2025-05-04T13:49:46","date_gmt":"2025-05-04T05:49:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/35359.html"},"modified":"2025-05-04T13:49:46","modified_gmt":"2025-05-04T05:49:46","slug":"%e3%80%90linux%e3%80%91%e8%bf%9b%e7%a8%8b%e9%97%b4%e9%80%9a%e4%bf%a1-%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/35359.html","title":{"rendered":"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054942-6816fff64c3b4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u5171\u4eab\u5185\u5b58&#xff08;Shared Memory&#xff09;<\/li>\n<li>\n<ul>\n<li>\u4ec0\u4e48\u662f\u5171\u4eab\u5185\u5b58<\/li>\n<li>2. \u5171\u4eab\u5185\u5b58\u7684\u7279\u70b9<\/li>\n<li>3.\u5171\u4eab\u5185\u5b58\u7684\u4e3b\u8981\u51fd\u6570<\/li>\n<li>\n<ul>\n<li>3.1.shmget()<\/li>\n<li>3.2.shmat<\/li>\n<li>3.3.shmdt<\/li>\n<li>3.4.shmctl<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\u5171\u4eab\u5185\u5b58\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/li>\n<li>\n<ul>\n<li>ShareMemory.hpp<\/li>\n<li>Server.cc<\/li>\n<li>Client.cc<\/li>\n<\/ul>\n<\/li>\n<li>\u603b\u7ed3<\/li>\n<\/ul>\n<h2>\u5171\u4eab\u5185\u5b58&#xff08;Shared Memory&#xff09;<\/h2>\n<h3>\u4ec0\u4e48\u662f\u5171\u4eab\u5185\u5b58<\/h3>\n<p>\u5171\u4eab\u5185\u5b58&#xff08;Shared Memory&#xff09;\u662f\u4e00\u79cd \u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;IPC&#xff09; \u673a\u5236&#xff0c;\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58&#xff0c;\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u4ea4\u6362\u6548\u7387\u3002\u76f8\u6bd4\u5176\u4ed6 IPC \u65b9\u5f0f&#xff08;\u5982\u7ba1\u9053\u3001\u6d88\u606f\u961f\u5217\u7b49&#xff09;&#xff0c;\u5171\u4eab\u5185\u5b58\u5177\u6709 \u901f\u5ea6\u5feb\u3001\u4f4e\u5f00\u9500 \u7684\u4f18\u52bf&#xff0c;\u56e0\u4e3a\u6570\u636e\u76f4\u63a5\u5b58\u50a8\u5728\u5185\u5b58\u4e2d&#xff0c;\u800c\u65e0\u9700\u901a\u8fc7\u5185\u6838\u8fdb\u884c\u6570\u636e\u62f7\u8d1d\u3002<\/p>\n<h3>2. \u5171\u4eab\u5185\u5b58\u7684\u7279\u70b9<\/h3>\n<ul>\n<li>\u9ad8\u6548&#xff1a;\u6570\u636e\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u5171\u4eab&#xff0c;\u907f\u514d\u4e86\u8fdb\u7a0b\u95f4\u6570\u636e\u62f7\u8d1d\u7684\u5f00\u9500\u3002<\/li>\n<li>\u8fdb\u7a0b\u53ef\u89c1&#xff1a;\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u5757\u5171\u4eab\u5185\u5b58&#xff0c;\u5b9e\u73b0\u9ad8\u901f\u6570\u636e\u4f20\u8f93\u3002<\/li>\n<li>\u9700\u8981\u540c\u6b65\u673a\u5236&#xff1a;\u7531\u4e8e\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u5e76\u53d1\u8bbf\u95ee\u5171\u4eab\u5185\u5b58&#xff0c;\u901a\u5e38\u9700\u8981\u4f7f\u7528 \u4fe1\u53f7\u91cf&#xff08;Semaphore&#xff09; \u6216 \u4e92\u65a5\u9501&#xff08;Mutex&#xff09; \u6765\u9632\u6b62\u6570\u636e\u7ade\u4e89\u3002<\/li>\n<\/ul>\n<h3>3.\u5171\u4eab\u5185\u5b58\u7684\u4e3b\u8981\u51fd\u6570<\/h3>\n<table>\n<tr>\u51fd\u6570\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>shmget()<\/td>\n<td>\u521b\u5efa\u6216\u83b7\u53d6\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5<\/td>\n<\/tr>\n<tr>\n<td>shmat()<\/td>\n<td>\u5c06\u5171\u4eab\u5185\u5b58\u9644\u52a0\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4<\/td>\n<\/tr>\n<tr>\n<td>shmdt()<\/td>\n<td>\u89e3\u9664\u5171\u4eab\u5185\u5b58\u4e0e\u8fdb\u7a0b\u7684\u5173\u8054<\/td>\n<\/tr>\n<tr>\n<td>shmctl()<\/td>\n<td>\u63a7\u5236\u5171\u4eab\u5185\u5b58&#xff08;\u5220\u9664\u3001\u4fee\u6539\u6743\u9650\u7b49&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3.1.shmget()<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054943-6816fff7e4db1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> shmget\u8868\u793a\u83b7\u53d6\u5171\u4eab\u5185\u5b58&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570key\u8868\u793a\u5171\u4eab\u5185\u5b58\u7684\u952e\u503c&#xff0c;\u7528\u4e8e\u6807\u8bc6\u552f\u4e00\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u3002<\/p>\n<hr \/>\n<p>\u8fd9\u4e2a\u53c2\u6570\u7531\u7528\u6237\u4e2a\u4eba\u8bbe\u7f6e&#xff0c;\u4f46\u662f\u901a\u5e38\u7528ftok\u51fd\u6570\u6765\u83b7\u53d6key\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054944-6816fff806bd7.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> ftok\u51fd\u6570\u901a\u8fc7\u4e00\u5b9a\u7684\u7b97\u6cd5\u6765\u83b7\u53d6\u76f8\u5bf9\u4e0d\u4f1a\u91cd\u590d\u7684key\u503c&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8def\u5f84&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u968f\u673a\u586b\u4e00\u4e2a\u6570&#xff0c;\u901a\u8fc7\u7b97\u6cd5\u83b7\u53d6\u76f8\u5bf9\u552f\u4e00\u7684key\u503c\u3002<\/p>\n<hr \/>\n<p>shmget\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u5982\u4f55\u521b\u5efa\u5171\u4eab\u5185\u5b58\u548c\u8bbe\u7f6e\u5171\u4eab\u5185\u5b58\u7684\u6743\u9650\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054944-6816fff8275c9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u7b2c\u4e09\u4e2a\u53c2\u6570\u6709\u7279\u5b9a\u7684\u5b8f\u53ef\u4ee5\u9009\u62e9&#xff0c;\u7ea2\u6846\u6846\u8d77\u6765\u7684\u662f\u5e38\u7528\u7684\u4e24\u4e2a\u3002 IPC_CREAT&#xff1a;\u5355\u72ec\u4f7f\u7528\u8868\u793a\u83b7\u53d6\u5171\u4eab\u5185\u5b58&#xff0c;\u5982\u679c\u5b58\u5728\u5219\u62a5\u9519 IPC_CREAT | IPC_EXCL&#xff1a;\u8868\u793a\u521b\u5efa\u5171\u4eab\u5185\u5b58 IPC_EXCL&#xff1a;\u5355\u72ec\u4f7f\u7528\u6ca1\u6709\u610f\u4e49<\/p>\n<h4>3.2.shmat<\/h4>\n<p>\u5f53\u6211\u4eec\u83b7\u53d6\u5230\u5171\u4eab\u5185\u5b58\u7684\u65f6\u5019&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u5171\u4eab\u5185\u5b58\u6302\u63a5\u5230\u865a\u62df\u5185\u5b58\u5730\u5740\u5f53\u4e2d&#xff0c;\u8fd9\u65f6\u5c31\u9700\u8981\u7528\u5230\u8fd9\u4e2a\u63a5\u53e3\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054944-6816fff898bb2.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u7528\u4e0b\u9762\u7b80\u56fe\u8868\u793a\u6302\u63a5&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054944-6816fff8b1968.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> shmget\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570shmid\u8868\u793ashmget\u7684\u8fd4\u56de\u503c&#xff0c;\u4f1a\u8fd4\u56de\u4e00\u4e2ashmid&#xff0c;\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u865a\u62df\u5730\u5740&#xff0c;\u6302\u63a5\u5230\u6307\u5b9a\u7684\u865a\u62df\u5730\u5740\u4e0a&#xff0c;\u4f46\u662f\u4e00\u822c\u60c5\u51b5\u4e0b\u6211\u4eec\u90fd\u4f1a\u9ed8\u8ba4\u4f7f\u7528\u5206\u914d\u7684\u865a\u62df\u5730\u5740&#xff0c;\u6240\u4ee5\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e00\u822c\u60c5\u51b5\u4e0b\u90fd\u4f1a\u586bnullptr&#xff0c;\u7b2c\u4e09\u4e2a\u53c2\u6570\u8868\u793a\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u63a7\u5236\u6620\u5c04\u65b9\u5f0f&#xff08;\u5e38\u7528 0 \u6216 SHM_RDONLY&#xff09;\u3002<\/p>\n<h4>3.3.shmdt<\/h4>\n<p>\u53bb\u5173\u8054&#xff0c;\u548c\u4e0a\u4e00\u4e2a\u5173\u8054\u6070\u597d\u76f8\u53cd&#xff0c;\u4e00\u4e2a\u662f\u5173\u8054\u4e00\u4e2a\u662f\u53bb\u5173\u8054\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054944-6816fff8eb89d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>3.4.shmctl<\/h4>\n<p>\u8fd9\u4e2a\u51fd\u6570\u662f\u7528\u4e8e\u63a7\u5236\u5171\u4eab\u5185\u5b58\u7684&#xff0c;\u5728\u547d\u4ee4\u884c\u6211\u4eec\u4e00\u822c\u7528ipcrm -m shmid\u8fd9\u4e2a\u547d\u4ee4\u6765\u5220\u9664\u5171\u4eab\u5185\u5b58&#xff0c;\u4f46\u662f\u5728\u4ee3\u7801\u5c42\u9762&#xff0c;\u6211\u4eec\u4e00\u822c\u7528shmctl\u8fd9\u4e2a\u51fd\u6570\u6765\u63a7\u5236\u5171\u4eab\u5185\u5b58&#xff0c;\u53ef\u4ee5\u8fdb\u884c\u5220\u9664\u4fee\u6539\u6743\u9650\u7b49\u64cd\u4f5c\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054945-6816fff910499.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u6807\u5fd7\u4f4d\u8fdb\u884c\u4ec0\u4e48\u64cd\u4f5c&#xff0c;\u4e0b\u9762\u662f\u53ef\u4ee5\u8fdb\u884c\u7684\u64cd\u4f5c&#xff0c;\u7ea2\u6846\u6846\u8d77\u6765\u7684&#xff0c;\u8868\u793a\u5220\u9664\u5171\u4eab\u5185\u5b58&#xff0c;\u6211\u4eec\u53ef\u4ee5\u7528\u8fd9\u4e2a\u5b8f\u6765\u5b9e\u73b0\u5220\u9664\u5171\u4eab\u5185\u5b58\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054945-6816fff925bfa.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u7684\u4fe1\u606f&#xff0c;\u653e\u5728\u4e00\u4e2a\u7ed3\u6784\u4f53\u5f53\u4e2d&#xff0c;\u5982\u679c\u6211\u4eec\u4e0d\u9700\u8981\u83b7\u53d6\u76f4\u63a5\u4f20nullptr <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054945-6816fff9731b1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u63a5\u53e3\u8bb2\u5b8c\u4e86&#xff0c;\u63a5\u4e0b\u6765\u7528\u5171\u4eab\u5185\u5b58\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/p>\n<h2>\u5171\u4eab\u5185\u5b58\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/h2>\n<h3>ShareMemory.hpp<\/h3>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">pragma<\/span> <span class=\"token expression\">once<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/types.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cstdlib&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;ctime&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/ipc.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/shm.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">using<\/span> <span class=\"token keyword\">namespace<\/span> std<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> string gpath <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;\/home\/llllyrics\/112_class&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">int<\/span> gprojid <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0X6666<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">\/\/\u64cd\u4f5c\u7cfb\u7edf&#xff0c;\u7533\u8bf7\u7a7a\u95f4&#xff0c;\u662f\u6309\u7167\u5757\u4e3a\u5355\u4f4d\u7684&#xff1a;4kb&#xff0c;1kb&#xff0c;2kb&#xff0c;4mb&#8230;&#8230;.<\/span><br \/>\n<span class=\"token keyword\">int<\/span> gshmsize <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">4096<\/span><span class=\"token punctuation\">;<\/span><br \/>\nmode_t gmode <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0600<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u6743\u9650<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">int<\/span> CREATE <span class=\"token operator\">&#061;<\/span> IPC_CREAT <span class=\"token operator\">|<\/span> IPC_EXCL <span class=\"token operator\">|<\/span> gmode<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">int<\/span> GET <span class=\"token operator\">&#061;<\/span> IPC_CREAT<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">ShareMemory<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token function\">CreateShmHelper<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> flag<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/\u521b\u5efakey<\/span><br \/>\n        _key <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">ftok<\/span><span class=\"token punctuation\">(<\/span>gpath<span class=\"token punctuation\">.<\/span><span class=\"token function\">c_str<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>gprojid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>_key <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token comment\">\/\/\u521b\u5efa\u5931\u8d25<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            cerr<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;ftok error&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token comment\">\/\/\u8ba9server\u521b\u5efa\u5171\u4eab\u5185\u5b58&amp;&amp;\u83b7\u53d6<\/span><br \/>\n        <span class=\"token comment\">\/\/\u6ce8\u610f&#xff1a;\u5171\u4eab\u5185\u5b58\u4e5f\u6709\u6743\u9650&#xff01;\u5728\u5e94\u7528\u5c42\u548c\u6587\u4ef6\u5173\u8054\u5ea6\u4e0d\u5927&#xff0c;\u4f46\u662f\u5728\u5e95\u5c42\u548c\u6587\u4ef6\u5173\u8054\u5ea6\u5927<\/span><br \/>\n        _shmid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">shmget<\/span><span class=\"token punctuation\">(<\/span>_key<span class=\"token punctuation\">,<\/span>gshmsize<span class=\"token punctuation\">,<\/span>flag<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u521b\u5efa<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>_shmid <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token comment\">\/\/\u521b\u5efa\u5931\u8d25<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            cerr<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;shmget error&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">ShareMemory<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><span class=\"token function\">_shmid<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">_key<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><span class=\"token function\">_addr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">ShareMemory<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">void<\/span> <span class=\"token function\">CreateShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>_shmid <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token function\">CreateShmHelper<\/span><span class=\"token punctuation\">(<\/span>CREATE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">GetShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">CreateShmHelper<\/span><span class=\"token punctuation\">(<\/span>GET<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">AttachShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/\u5c06\u5171\u4eab\u5185\u5b58\u6302\u63a5\u5230\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u5f53\u4e2d<\/span><br \/>\n        _addr <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">shmat<\/span><span class=\"token punctuation\">(<\/span>_shmid<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u5c06\u5171\u4eab\u5185\u5b58\u6302\u63a5\u5230\u81ea\u5df1\u7684\u865a\u62df\u5730\u5740\u4e0a\u3002<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">long<\/span> <span class=\"token keyword\">long<\/span><span class=\"token punctuation\">)<\/span>_addr <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u6302\u63a5\u5931\u8d25\u8fd4\u56denullptr<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">DetachShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>_addr <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token function\">shmdt<\/span><span class=\"token punctuation\">(<\/span>_addr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;detach done:&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">DeleteShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> n <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">shmctl<\/span><span class=\"token punctuation\">(<\/span>_shmid<span class=\"token punctuation\">,<\/span>IPC_RMID<span class=\"token punctuation\">,<\/span><span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>n <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token punctuation\">{<\/span><br \/>\n            cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;delete failed&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n        cout<span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#034;delete shm done&#034;<\/span><span class=\"token operator\">&lt;&lt;<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span><span class=\"token operator\">*<\/span> <span class=\"token function\">GetAddr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> _addr<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">ShmMeta<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><\/p>\n<p>    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> _shmid<span class=\"token punctuation\">;<\/span><br \/>\n    key_t _key<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>_addr<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\nShareMemory shm<span class=\"token punctuation\">;<\/span><\/p>\n<h3>Server.cc<\/h3>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;ShareMemory.hpp&#034;<\/span><\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">CreateShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">AttachShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u63a5\u6536&#8212;-IPC<\/span><br \/>\n    <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> strinfo <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">GetAddr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u83b7\u53d6\u670d\u52a1\u5668\u7684\u865a\u62df\u5730\u5740<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u6253\u5370\u5171\u4eab\u5730\u5740\u4e2d\u7684\u5185\u5bb9<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;%s\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span>strinfo<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">DetachShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">DeleteShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>Client.cc<\/h3>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;ShareMemory.hpp&#034;<\/span><\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">GetShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">AttachShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5199\u5165&#8212;-IPC<\/span><br \/>\n    <span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span> strinfo <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">GetAddr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u865a\u62df\u5730\u5740<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> ch <span class=\"token operator\">&#061;<\/span> <span class=\"token char\">&#039;A&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span>ch <span class=\"token operator\">&lt;&#061;<\/span> <span class=\"token char\">&#039;Z&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        strinfo<span class=\"token punctuation\">[<\/span>ch<span class=\"token operator\">&#8211;<\/span><span class=\"token char\">&#039;A&#039;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> ch<span class=\"token punctuation\">;<\/span><span class=\"token comment\">\/\/\u8fd9\u91cc\u64cd\u4f5c\u5171\u4eab\u5185\u5b58\u7684\u65f6\u5019\u4e3a\u4ec0\u4e48\u6ca1\u6709\u7528\u7cfb\u7edf\u8c03\u7528&#xff1f;<\/span><br \/>\n        ch<span class=\"token operator\">&#043;&#043;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    shm<span class=\"token punctuation\">.<\/span><span class=\"token function\">DetachShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5171\u4eab\u5185\u5b58\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u673a\u5236&#xff0c;\u56e0\u5176\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u64cd\u4f5c\u6570\u636e&#xff0c;\u907f\u514d\u4e86\u6570\u636e\u62f7\u8d1d&#xff0c;\u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u3002\u901a\u8fc7 shmget\u3001shmat\u3001shmctl \u7b49\u51fd\u6570&#xff0c;Linux \u7cfb\u7edf\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u63a5\u53e3\u3002\u5c3d\u7ba1\u5171\u4eab\u5185\u5b58\u5177\u6709\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf&#xff0c;\u4f46\u7531\u4e8e\u5176\u6ca1\u6709\u5185\u5efa\u7684\u540c\u6b65\u673a\u5236&#xff0c;\u4f7f\u7528\u65f6\u5fc5\u987b\u7279\u522b\u6ce8\u610f\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\u95ee\u9898\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u7ed3\u5408\u4fe1\u53f7\u91cf\u3001\u6d88\u606f\u961f\u5217\u7b49\u540c\u6b65\u673a\u5236&#xff0c;\u5171\u4eab\u5185\u5b58\u53ef\u4ee5\u4e3a\u591a\u8fdb\u7a0b\u95f4\u63d0\u4f9b\u9ad8\u6548\u4e14\u7a33\u5b9a\u7684\u901a\u4fe1\u624b\u6bb5\u3002\u7136\u800c&#xff0c;\u5f00\u53d1\u8005\u9700\u8981\u6ce8\u610f\u8d44\u6e90\u7684\u7ba1\u7406\u4e0e\u6e05\u7406&#xff0c;\u4ee5\u514d\u9020\u6210\u5185\u5b58\u6cc4\u6f0f\u6216\u6570\u636e\u51b2\u7a81\u3002<\/p>\n<p>\u603b\u4e4b&#xff0c;\u5171\u4eab\u5185\u5b58\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5de5\u5177&#xff0c;\u4f46\u4f7f\u7528\u65f6\u9700\u8981\u5c0f\u5fc3\u8c28\u614e&#xff0c;\u786e\u4fdd\u6570\u636e\u540c\u6b65\u548c\u8d44\u6e90\u7ba1\u7406\u5f97\u5f53&#xff0c;\u624d\u80fd\u5145\u5206\u53d1\u6325\u5176\u4f18\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.2k\u6b21\uff0c\u70b9\u8d5e65\u6b21\uff0c\u6536\u85cf38\u6b21\u3002\u5171\u4eab\u5185\u5b58\uff08Shared Memory\uff09\u662f\u4e00\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u673a\u5236\uff0c\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58\uff0c\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u4ea4\u6362\u6548\u7387\u3002\u76f8\u6bd4\u5176\u4ed6 IPC \u65b9\u5f0f\uff08\u5982\u7ba1\u9053\u3001\u6d88\u606f\u961f\u5217\u7b49\uff09\uff0c\u5171\u4eab\u5185\u5b58\u5177\u6709\u901f\u5ea6\u5feb\u3001\u4f4e\u5f00\u9500\u7684\u4f18\u52bf\uff0c\u56e0\u4e3a\u6570\u636e\u76f4\u63a5\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u800c\u65e0\u9700\u901a\u8fc7\u5185\u6838\u8fdb\u884c\u6570\u636e\u62f7\u8d1d\u3002\u5171\u4eab\u5185\u5b58\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u673a\u5236\uff0c\u56e0\u5176\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u64cd\u4f5c\u6570\u636e\uff0c\u907f\u514d\u4e86\u6570\u636e\u62f7\u8d1d\uff0c\u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u3002\u901a\u8fc7shmgetshmatshmctl\u7b49\u51fd\u6570\uff0cLinux \u7cfb\u7edf\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u63a5\u53e3\u3002_\u5171\u4eab\u5185\u5b58\u8fdb\u7a0b\u95f4\u901a\u4fe1<\/p>\n","protected":false},"author":2,"featured_media":35349,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,58,3026,43],"topic":[],"class_list":{"0":"post-35359","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-server","8":"tag-c","9":"tag-linux","11":"tag-43"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/35359.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.2k\u6b21\uff0c\u70b9\u8d5e65\u6b21\uff0c\u6536\u85cf38\u6b21\u3002\u5171\u4eab\u5185\u5b58\uff08Shared Memory\uff09\u662f\u4e00\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u673a\u5236\uff0c\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58\uff0c\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u4ea4\u6362\u6548\u7387\u3002\u76f8\u6bd4\u5176\u4ed6 IPC \u65b9\u5f0f\uff08\u5982\u7ba1\u9053\u3001\u6d88\u606f\u961f\u5217\u7b49\uff09\uff0c\u5171\u4eab\u5185\u5b58\u5177\u6709\u901f\u5ea6\u5feb\u3001\u4f4e\u5f00\u9500\u7684\u4f18\u52bf\uff0c\u56e0\u4e3a\u6570\u636e\u76f4\u63a5\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u800c\u65e0\u9700\u901a\u8fc7\u5185\u6838\u8fdb\u884c\u6570\u636e\u62f7\u8d1d\u3002\u5171\u4eab\u5185\u5b58\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u673a\u5236\uff0c\u56e0\u5176\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u64cd\u4f5c\u6570\u636e\uff0c\u907f\u514d\u4e86\u6570\u636e\u62f7\u8d1d\uff0c\u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u3002\u901a\u8fc7shmgetshmatshmctl\u7b49\u51fd\u6570\uff0cLinux \u7cfb\u7edf\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u63a5\u53e3\u3002_\u5171\u4eab\u5185\u5b58\u8fdb\u7a0b\u95f4\u901a\u4fe1\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/35359.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-04T05:49:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054942-6816fff64c3b4.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/35359.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/35359.html\",\"name\":\"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-04T05:49:46+00:00\",\"dateModified\":\"2025-05-04T05:49:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/35359.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/35359.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/35359.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/35359.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4.2k\u6b21\uff0c\u70b9\u8d5e65\u6b21\uff0c\u6536\u85cf38\u6b21\u3002\u5171\u4eab\u5185\u5b58\uff08Shared Memory\uff09\u662f\u4e00\u79cd\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u673a\u5236\uff0c\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58\uff0c\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u4ea4\u6362\u6548\u7387\u3002\u76f8\u6bd4\u5176\u4ed6 IPC \u65b9\u5f0f\uff08\u5982\u7ba1\u9053\u3001\u6d88\u606f\u961f\u5217\u7b49\uff09\uff0c\u5171\u4eab\u5185\u5b58\u5177\u6709\u901f\u5ea6\u5feb\u3001\u4f4e\u5f00\u9500\u7684\u4f18\u52bf\uff0c\u56e0\u4e3a\u6570\u636e\u76f4\u63a5\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u800c\u65e0\u9700\u901a\u8fc7\u5185\u6838\u8fdb\u884c\u6570\u636e\u62f7\u8d1d\u3002\u5171\u4eab\u5185\u5b58\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u673a\u5236\uff0c\u56e0\u5176\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u64cd\u4f5c\u6570\u636e\uff0c\u907f\u514d\u4e86\u6570\u636e\u62f7\u8d1d\uff0c\u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\u3002\u901a\u8fc7shmgetshmatshmctl\u7b49\u51fd\u6570\uff0cLinux \u7cfb\u7edf\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u63a5\u53e3\u3002_\u5171\u4eab\u5185\u5b58\u8fdb\u7a0b\u95f4\u901a\u4fe1","og_url":"https:\/\/www.wsisp.com\/helps\/35359.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-04T05:49:46+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504054942-6816fff64c3b4.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/35359.html","url":"https:\/\/www.wsisp.com\/helps\/35359.html","name":"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-04T05:49:46+00:00","dateModified":"2025-05-04T05:49:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/35359.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/35359.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/35359.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u3011\u8fdb\u7a0b\u95f4\u901a\u4fe1\u2014\u2014\u5171\u4eab\u5185\u5b58"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/35359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=35359"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/35359\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/35349"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=35359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=35359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=35359"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=35359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}