{"id":17025,"date":"2025-04-18T23:27:58","date_gmt":"2025-04-18T15:27:58","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/17025.html"},"modified":"2025-04-18T23:27:58","modified_gmt":"2025-04-18T15:27:58","slug":"%e3%80%90linux%e3%80%91systemv%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/17025.html","title":{"rendered":"\u3010Linux\u3011systemV\u5171\u4eab\u5185\u5b58"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152755-68026f7bc527b.jpg\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4e2a\u4eba\u4e3b\u9875~<\/p>\n<hr \/>\n<h4>systemV\u5171\u4eab\u5185\u5b58<\/h4>\n<ul>\n<li>\u4e00\u3001\u5de5\u4f5c\u539f\u7406<\/li>\n<li>\u4e8c\u3001\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3<\/li>\n<li>\n<ul>\n<li>1\u3001\u7533\u8bf7\u5171\u4eab\u5185\u5b58<\/li>\n<li>\n<ul>\n<li>&#xff08;\u4e00&#xff09;key\u7684\u83b7\u53d6<\/li>\n<li>&#xff08;\u4e8c&#xff09;\u5171\u4eab\u5185\u5b58\u7684\u7533\u8bf7<\/li>\n<\/ul>\n<\/li>\n<li>2\u3001\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u8fde\u63a5\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4<\/li>\n<li>3\u3001\u5c06\u5185\u5b58\u5171\u4eab\u6bb5\u4e0e\u5f53\u524d\u8fdb\u7a0b\u8131\u79bb<\/li>\n<li>4\u3001\u63a7\u5236\u5171\u4eab\u5185\u5b58<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001\u5f00\u59cb\u901a\u4fe1<\/li>\n<li>\n<ul>\n<li>1\u3001comm.hpp<\/li>\n<li>2\u3001processa.cpp<\/li>\n<li>3\u3001processb.cpp<\/li>\n<\/ul>\n<\/li>\n<li>\u56db\u3001\u5176\u4ed6\u95ee\u9898<\/li>\n<\/ul>\n<h2>\u4e00\u3001\u5de5\u4f5c\u539f\u7406<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152755-68026f7be3c4d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u64cd\u4f5c\u7cfb\u7edf\u5728\u7269\u7406\u5185\u5b58\u4e0a\u7533\u8bf7\u4e00\u5757\u7a7a\u95f4&#xff0c;\u7136\u540e\u5c06\u7533\u8bf7\u5230\u7684\u7a7a\u95f4\u901a\u8fc7\u9875\u8868\u6620\u5c04\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4mm_struct\u7684\u5171\u4eab\u533a\u4e2d&#xff0c;\u7136\u540e\u8fd4\u56de\u865a\u62df\u5730\u5740\u4f9b\u7a0b\u5e8f\u4f7f\u7528&#xff0c;\u5982\u679c\u591a\u4e2a\u8fdb\u7a0b\u7533\u8bf7\u7684\u662f\u540c\u4e00\u5757\u7269\u7406\u7a7a\u95f4&#xff0c;\u90a3\u4e48\u5b83\u4eec\u5c31\u53ef\u4ee5\u8fdb\u884c\u901a\u4fe1 \u7531\u4e8e\u540c\u4e00\u65f6\u95f4\u53ef\u80fd\u6709\u591a\u7ec4\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1&#xff0c;\u6240\u4ee5\u7cfb\u7edf\u5f53\u4e2d\u53ef\u80fd\u5b58\u5728\u591a\u4e2a\u5171\u4eab\u5185\u5b58\u5757&#xff0c;\u6240\u4ee5\u64cd\u4f5c\u7cfb\u7edf\u8981\u628a\u8fd9\u4e9b\u5185\u5b58\u7ba1\u7406\u8d77\u6765&#xff0c;\u6240\u4ee5\u5185\u6838\u4e2d\u4f1a\u6709\u4e00\u4e2a\u7ed3\u6784\u4f53\u6765\u63cf\u8ff0\u5171\u4eab\u5185\u5b58<\/p>\n<h2>\u4e8c\u3001\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3<\/h2>\n<h3>1\u3001\u7533\u8bf7\u5171\u4eab\u5185\u5b58<\/h3>\n<h4>&#xff08;\u4e00&#xff09;key\u7684\u83b7\u53d6<\/h4>\n<p><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;sys\/ipc.h&gt;<\/span><\/span><br \/>\n<span class=\"token class-name\">key_t<\/span> <span class=\"token function\">ftok<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>pathname<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> proj_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;\u6210\u529fftok \u4f1a\u5c06proj_id\u4e0epathname\u5bf9\u5e94\u7684\u6587\u4ef6\u4fe1\u606f\u7ed3\u5408\u8d77\u6765\u751f\u6210\u6700\u7ec8\u7684key\u503c&#xff0c;\u5931\u8d25\u8fd4\u56de-1<\/p>\n<p>pathname&#xff1a;\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u7684\u8def\u5f84\u540d proj_id&#xff1a;\u7531\u7528\u6237\u6307\u5b9a\u7684\u975e\u96f6\u6574\u6570&#xff0c;\u901a\u5e38\u662f\u4e00\u4e2a\u5355\u5b57\u7b26&#xff08;\u56e0\u4e3a\u53ea\u6709\u4f4e 8 \u4f4d\u4f1a\u88ab\u4f7f\u7528&#xff09;&#xff0c;<\/p>\n<p>\u53ea\u8981\u8fd9\u4e24\u4e2a\u624d\u53c2\u6570\u4e00\u81f4&#xff0c;\u90a3\u4e48\u751f\u6210\u7684key\u503c\u5c31\u4e00\u5b9a\u4e00\u81f4<\/p>\n<p>key\u503c\u662f\u6211\u4eec\u5c11\u89c1\u7684\u7531\u6211\u4eec\u81ea\u5df1\u4f20\u53c2\u751f\u6210\u7684\u56fa\u5b9a\u7684\u503c&#xff0c;Linux\u5728\u6d89\u53ca\u5185\u5b58\u65b9\u9762\u7684\u65f6\u5019&#xff0c;\u901a\u5e38\u662f\u64cd\u4f5c\u7cfb\u7edf\u4ee3\u4e3a\u5904\u7406&#xff0c;\u8fd9\u91cc\u5176\u5b9e\u662f\u56e0\u4e3a\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u8fdb\u7a0bAB&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u751f\u6210\u4e00\u4e2akey\u7ed9\u5230A&#xff0c;\u4f46\u662fB\u4e5f\u9700\u8981\u8fd9\u4e2akey&#xff0c;\u4f46\u662f\u64cd\u4f5c\u7cfb\u7edf\u662f\u4e0d\u77e5\u9053\u54ea\u4e24\u4e2a\u8fdb\u7a0b\u8981\u5efa\u7acb\u4fe1\u9053\u8fdb\u884c\u901a\u4fe1\u7684&#xff0c;\u53ea\u6709\u7a0b\u5e8f\u5458\u77e5\u9053\u6211\u4eec\u54ea\u4e24\u4e2a\u8fdb\u7a0b\u8981\u5efa\u7acb\u4fe1\u9053\u8fdb\u884c\u901a\u4fe1&#xff0c;\u56e0\u4e3a\u5728\u5efa\u7acb\u4fe1\u9053\u4e4b\u524d\u8fdb\u7a0b\u4e4b\u95f4\u662f\u76f8\u4e92\u72ec\u7acb\u7684&#xff0c;\u6240\u4ee5\u5c31\u4e0d\u80fd\u7531\u64cd\u4f5c\u7cfb\u7edf\u6765\u5206\u914dkey\u503c<\/p>\n<h4>&#xff08;\u4e8c&#xff09;\u5171\u4eab\u5185\u5b58\u7684\u7533\u8bf7<\/h4>\n<p>shmget\u51fd\u6570\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5728\u5185\u6838\u4e2d\u521b\u5efa\u6216\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26<\/p>\n<p><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\">int<\/span> <span class=\"token function\">shmget<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">key_t<\/span> key<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">size_t<\/span> size<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> shmflg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd4\u56de\u503c:\u6210\u529f\u8fd4\u56de\u4e00\u4e2a\u975e\u8d1f\u6574\u6570&#xff0c;\u5373\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7801&#xff08;shmid&#xff09;&#xff0c;\u5931\u8d25\u8fd4\u56de-1<\/p>\n<p>key:key\u662f\u4e00\u4e2a\u6570\u5b57&#xff0c;\u5b83\u662f\u4e0d\u540c\u5171\u4eab\u5185\u5b58\u8ba9\u81ea\u5df1\u5177\u5907\u552f\u4e00\u6027\u7684\u6807\u8bc6<\/p>\n<p>size:\u5171\u4eab\u5185\u5b58\u5927\u5c0f&#xff0c;\u5355\u4f4d\u4e3a\u5b57\u8282&#xff0c;\u4e00\u822c\u6700\u597d\u662f4096\u7684\u6574\u6570\u500d&#xff0c;\u5b83\u6309\u7167\u9875\u7533\u8bf7&#xff0c;\u5373\u9700\u89814097\u5b57\u8282\u5c31\u7533\u8bf74096*2\u4e2a\u5b57\u8282<\/p>\n<p>shmflg\u662f\u4e00\u4e2a\u6807\u8bc6\u7b26<\/p>\n<table>\n<tr>shmflg\u542b\u4e49\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>IPC_CREAT<\/td>\n<td>\u521b\u5efa\u6807\u5fd7<\/td>\n<td>\u5982\u679c\u6307\u5b9a\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u4e0d\u5b58\u5728&#xff0c;\u5c31\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff1b;\u82e5\u5df2\u5b58\u5728&#xff0c;\u5219\u76f4\u63a5\u8fd4\u56de\u5176\u6807\u8bc6\u7b26&#xff0c;\u5e38\u7528\u4e8e\u5728\u4e0d\u786e\u5b9a\u5171\u4eab\u5185\u5b58\u662f\u5426\u5b58\u5728\u65f6\u521b\u5efa\u6216\u83b7\u53d6\u5b83<\/td>\n<\/tr>\n<tr>\n<td>IPC_EXCL<\/td>\n<td>\u6392\u4ed6\u6807\u5fd7<\/td>\n<td>\u9700\u548c IPC_CREAT \u4e00\u8d77\u4f7f\u7528&#xff0c;\u82e5\u5171\u4eab\u5185\u5b58\u5df2\u5b58\u5728&#xff0c;shmget \u8c03\u7528\u5931\u8d25&#xff0c;errno \u8bbe\u4e3a EEXIST&#xff0c;\u53ef\u786e\u4fdd\u65b0\u521b\u5efa\u5171\u4eab\u5185\u5b58<\/td>\n<\/tr>\n<tr>\n<td>IPC_NOWAIT<\/td>\n<td>\u975e\u963b\u585e\u6807\u5fd7<\/td>\n<td>\u5f53\u64cd\u4f5c\u4e0d\u80fd\u7acb\u5373\u5b8c\u6210\u65f6&#xff0c;\u4e0d\u963b\u585e\u8c03\u7528\u8fdb\u7a0b&#xff0c;\u76f4\u63a5\u8fd4\u56de\u9519\u8bef&#xff0c;\u4f8b\u5982\u5728\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u65f6&#xff0c;\u82e5\u5f53\u524d\u8d44\u6e90\u65e0\u6cd5\u7acb\u5373\u83b7\u53d6&#xff0c;\u4e0d\u7b49\u5f85\u800c\u76f4\u63a5\u8fd4\u56de\u9519\u8bef<\/td>\n<\/tr>\n<tr>\n<td>SHM_HUGETLB<\/td>\n<td>\u5927\u9875\u6807\u5fd7<\/td>\n<td>\u5c1d\u8bd5\u4f7f\u7528\u5927\u9875\u5185\u5b58\u5206\u914d\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u5927\u9875\u5185\u5b58\u53ef\u51cf\u5c11\u9875\u8868\u9879\u6570\u91cf&#xff0c;\u964d\u4f4e\u5185\u5b58\u7ba1\u7406\u5f00\u9500&#xff0c;\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd&#xff0c;\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>SHM_NORESERVE<\/td>\n<td>\u4e0d\u4fdd\u7559\u4ea4\u6362\u7a7a\u95f4\u6807\u5fd7<\/td>\n<td>\u4e0d\u9884\u5148\u4e3a\u5171\u4eab\u5185\u5b58\u6bb5\u4fdd\u7559\u4ea4\u6362\u7a7a\u95f4&#xff0c;\u6b63\u5e38\u521b\u5efa\u5171\u4eab\u5185\u5b58\u65f6&#xff0c;\u7cfb\u7edf\u4f1a\u9884\u7559\u4ea4\u6362\u7a7a\u95f4\u4ee5\u9632\u5185\u5b58\u4e0d\u8db3&#xff0c;\u4f7f\u7528\u6b64\u6807\u5fd7\u53ef\u8282\u7701\u4ea4\u6362\u7a7a\u95f4&#xff0c;\u4f46\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u7d27\u5f20\u65f6\u51fa\u73b0\u9875\u9762\u4ea4\u6362\u95ee\u9898<\/td>\n<\/tr>\n<tr>\n<td>\u6743\u9650\u6807\u5fd7&#xff08;\u5982 0600\u30010666 \u7b49&#xff09;<\/td>\n<td>\u6743\u9650\u8bbe\u7f6e<\/td>\n<td>\u7c7b\u4f3c\u6587\u4ef6\u6743\u9650\u8bbe\u7f6e&#xff0c;\u7528\u4e8e\u89c4\u5b9a\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u8bbf\u95ee\u6743\u9650&#xff0c;0600 \u8868\u793a\u53ea\u6709\u6240\u6709\u8005\u6709\u8bfb\u5199\u6743\u9650&#xff0c;0666 \u8868\u793a\u6240\u6709\u7528\u6237\u90fd\u6709\u8bfb\u5199\u6743\u9650<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2\u3001\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u8fde\u63a5\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4<\/h3>\n<p>shmat\u51fd\u6570\u7684\u6838\u5fc3\u4f5c\u7528\u662f\u5728\u8c03\u7528\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u548c\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u7269\u7406\u5185\u5b58\u4e4b\u95f4\u5efa\u7acb\u6620\u5c04\u5173\u7cfb&#xff0c;\u5728\u8c03\u7528shmget\u51fd\u6570\u65f6&#xff0c;\u867d\u7136\u521b\u5efa\u6216\u83b7\u53d6\u4e86\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26&#xff0c;\u4f46\u8fdb\u7a0b\u8fd8\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u8be5\u5171\u4eab\u5185\u5b58&#xff0c;\u53ea\u6709\u901a\u8fc7shmat\u51fd\u6570\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u540e&#xff0c;\u8fdb\u7a0b\u624d\u80fd\u50cf\u8bbf\u95ee\u666e\u901a\u5185\u5b58\u4e00\u6837\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u6bb5\u4e2d\u7684\u6570\u636e<\/p>\n<p><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;sys\/shm.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span><span class=\"token function\">shmat<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> shmid<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>shmaddr<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> shmflg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u4e00\u4e2a\u6307\u9488&#xff0c;\u6307\u5411\u5171\u4eab\u5185\u5b58\u7684\u8d77\u59cb\u5730\u5740&#xff0c;\u5931\u8d25\u8fd4\u56de -1<\/p>\n<p>shmid&#xff1a;\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26&#xff0c;\u7528\u4e8e\u6307\u5b9a\u8981\u9644\u52a0\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u5373shmget\u7684\u8fd4\u56de\u503c<\/p>\n<p>shmaddr&#xff1a;\u6307\u5b9a\u5171\u4eab\u5185\u5b58\u6bb5\u8981\u9644\u52a0\u5230\u7684\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u7684\u5730\u5740&#xff0c;\u5982\u679c\u4e3a NULL&#xff0c;\u5219\u7531\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u5408\u9002\u7684\u5730\u5740<\/p>\n<p>shmflg&#xff1a;\u6807\u8bc6\u7b26<\/p>\n<table>\n<tr>\u7c7b\u578b\u542b\u4e49\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>SHM_RDONLY<\/td>\n<td>\u5171\u4eab\u5185\u5b58\u53ea\u8bfb<\/td>\n<td>\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4&#xff0c;\u8fdb\u7a0b\u53ea\u80fd\u8bfb\u53d6\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u6570\u636e&#xff0c;\u4e0d\u80fd\u8fdb\u884c\u5199\u64cd\u4f5c&#xff0c;\u589e\u5f3a\u6570\u636e\u5b89\u5168\u6027&#xff0c;\u9002\u7528\u4e8e\u591a\u8fdb\u7a0b\u5171\u4eab\u53ea\u8bfb\u6570\u636e\u7684\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td>SHM_RND<\/td>\n<td>\u5730\u5740\u820d\u5165<\/td>\n<td>\u5f53 shmaddr \u53c2\u6570\u4e0d\u4e3a NULL \u65f6&#xff0c;\u5c06 shmaddr \u5411\u4e0b\u820d\u5165\u5230\u4e00\u4e2a\u5408\u9002\u7684\u5185\u5b58\u8fb9\u754c&#xff08;\u901a\u5e38\u662f\u7cfb\u7edf\u9875\u9762\u5927\u5c0f\u7684\u6574\u6570\u500d&#xff09;&#xff0c;\u4fdd\u8bc1\u5171\u4eab\u5185\u5b58\u7684\u6b63\u786e\u9644\u52a0<\/td>\n<\/tr>\n<tr>\n<td>SHM_REMAP<\/td>\n<td>\u91cd\u65b0\u6620\u5c04<\/td>\n<td>\u5982\u679c\u5171\u4eab\u5185\u5b58\u6bb5\u5df2\u7ecf\u88ab\u9644\u52a0\u5230\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4&#xff0c;\u4f7f\u7528\u6b64\u6807\u5fd7\u53ef\u4ee5\u91cd\u65b0\u6620\u5c04\u8be5\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u5e38\u7528\u4e8e\u66f4\u65b0\u5171\u4eab\u5185\u5b58\u7684\u6620\u5c04\u5173\u7cfb<\/td>\n<\/tr>\n<tr>\n<td>SHM_EXEC<\/td>\n<td>\u53ef\u6267\u884c\u6743\u9650<\/td>\n<td>\u5141\u8bb8\u5728\u5171\u4eab\u5185\u5b58\u6bb5\u4e0a\u6267\u884c\u7a0b\u5e8f\u6307\u4ee4&#xff0c;\u4e0d\u8fc7\u5e76\u975e\u6240\u6709\u7cfb\u7edf\u90fd\u652f\u6301\u8be5\u6807\u5fd7&#xff0c;\u9002\u7528\u4e8e\u9700\u8981\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u6267\u884c\u4ee3\u7801\u7684\u7279\u6b8a\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td>SHM_COPY<\/td>\n<td>\u521b\u5efa\u79c1\u6709\u526f\u672c<\/td>\n<td>\u67d0\u4e9b\u7cfb\u7edf\u652f\u6301\u8be5\u6807\u5fd7&#xff0c;\u5c1d\u8bd5\u521b\u5efa\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u4e00\u4e2a\u79c1\u6709\u526f\u672c&#xff0c;\u540e\u7eed\u5bf9\u8be5\u526f\u672c\u7684\u4fee\u6539\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u8fdb\u7a0b\u770b\u5230\u7684\u5171\u4eab\u5185\u5b58\u5185\u5bb9&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u8fdb\u7a0b\u5bf9\u5171\u4eab\u5185\u5b58\u7684\u72ec\u7acb\u4fee\u6539<\/td>\n<\/tr>\n<tr>\n<td>SHM_ANON<\/td>\n<td>\u533f\u540d\u5171\u4eab\u5185\u5b58<\/td>\n<td>\u90e8\u5206\u7cfb\u7edf\u652f\u6301\u8be5\u6807\u5fd7&#xff0c;\u7528\u4e8e\u521b\u5efa\u533f\u540d\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u6b64\u65f6 shmid \u53c2\u6570\u4f1a\u88ab\u5ffd\u7565&#xff0c;\u53ef\u7ed3\u5408 shmaddr \u4f7f\u7528&#xff0c;\u5e38\u7528\u4e8e\u7236\u5b50\u8fdb\u7a0b\u95f4\u7684\u5185\u5b58\u5171\u4eab<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3\u3001\u5c06\u5185\u5b58\u5171\u4eab\u6bb5\u4e0e\u5f53\u524d\u8fdb\u7a0b\u8131\u79bb<\/h3>\n<p>\u5f53\u8fdb\u7a0b\u8c03\u7528shmat\u51fd\u6570\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u540e&#xff0c;\u7cfb\u7edf\u4f1a\u5728\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u548c\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u7269\u7406\u5185\u5b58\u4e4b\u95f4\u5efa\u7acb\u6620\u5c04\u5173\u7cfb&#xff0c;\u4f7f\u5f97\u8fdb\u7a0b\u53ef\u4ee5\u50cf\u8bbf\u95ee\u666e\u901a\u5185\u5b58\u4e00\u6837\u8bbf\u95ee\u5171\u4eab\u5185\u5b58&#xff0c;\u800cshmdt\u51fd\u6570\u7684\u6838\u5fc3\u4f5c\u7528\u5c31\u662f\u89e3\u9664\u8fd9\u79cd\u6620\u5c04\u5173\u7cfb<\/p>\n<p><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;sys\/shm.h&gt;<\/span><\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">shmdt<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>shmaddr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de0&#xff0c;\u5931\u8d25\u8fd4\u56de-1<\/p>\n<p>shmaddr&#xff1a;shmat\u8fd4\u56de\u7684\u6307\u9488<\/p>\n<h3>4\u3001\u63a7\u5236\u5171\u4eab\u5185\u5b58<\/h3>\n<p>\u901a\u8fc7cmd\u63a7\u5236\u5171\u4eab\u5185\u5b58<\/p>\n<p><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\">int<\/span> <span class=\"token function\">shmctl<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> shmid<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> cmd<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">shmid_ds<\/span> <span class=\"token operator\">*<\/span>buf<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de0&#xff0c;\u5931\u8d25\u8fd4\u56de-1<\/p>\n<p>shmid&#xff1a;\u540c\u4e0a<\/p>\n<p>cmd&#xff1a;\u5c06\u8981\u91c7\u53d6\u7684\u52a8\u4f5c<\/p>\n<table>\n<tr>\u7c7b\u578b\u542b\u4e49\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>IPC_STAT<\/td>\n<td>\u83b7\u53d6\u72b6\u6001\u4fe1\u606f<\/td>\n<td>\u5c06\u4e0e shmid \u5173\u8054\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5f53\u524d\u72b6\u6001\u4fe1\u606f\u590d\u5236\u5230 buf \u6307\u5411\u7684 struct shmid_ds \u7ed3\u6784\u4f53\u4e2d&#xff0c;\u8fd9\u4e2a\u7ed3\u6784\u4f53\u5305\u542b\u4e86\u5982\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5927\u5c0f\u3001\u6240\u6709\u8005\u3001\u6743\u9650\u3001\u521b\u5efa\u65f6\u95f4\u3001\u6700\u540e\u8bbf\u95ee\u65f6\u95f4\u7b49\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>IPC_SET<\/td>\n<td>\u8bbe\u7f6e\u72b6\u6001\u4fe1\u606f<\/td>\n<td>\u4f7f\u7528 buf \u6307\u5411\u7684 struct shmid_ds \u7ed3\u6784\u4f53\u4e2d\u7684\u503c\u6765\u66f4\u65b0\u4e0e shmid \u5173\u8054\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u90e8\u5206\u72b6\u6001\u4fe1\u606f&#xff0c;\u53ef\u4ee5\u66f4\u65b0\u7684\u4fe1\u606f\u5305\u62ec\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6240\u6709\u8005\u3001\u6743\u9650\u7b49<\/td>\n<\/tr>\n<tr>\n<td>IPC_RMID<\/td>\n<td>\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5<\/td>\n<td>\u6807\u8bb0\u4e0e shmid \u5173\u8054\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u4e3a\u5220\u9664\u72b6\u6001&#xff0c;\u5f53\u6700\u540e\u4e00\u4e2a\u4f7f\u7528\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u8fdb\u7a0b\u5206\u79bb\u5b83\u4e4b\u540e&#xff0c;\u7cfb\u7edf\u4f1a\u771f\u6b63\u91ca\u653e\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u6240\u5360\u7528\u7684\u8d44\u6e90&#xff0c;\u6b64\u65f6 buf \u53c2\u6570\u4f1a\u88ab\u5ffd\u7565&#xff0c;\u901a\u5e38\u4f20\u9012 NULL<\/td>\n<\/tr>\n<tr>\n<td>IPC_INFO<\/td>\n<td>\u83b7\u53d6\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u4fe1\u606f<\/td>\n<td>\u83b7\u53d6\u7cfb\u7edf\u8303\u56f4\u5185\u7684\u5171\u4eab\u5185\u5b58\u8d44\u6e90\u4fe1\u606f&#xff0c;\u8fd9\u4e9b\u4fe1\u606f\u4f1a\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u7531\u7cfb\u7edf\u5b9a\u4e49\u7684\u7279\u5b9a\u7ed3\u6784\u4f53\u4e2d&#xff08;\u901a\u5e38\u4e0d\u662f struct shmid_ds&#xff09;&#xff0c;buf \u5e94\u6307\u5411\u8be5\u7ed3\u6784\u4f53&#xff0c;\u7528\u4e8e\u63a5\u6536\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>SHM_INFO<\/td>\n<td>\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\u4fe1\u606f<\/td>\n<td>\u83b7\u53d6\u7cfb\u7edf\u4e2d\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u76f8\u5173\u7edf\u8ba1\u4fe1\u606f&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u7edf\u8ba1\u4fe1\u606f\u7684\u7ed3\u6784\u4f53&#xff0c;\u540c\u6837&#xff0c;buf \u8981\u6307\u5411\u5408\u9002\u7684\u7ed3\u6784\u4f53\u6765\u63a5\u6536\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>SHM_STAT<\/td>\n<td>\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u72b6\u6001<\/td>\n<td>\u7c7b\u4f3c\u4e8e IPC_STAT&#xff0c;\u4f46\u4e0d\u662f\u901a\u8fc7 shmid \u6765\u6307\u5b9a\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u800c\u662f\u901a\u8fc7\u4e00\u4e2a\u7d22\u5f15\u503c&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u904d\u5386\u7cfb\u7edf\u4e2d\u7684\u5171\u4eab\u5185\u5b58\u6bb5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>buf&#xff1a;\u6307\u5411\u4e00\u4e2a\u4fdd\u5b58\u7740\u5171\u4eab\u5185\u5b58\u7684\u6a21\u5f0f\u72b6\u6001\u548c\u8bbf\u95ee\u6743\u9650\u7684\u7ed3\u6784\u4f53struct shmid_ds<\/p>\n<h2>\u4e09\u3001\u5f00\u59cb\u901a\u4fe1<\/h2>\n<p>\u7531\u4e8e\u5171\u4eab\u5185\u5b58\u901a\u4fe1\u6ca1\u529e\u6cd5\u505a\u5230\u540c\u6b65\u548c\u4e92\u65a5&#xff0c;\u6211\u4eec\u901a\u8fc7\u52a0\u5165\u547d\u540d\u7ba1\u9053\u7684\u65b9\u5f0f\u6765\u5f62\u6210\u540c\u6b65\u4e0e\u4e92\u65a5\u7684\u6548\u679c&#xff0c;\u666e\u901a\u7684\u5171\u4eab\u5185\u5b58\u901a\u4fe1\u5c31\u8c03\u7528\u5b8c\u63a5\u53e3\u76f4\u63a5\u5199\u76f4\u63a5\u8bfb\u5c31\u884c&#xff0c;\u6bd4\u8f83\u7b80\u5355&#xff0c;\u56e0\u4e3a\u5171\u4eab\u5185\u5b58\u662f\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u76f4\u63a5\u8bfb\u7684&#xff0c;\u6240\u4ee5\u6211\u4eec\u5347\u7ea7\u4e00\u4e0b<\/p>\n<h3>1\u3001comm.hpp<\/h3>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">ifndef<\/span> <span class=\"token expression\">__COMM_HPP__<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">__COMM_HPP__<\/span><\/span><\/p>\n<p><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;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;cstring&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 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;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;sys\/stat.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">FIFO_FILE<\/span> <span class=\"token string\">&#034;.\/myfifo&#034;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">MODE<\/span> <span class=\"token expression\"><span class=\"token number\">0664<\/span><\/span><\/span><\/p>\n<p><span class=\"token keyword\">enum<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n    FIFO_CREATE_ERR <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    FIFO_DELETE_ERR<span class=\"token punctuation\">,<\/span><br \/>\n    FIFO_OPEN_ERR<span class=\"token punctuation\">,<\/span><br \/>\n    FIFO_READ_ERR<br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">using<\/span> <span class=\"token keyword\">namespace<\/span> std<span class=\"token punctuation\">;<\/span><\/p>\n<p>Log log<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">int<\/span> size <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">4096<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> string pathname<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;\/home\/slm&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">int<\/span> proj_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x6667<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u5c01\u88c5\u4e00\u4e2a\u83b7\u53d6Key\u503c\u7684\u51fd\u6570<\/span><br \/>\nkey_t <span class=\"token function\">GetKey<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/\u4f7f\u7528ftok\u51fd\u6570\u83b7\u53d6key\u503c<\/span><br \/>\n    key_t k <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">ftok<\/span><span class=\"token punctuation\">(<\/span>pathname<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> proj_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>k <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        <span class=\"token function\">exit<\/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 punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8fd4\u56de\u751f\u6210\u7684key\u503c<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> k<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/\u5c01\u88c5\u4e00\u4e2a\u5efa\u7acb\u5171\u4eab\u5185\u5b58\u533a\u7684\u51fd\u6570<\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token function\">GetShareMemHelper<\/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\">\/\/\u8c03\u7528\u83b7\u53d6key\u503c&#xff0c;\u7136\u540e\u5728\u7269\u7406\u5185\u5b58\u4e0a\u7533\u8bf7\u5171\u4eab\u5185\u5b58<\/span><br \/>\n    key_t k <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">GetKey<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> shmid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">shmget<\/span><span class=\"token punctuation\">(<\/span>k<span class=\"token punctuation\">,<\/span> size<span class=\"token punctuation\">,<\/span> flag<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/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><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/\u8fd4\u56deshmid<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> shmid<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u5982\u679c\u6307\u5b9a\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u5df2\u7ecf\u5b58\u5728&#xff0c;\u51fd\u6570\u4f1a\u5931\u8d25<\/span><br \/>\n<span class=\"token keyword\">int<\/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\">return<\/span> <span class=\"token function\">GetShareMemHelper<\/span><span class=\"token punctuation\">(<\/span>IPC_CREAT <span class=\"token operator\">|<\/span> IPC_EXCL <span class=\"token operator\">|<\/span> <span class=\"token number\">0666<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/\u7528\u4e8e\u83b7\u53d6\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u5982\u679c\u6307\u5b9a\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u4e0d\u5b58\u5728&#xff0c;\u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff1b;\u5982\u679c\u5df2\u7ecf\u5b58\u5728&#xff0c;\u5219\u76f4\u63a5\u8fd4\u56de\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26<\/span><br \/>\n<span class=\"token keyword\">int<\/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 keyword\">return<\/span> <span class=\"token function\">GetShareMemHelper<\/span><span class=\"token punctuation\">(<\/span>IPC_CREAT<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Init<\/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\">Init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u521b\u5efa\u7ba1\u9053<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> n <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">mkfifo<\/span><span class=\"token punctuation\">(<\/span>FIFO_FILE<span class=\"token punctuation\">,<\/span> MODE<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\">&#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 punctuation\">{<\/span><br \/>\n            <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;mkfifo&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span>FIFO_CREATE_ERR<span class=\"token punctuation\">)<\/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 operator\">~<\/span><span class=\"token function\">Init<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ \u9500\u6bc1\u7ba1\u9053<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> m <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">unlink<\/span><span class=\"token punctuation\">(<\/span>FIFO_FILE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>m <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 punctuation\">{<\/span><br \/>\n            <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;unlink&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span>FIFO_DELETE_ERR<span class=\"token punctuation\">)<\/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 punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">endif<\/span><\/span><\/p>\n<h3>2\u3001processa.cpp<\/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;comm.hpp&#034;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">extern<\/span> Log log<span class=\"token punctuation\">;<\/span><\/p>\n<p><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<span class=\"token comment\">\/\/\u521b\u5efa\u7ba1\u9053<\/span><br \/>\n    Init init<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8c03\u7528 CreateShm \u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u5e76\u8fd4\u56de\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26shmid<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> shmid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">CreateShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8c03\u7528 shmat \u51fd\u6570\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4&#xff0c;shmid \u662f\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26<\/span><br \/>\n    <span class=\"token comment\">\/\/nullptr \u8868\u793a\u8ba9\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u9644\u52a0\u5730\u5740&#xff0c;0 \u8868\u793a\u9ed8\u8ba4\u9644\u52a0\u6807\u5fd7<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8fd4\u56de\u503c\u662f\u5171\u4eab\u5185\u5b58\u6bb5\u5728\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u4e2d\u7684\u8d77\u59cb\u5730\u5740&#xff0c;\u5c06\u5176\u5f3a\u5236\u8f6c\u6362\u4e3achar*\u7c7b\u578b\u5e76\u8d4b\u503c\u7ed9shmaddr<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>shmaddr <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><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><br \/>\n    <span class=\"token comment\">\/\/\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u547d\u540d\u7ba1\u9053\u6587\u4ef6FIFO_FILE&#xff0c;\u7531\u4e8e\u662f\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00&#xff0c;\u8be5\u64cd\u4f5c\u4f1a\u963b\u585e<\/span><br \/>\n    <span class=\"token comment\">\/\/\u76f4\u5230\u6709\u5176\u4ed6\u8fdb\u7a0b\u4ee5\u5199\u6a21\u5f0f\u6253\u5f00\u540c\u4e00\u4e2a\u547d\u540d\u7ba1\u9053<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> fd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">open<\/span><span class=\"token punctuation\">(<\/span>FIFO_FILE<span class=\"token punctuation\">,<\/span> O_RDONLY<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>fd <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        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span>FIFO_OPEN_ERR<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5b9a\u4e49shmds\u7528\u4e8e\u5b58\u50a8\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u72b6\u6001\u4fe1\u606f<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">shmid_ds<\/span> shmds<span class=\"token punctuation\">;<\/span><br \/>\n    <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 comment\">\/\/\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u53d8\u91cfc&#xff0c;\u8c03\u7528read\u51fd\u6570\u4ece\u547d\u540d\u7ba1\u9053\u6587\u4ef6\u63cf\u8ff0\u7b26fd\u4e2d\u8bfb\u53d61\u4e2a\u5b57\u8282\u7684\u6570\u636e\u5230c\u4e2d<\/span><br \/>\n<span class=\"token comment\">\/\/\u5e76\u5c06\u5b9e\u9645\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u5b58\u50a8\u5728s\u4e2d&#xff0c;\u5982\u679c\u80fd\u8bfb\u53d6\u5230\u8bf4\u660e\u901a\u4fe1\u8fdb\u7a0b\u7ed9\u6211\u4eec\u53d1\u4fe1\u53f7\u4e86&#xff0c;\u6709\u5185\u5bb9<\/span><br \/>\n<span class=\"token comment\">\/\/\u90a3\u6211\u4eec\u5c31\u8bfb\u53d6\u5171\u4eab\u5185\u5b58\u90e8\u5206<\/span><br \/>\n        <span class=\"token keyword\">char<\/span> c<span class=\"token punctuation\">;<\/span><br \/>\n        ssize_t s <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">read<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>c<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 keyword\">if<\/span><span class=\"token punctuation\">(<\/span>s <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>s <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            <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span>FIFO_READ_ERR<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u6307\u9488\u8bbf\u95ee\u5171\u4eab\u5185\u5b58<\/span><br \/>\n        cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;client say&#064; &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> shmaddr <span class=\"token operator\">&lt;&lt;<\/span> endl<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\">\/\/\u8c03\u7528shmctl\u51fd\u6570&#xff0c;\u4f7f\u7528IPC_STAT\u547d\u4ee4\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u72b6\u6001\u4fe1\u606f&#xff0c;\u5e76\u5c06\u5176\u5b58\u50a8\u5728shmds\u7ed3\u6784\u4f53\u4e2d<\/span><br \/>\n<span class=\"token comment\">\/\/\u7136\u540e\u5c06\u7ed3\u6784\u4f53shmds\u90e8\u5206\u4fe1\u606f\u6253\u5370\u51fa\u6765<\/span><br \/>\n        <span class=\"token function\">shmctl<\/span><span class=\"token punctuation\">(<\/span>shmid<span class=\"token punctuation\">,<\/span> IPC_STAT<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>shmds<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;shm size: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> shmds<span class=\"token punctuation\">.<\/span>shm_segsz <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n        cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;shm nattch: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> shmds<span class=\"token punctuation\">.<\/span>shm_nattch <span class=\"token operator\">&lt;&lt;<\/span> endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shm key: 0x%x\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span>  shmds<span class=\"token punctuation\">.<\/span>shm_perm<span class=\"token punctuation\">.<\/span>__key<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;shm mode: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> shmds<span class=\"token punctuation\">.<\/span>shm_perm<span class=\"token punctuation\">.<\/span>mode <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 comment\">\/\/\u53d6\u6d88\u94fe\u63a5<\/span><br \/>\n    <span class=\"token function\">shmdt<\/span><span class=\"token punctuation\">(<\/span>shmaddr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8c03\u7528shmctl\u51fd\u6570&#xff0c;\u4f7f\u7528IPC_RMID\u547d\u4ee4\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u6807\u8bb0\u4e3a\u5f85\u5220\u9664\u72b6\u6001<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5f53\u6240\u6709\u9644\u52a0\u8fdb\u7a0b\u90fd\u5206\u79bb\u540e&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5220\u9664\u8be5\u5171\u4eab\u5185\u5b58\u6bb5<\/span><br \/>\n    <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 function\">close<\/span><span class=\"token punctuation\">(<\/span>fd<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>3\u3001processb.cpp<\/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;comm.hpp&#034;<\/span><\/span><\/p>\n<p><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<span class=\"token comment\">\/\/\u8c03\u7528GetShm\u51fd\u6570\u83b7\u53d6\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26shmid<\/span><br \/>\n<span class=\"token comment\">\/\/\u5728\u53c2\u6570\u76f8\u540c\u3001\u65e0\u7279\u6b8a\u60c5\u51b5\u7684\u60c5\u51b5\u4e0b&#xff0c;shmid\u4e5f\u76f8\u540c<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> shmid <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">GetShm<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>shmaddr <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><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><\/p>\n<p>    <span class=\"token keyword\">int<\/span> fd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">open<\/span><span class=\"token punctuation\">(<\/span>FIFO_FILE<span class=\"token punctuation\">,<\/span> O_WRONLY<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>fd <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        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span>FIFO_OPEN_ERR<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4e00\u65e6\u6709\u4e86\u5171\u4eab\u5185\u5b58&#xff0c;\u6302\u63a5\u5230\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u4e2d&#xff0c;\u76f4\u63a5\u628a\u4ed6\u5f53\u6210\u4f60\u7684\u5185\u5b58\u7a7a\u95f4\u6765\u7528\u5373\u53ef<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4e0d\u9700\u8981\u8c03\u7528\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3<\/span><br \/>\n    <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        cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Please Enter&#064; &#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">fgets<\/span><span class=\"token punctuation\">(<\/span>shmaddr<span class=\"token punctuation\">,<\/span> <span class=\"token number\">4096<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token constant\">stdin<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u5728\u7ba1\u9053\u5199\u5165\u4e00\u4e2a\u5b57\u7b26\u6765\u901a\u77e5\u670d\u52a1\u5668\u5171\u4eab\u5185\u5b58\u6709\u65b0\u6570\u636e<\/span><br \/>\n        <span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;c&#034;<\/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 punctuation\">}<\/span><br \/>\n<span class=\"token comment\">\/\/\u8c03\u7528shmdt\u51fd\u6570\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u4ece\u5f53\u524d\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u5206\u79bb&#xff0c;\u91ca\u653e\u76f8\u5173\u7684\u8d44\u6e90<\/span><br \/>\n    <span class=\"token function\">shmdt<\/span><span class=\"token punctuation\">(<\/span>shmaddr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>fd<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<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152756-68026f7c4b979.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\u56db\u3001\u5176\u4ed6\u95ee\u9898<\/h2>\n<p>key\u662f\u5728\u64cd\u4f5c\u7cfb\u7edf\u5185\u6807\u5b9a\u5171\u4eab\u5185\u5b58\u552f\u4e00\u6027\u7684&#xff0c;\u800cshmid\u662f\u5728\u8fdb\u7a0b\u5185\u6807\u5b9a\u8d44\u6e90\u552f\u4e00\u6027\u7684&#xff0c;\u4e8c\u8005\u867d\u7136\u90fd\u662f\u6807\u5b9a\u552f\u4e00\u6027&#xff0c;\u4f46\u662f\u4f7f\u7528\u8303\u56f4\u4e0d\u540c&#xff0c;\u5e76\u4e14\u867d\u7136\u5171\u4eab\u5185\u5b58\u5c5e\u4e8e\u6587\u4ef6\u7cfb\u7edf&#xff0c;\u4f46\u662fshmid\u548c\u6587\u4ef6\u63cf\u8ff0\u7b26\u517c\u5bb9\u6027\u4e0d\u597d&#xff0c;\u5171\u4eab\u5185\u5b58\u8fd9\u65b9\u9762\u5355\u72ec\u641e\u4e86\u4e00\u5957\u7c7b\u4f3c\u4e8e\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u7684\u89c4\u5219<\/p>\n<p>\u5171\u4eab\u5185\u5b58\u7684\u751f\u547d\u5468\u671f\u968f\u5185\u6838&#xff0c;\u9664\u975e\u7528\u6237\u91ca\u653e\u6216\u8005\u5185\u6838\u91cd\u542f&#xff0c;\u5171\u4eab\u5185\u5b58\u624d\u4f1a\u91ca\u653e<\/p>\n<p>ipcs -m\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u5171\u4eab\u5185\u5b58&#xff0c;\u5176\u4e2dperms\u662f\u6743\u9650\u4f4d&#xff0c;nattch\u662f\u548c\u5f53\u524d\u5171\u4eab\u5185\u5b58\u5173\u8054\u7684\u8fdb\u7a0b\u4e2a\u6570 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152757-68026f7d08a96.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u5171\u4eab\u5185\u5b58\u662f\u6ca1\u6709\u5b58\u5728\u540c\u6b65\u4e92\u65a5\u8fd9\u6837\u7684\u4fdd\u62a4\u673a\u5236\u7684&#xff0c;\u5b83\u662f\u6240\u6709\u8fdb\u7a0b\u901a\u4fe1\u65b9\u5f0f\u4e2d\u6700\u5feb\u7684&#xff0c;\u56e0\u4e3a\u76f8\u8f83\u5176\u4ed6\u65b9\u5f0f&#xff0c;\u5b83\u7684\u62f7\u8d1d\u66f4\u5c11<\/p>\n<p>\u5171\u4eab\u5185\u5b58\u5185\u90e8\u7684\u6570\u636e\u7531\u7528\u6237\u81ea\u5df1\u7ef4\u62a4<\/p>\n<hr \/>\n<p>\u4eca\u65e5\u5206\u4eab\u5c31\u5230\u8fd9\u91cc\u4e86~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152757-68026f7d1d786.jpg\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2k\u6b21\uff0c\u70b9\u8d5e108\u6b21\uff0c\u6536\u85cf58\u6b21\u3002\u5171\u4eab\u5185\u5b58\u7684\u539f\u7406\u3001\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3\u548c\u914d\u5408\u547d\u540d\u7ba1\u9053\u7684\u540c\u6b65\u4e92\u65a5\u6027\u5b9e\u73b0<\/p>\n","protected":false},"author":2,"featured_media":17015,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,371,123,43,305,124,44],"topic":[],"class_list":["post-17025","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-371","tag-123","tag-43","tag-305","tag-124","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u3011systemV\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\/17025.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u3011systemV\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2k\u6b21\uff0c\u70b9\u8d5e108\u6b21\uff0c\u6536\u85cf58\u6b21\u3002\u5171\u4eab\u5185\u5b58\u7684\u539f\u7406\u3001\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3\u548c\u914d\u5408\u547d\u540d\u7ba1\u9053\u7684\u540c\u6b65\u4e92\u65a5\u6027\u5b9e\u73b0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/17025.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T15:27:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152755-68026f7bc527b.jpg\" \/>\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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/17025.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/17025.html\",\"name\":\"\u3010Linux\u3011systemV\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T15:27:58+00:00\",\"dateModified\":\"2025-04-18T15:27:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/17025.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/17025.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/17025.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u3011systemV\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\u3011systemV\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\/17025.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u3011systemV\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb2k\u6b21\uff0c\u70b9\u8d5e108\u6b21\uff0c\u6536\u85cf58\u6b21\u3002\u5171\u4eab\u5185\u5b58\u7684\u539f\u7406\u3001\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3\u548c\u914d\u5408\u547d\u540d\u7ba1\u9053\u7684\u540c\u6b65\u4e92\u65a5\u6027\u5b9e\u73b0","og_url":"https:\/\/www.wsisp.com\/helps\/17025.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T15:27:58+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418152755-68026f7bc527b.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/17025.html","url":"https:\/\/www.wsisp.com\/helps\/17025.html","name":"\u3010Linux\u3011systemV\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T15:27:58+00:00","dateModified":"2025-04-18T15:27:58+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/17025.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/17025.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/17025.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u3011systemV\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\/17025","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=17025"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/17025\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/17015"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=17025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=17025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=17025"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=17025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}