{"id":43288,"date":"2025-06-11T16:50:54","date_gmt":"2025-06-11T08:50:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/43288.html"},"modified":"2025-06-11T16:50:54","modified_gmt":"2025-06-11T08:50:54","slug":"%e3%80%90c%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8webserver%e3%80%91-7%ef%bc%9a%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/43288.html","title":{"rendered":"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\u5171\u4eab\u5185\u5b58"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250611085052-6849436c2da08.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <\/p>\n<h4>\u672c\u6587\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4e00\u3001\u5171\u4eab\u5185\u5b58<\/li>\n<li>\n<ul>\n<li>1.1 shmget\u51fd\u6570<\/li>\n<li>1.2 shmat<\/li>\n<li>1.3 shmdt<\/li>\n<li>1.4 shmctl<\/li>\n<li>1.5 ftok<\/li>\n<li>1.6 \u5171\u4eab\u5185\u5b58\u548c\u5185\u5b58\u6620\u5c04\u7684\u5173\u8054<\/li>\n<li>1.7 \u5c0fdemo<\/li>\n<\/ul>\n<\/li>\n<li>\u4e8c\u3001\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u547d\u4ee4<\/li>\n<\/ul>\n<h2>\u4e00\u3001\u5171\u4eab\u5185\u5b58<\/h2>\n<p>\u5171\u4eab\u5185\u5b58\u5141\u8bb8\u4e24\u4e2a\u6216\u8005\u591a\u4e2a\u8fdb\u7a0b\u5171\u4eab\u7269\u7406\u5185\u5b58\u7684\u540c\u4e00\u5757\u533a\u57df&#xff08;\u901a\u5e38\u88ab\u79f0\u4e3a\u6bb5&#xff09;\u3002\u7531\u4e8e\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5\u4f1a\u79f0\u4e3a\u4e00\u4e2a\u8fdb\u7a0b\u7528\u6237\u7a7a\u95f4\u7684\u4e00\u90e8\u5206&#xff0c;\u56e0\u6b64\u8fd9\u79cd IPC \u673a\u5236\u65e0\u9700\u5185\u6838\u4ecb\u5165\u3002\u6240\u6709\u9700\u8981\u505a\u7684\u5c31\u662f\u8ba9\u4e00\u4e2a\u8fdb\u7a0b\u5c06\u6570\u636e\u590d\u5236\u8fdb\u5171\u4eab\u5185\u5b58\u4e2d&#xff0c;\u5e76\u4e14\u8fd9\u90e8\u5206\u6570\u636e\u4f1a\u5bf9\u5176\u4ed6\u6240\u6709\u5171\u4eab\u540c\u4e00\u4e2a\u6bb5\u7684\u8fdb\u7a0b\u53ef\u7528\u3002<\/p>\n<p>\u5171\u4eab\u5185\u5b58\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\u533a\u57df\u3002\u8fd9\u79cd\u5171\u4eab\u5185\u5b58\u533a\u57df\u901a\u5e38\u88ab\u79f0\u4e3a\u5171\u4eab\u5185\u5b58\u6bb5\u3002\u5171\u4eab\u5185\u5b58\u6bb5\u53ef\u4ee5\u88ab\u6620\u5c04\u5230\u591a\u4e2a\u8fdb\u7a0b\u7684\u7528\u6237\u7a7a\u95f4\u4e2d&#xff0c;\u4ece\u800c\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u7684\u9ad8\u6548\u901a\u4fe1\u3002\u865a\u62df\u5185\u5b58\u6620\u5c04&#xff1a;\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u4f1a\u6709\u4e00\u90e8\u5206\u88ab\u6620\u5c04\u5230\u8fd9\u5757\u5171\u4eab\u7684\u7269\u7406\u5185\u5b58\u4e0a\u3002\u5f53\u8fdb\u7a0b\u8bbf\u95ee\u8fd9\u5757\u6620\u5c04\u7684\u865a\u62df\u5185\u5b58\u533a\u57df\u65f6&#xff0c;\u5b9e\u9645\u4e0a\u662f\u5728\u8bbf\u95ee\u5171\u4eab\u7684\u7269\u7406\u5185\u5b58\u3002\u7531\u4e8e\u8fd9\u5757\u5185\u5b58\u88ab\u6620\u5c04\u5230\u8fdb\u7a0b\u7684\u7528\u6237\u7a7a\u95f4\u4e2d&#xff0c;\u8fdb\u7a0b\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5b83&#xff0c;\u800c\u4e0d\u9700\u8981\u5185\u6838\u4ecb\u5165\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250611085052-6849436c8022e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u4e0e\u7ba1\u9053\u7b49\u8981\u6c42\u53d1\u9001\u8fdb\u7a0b\u5c06\u6570\u636e\u4ece\u7528\u6237\u7a7a\u95f4\u7684\u7f13\u51b2\u533a\u590d\u5236\u8fdb\u5185\u6838\u5185\u5b58\u548c\u63a5\u6536\u8fdb\u7a0b\u5c06\u6570\u636e\u4ece\u5185\u6838\u5185\u5b58\u590d\u5236\u8fdb\u7528\u6237\u7a7a\u95f4\u7684\u7f13\u51b2\u533a\u7684\u505a\u6cd5\u76f8\u6bd4&#xff0c;\u8fd9\u79cd IPC \u6280\u672f\u7684\u901f\u5ea6\u66f4\u5feb\u3002\u5185\u5b58\u6620\u5c04\u76f8\u6bd4\u5171\u4eab\u5185\u5b58&#xff0c;\u6548\u7387\u4f1a\u6bd4\u8f83\u4f4e\u3002\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u6548\u7387\u6bd4\u8f83\u9ad8\u3002<\/p>\n<ul>\n<li>\u521b\u5efa\u6216\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5<\/li>\n<\/ul>\n<p>\u8c03\u7528shmget()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u6216\u8005\u83b7\u53d6\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26&#xff08;\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u53ef\u80fd\u7531\u5176\u4ed6\u8fdb\u7a0b\u521b\u5efa&#xff09;\u3002\u8fd9\u4e2a\u8c03\u7528\u5c06\u8fd4\u56de\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6807\u8bc6\u7b26&#xff0c;\u8be5\u6807\u8bc6\u7b26\u5c06\u5728\u540e\u7eed\u7684\u8c03\u7528\u4e2d\u88ab\u4f7f\u7528\u3002<\/p>\n<ul>\n<li>\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58<\/li>\n<\/ul>\n<p>\u4f7f\u7528shmat()\u51fd\u6570\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u8c03\u7528\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58\u4e2d&#xff0c;\u4f7f\u5176\u6210\u4e3a\u8fdb\u7a0b\u865a\u62df\u5185\u5b58\u7684\u4e00\u90e8\u5206\u3002\u4ece\u8fd9\u4e00\u523b\u8d77&#xff0c;\u7a0b\u5e8f\u53ef\u4ee5\u50cf\u5bf9\u5f85\u5176\u4ed6\u666e\u901a\u5185\u5b58\u4e00\u6837\u4f7f\u7528\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u3002\u4e3a\u4e86\u5f15\u7528\u8fd9\u5757\u5171\u4eab\u5185\u5b58&#xff0c;\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528shmat()\u8c03\u7528\u8fd4\u56de\u7684addr\u503c&#xff0c;\u8fd9\u662f\u4e00\u4e2a\u6307\u5411\u8fdb\u7a0b\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u5171\u4eab\u5185\u5b58\u6bb5\u8d77\u70b9\u7684\u6307\u9488\u3002<\/p>\n<ul>\n<li>\u5206\u79bb\u5171\u4eab\u5185\u5b58\u6bb5&#xff08;\u53ef\u9009&#xff09;<\/li>\n<\/ul>\n<p>\u8c03\u7528shmdt()\u51fd\u6570\u6765\u5206\u79bb\u5171\u4eab\u5185\u5b58\u6bb5\u3002\u5206\u79bb\u540e&#xff0c;\u8fdb\u7a0b\u5c06\u65e0\u6cd5\u518d\u5f15\u7528\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u3002\u8fd9\u4e00\u6b65\u662f\u53ef\u9009\u7684&#xff0c;\u56e0\u4e3a\u5728\u8fdb\u7a0b\u7ec8\u6b62\u65f6&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5b8c\u6210\u8fd9\u4e00\u6b65\u3002<\/p>\n<ul>\n<li>\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5<\/li>\n<\/ul>\n<p>\u8c03\u7528shmctl()\u51fd\u6570\u6765\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5\u3002\u53ea\u6709\u5728\u6240\u6709\u9644\u52a0\u5230\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u8fdb\u7a0b\u90fd\u4e0e\u4e4b\u5206\u79bb\u4e4b\u540e&#xff0c;\u5171\u4eab\u5185\u5b58\u6bb5\u624d\u4f1a\u88ab\u9500\u6bc1\u3002\u8fd9\u4e00\u6b65\u901a\u5e38\u53ea\u9700\u8981\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c\u5373\u53ef\u3002<\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">shmget<\/span><span class=\"token punctuation\">(<\/span>key_t key<span class=\"token punctuation\">,<\/span> size_t size<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> shmflg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/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><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><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><br \/>\nkey_t <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<h3>1.1 shmget\u51fd\u6570<\/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\">&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><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">shmget<\/span><span class=\"token punctuation\">(<\/span>key_t key<span class=\"token punctuation\">,<\/span> size_t size<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> shmflg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u529f\u80fd&#xff1a;\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u6216\u8005\u83b7\u53d6\u4e00\u4e2a\u65e2\u6709\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u3002<br \/>\n        \u65b0\u521b\u5efa\u7684\u5185\u5b58\u6bb5\u4e2d\u7684\u6570\u636e\u90fd\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a<span class=\"token number\">0<\/span><br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u53c2\u6570&#xff1a;<br \/>\n        <span class=\"token operator\">&#8211;<\/span> key <span class=\"token operator\">:<\/span> key_t\u7c7b\u578b\u662f\u4e00\u4e2a\u6574\u5f62&#xff0c;\u901a\u8fc7\u8fd9\u4e2a\u627e\u5230\u6216\u8005\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u3002<br \/>\n                \u4e00\u822c\u4f7f\u7528<span class=\"token number\">16<\/span>\u8fdb\u5236\u8868\u793a&#xff0c;\u975e<span class=\"token number\">0<\/span>\u503c<br \/>\n        <span class=\"token operator\">&#8211;<\/span> size<span class=\"token operator\">:<\/span> \u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f<br \/>\n        <span class=\"token operator\">&#8211;<\/span> shmflg<span class=\"token operator\">:<\/span> \u5c5e\u6027<br \/>\n            <span class=\"token operator\">&#8211;<\/span> \u8bbf\u95ee\u6743\u9650<br \/>\n            <span class=\"token operator\">&#8211;<\/span> \u9644\u52a0\u5c5e\u6027&#xff1a;\u521b\u5efa<span class=\"token operator\">\/<\/span>\u5224\u65ad\u5171\u4eab\u5185\u5b58\u662f\u4e0d\u662f\u5b58\u5728<br \/>\n                <span class=\"token operator\">&#8211;<\/span> \u521b\u5efa&#xff1a;IPC_CREAT<br \/>\n                <span class=\"token operator\">&#8211;<\/span> \u5224\u65ad\u5171\u4eab\u5185\u5b58\u662f\u5426\u5b58\u5728&#xff1a; IPC_EXCL <span class=\"token punctuation\">,<\/span> \u9700\u8981\u548cIPC_CREAT\u4e00\u8d77\u4f7f\u7528<br \/>\n                    IPC_CREAT <span class=\"token operator\">|<\/span> IPC_EXCL <span class=\"token operator\">|<\/span> <span class=\"token number\">0664<\/span><br \/>\n        <span class=\"token operator\">&#8211;<\/span> \u8fd4\u56de\u503c&#xff1a;<br \/>\n            \u5931\u8d25&#xff1a;<span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span> \u5e76\u8bbe\u7f6e\u9519\u8bef\u53f7<br \/>\n            \u6210\u529f&#xff1a;<span class=\"token operator\">&gt;<\/span><span class=\"token number\">0<\/span> \u8fd4\u56de\u5171\u4eab\u5185\u5b58\u7684\u5f15\u7528\u7684ID&#xff0c;\u540e\u9762\u64cd\u4f5c\u5171\u4eab\u5185\u5b58\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u503c\u3002<\/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;stdio.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;stdlib.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 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><\/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> <span class=\"token punctuation\">{<\/span><br \/>\n    key_t key <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x1234<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5b9a\u4e49\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u7684\u952e\u503c&#xff0c;\u901a\u5e38\u4f7f\u752816\u8fdb\u5236\u8868\u793a<\/span><br \/>\n    size_t size <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1024<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5b9a\u4e49\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f&#xff08;\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d&#xff09;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> shmflg <span class=\"token operator\">&#061;<\/span> IPC_CREAT <span class=\"token operator\">|<\/span> <span class=\"token number\">0664<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u5171\u4eab\u5185\u5b58\u7684\u6807\u5fd7\u548c\u8bbf\u95ee\u6743\u9650<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8c03\u7528 shmget \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u6216\u83b7\u53d6\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6807\u8bc6\u7b26<\/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>key<span class=\"token punctuation\">,<\/span> size<span class=\"token punctuation\">,<\/span> shmflg<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <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> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shmget failed&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5982\u679c\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u4fe1\u606f<\/span><br \/>\n        <span class=\"token function\">exit<\/span><span class=\"token punctuation\">(<\/span>EXIT_FAILURE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Shared memory created\/obtained successfully. ID: %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> shmid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <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>1.2 shmat<\/h3>\n<p><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><br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u529f\u80fd&#xff1a;\u548c\u5f53\u524d\u7684\u8fdb\u7a0b\u8fdb\u884c\u5173\u8054<br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u53c2\u6570&#xff1a;<br \/>\n        <span class=\"token operator\">&#8211;<\/span> shmid <span class=\"token operator\">:<\/span> \u5171\u4eab\u5185\u5b58\u7684\u6807\u8bc6&#xff08;ID&#xff09;<span class=\"token punctuation\">,<\/span>\u7531shmget\u8fd4\u56de\u503c\u83b7\u53d6<br \/>\n        <span class=\"token operator\">&#8211;<\/span> shmaddr<span class=\"token operator\">:<\/span> \u7533\u8bf7\u7684\u5171\u4eab\u5185\u5b58\u7684\u8d77\u59cb\u5730\u5740&#xff0c;\u6307\u5b9a<span class=\"token constant\">NULL<\/span>&#xff0c;\u5185\u6838\u6307\u5b9a<br \/>\n        <span class=\"token operator\">&#8211;<\/span> shmflg <span class=\"token operator\">:<\/span> \u5bf9\u5171\u4eab\u5185\u5b58\u7684\u64cd\u4f5c<br \/>\n            <span class=\"token operator\">&#8211;<\/span> \u8bfb &#xff1a; SHM_RDONLY<span class=\"token punctuation\">,<\/span> \u5fc5\u987b\u8981\u6709\u8bfb\u6743\u9650<br \/>\n            <span class=\"token operator\">&#8211;<\/span> \u8bfb\u5199&#xff1a; <span class=\"token number\">0<\/span><br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u8fd4\u56de\u503c&#xff1a;<br \/>\n        \u6210\u529f&#xff1a;\u8fd4\u56de\u5171\u4eab\u5185\u5b58\u7684\u9996&#xff08;\u8d77\u59cb&#xff09;\u5730\u5740\u3002  \u5931\u8d25<span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8c03\u7528 shmat \u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58\u4e2d<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u53c2\u6570&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8211; shmid: \u5171\u4eab\u5185\u5b58\u7684\u6807\u8bc6\u7b26<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8211; shmaddr: \u6307\u5b9a\u4e3aNULL&#xff0c;\u8ba9\u5185\u6838\u9009\u62e9\u5408\u9002\u7684\u5730\u5740<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#8211; shmflg: 0 \u8868\u793a\u8bfb\u5199\u6743\u9650<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span>shmaddr <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 constant\">NULL<\/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 keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>shmaddr <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/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><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shmat failed&#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>EXIT_FAILURE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Shared memory attached at address: %p\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> shmaddr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>1.3 shmdt<\/h3>\n<p><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><br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u529f\u80fd&#xff1a;\u89e3\u9664\u5f53\u524d\u8fdb\u7a0b\u548c\u5171\u4eab\u5185\u5b58\u7684\u5173\u8054<br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u53c2\u6570&#xff1a;<br \/>\n        shmaddr&#xff1a;\u5171\u4eab\u5185\u5b58\u7684\u9996\u5730\u5740<br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u8fd4\u56de\u503c&#xff1a;\u6210\u529f <span class=\"token number\">0<\/span>&#xff0c; \u5931\u8d25 <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">shmdt<\/span><span class=\"token punctuation\">(<\/span>shmaddr<span class=\"token punctuation\">)<\/span> <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 punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shmdt failed&#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>EXIT_FAILURE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Shared memory detached successfully.\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>1.4 shmctl<\/h3>\n<p><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><br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u529f\u80fd&#xff1a;\u5bf9\u5171\u4eab\u5185\u5b58\u8fdb\u884c\u64cd\u4f5c\u3002\u5220\u9664\u5171\u4eab\u5185\u5b58&#xff0c;\u5171\u4eab\u5185\u5b58\u8981\u5220\u9664\u624d\u4f1a\u6d88\u5931&#xff0c;\u521b\u5efa\u5171\u4eab\u5185\u5b58\u7684\u8fdb\u884c\u88ab\u9500\u6bc1\u4e86\u5bf9\u5171\u4eab\u5185\u5b58\u662f\u6ca1\u6709\u4efb\u4f55\u5f71\u54cd\u3002<br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u53c2\u6570&#xff1a;<br \/>\n        <span class=\"token operator\">&#8211;<\/span> shmid<span class=\"token operator\">:<\/span> \u5171\u4eab\u5185\u5b58\u7684ID<br \/>\n        <span class=\"token operator\">&#8211;<\/span> cmd <span class=\"token operator\">:<\/span> \u8981\u505a\u7684\u64cd\u4f5c<br \/>\n            <span class=\"token operator\">&#8211;<\/span> IPC_STAT <span class=\"token operator\">:<\/span> \u83b7\u53d6\u5171\u4eab\u5185\u5b58\u7684\u5f53\u524d\u7684\u72b6\u6001<br \/>\n            <span class=\"token operator\">&#8211;<\/span> IPC_SET <span class=\"token operator\">:<\/span> \u8bbe\u7f6e\u5171\u4eab\u5185\u5b58\u7684\u72b6\u6001<br \/>\n            <span class=\"token operator\">&#8211;<\/span> IPC_RMID<span class=\"token operator\">:<\/span> \u6807\u8bb0\u5171\u4eab\u5185\u5b58\u88ab\u9500\u6bc1<br \/>\n        <span class=\"token operator\">&#8211;<\/span> buf&#xff1a;\u9700\u8981\u8bbe\u7f6e\u6216\u8005\u83b7\u53d6\u7684\u5171\u4eab\u5185\u5b58\u7684\u5c5e\u6027\u4fe1\u606f<br \/>\n            <span class=\"token operator\">&#8211;<\/span> IPC_STAT <span class=\"token operator\">:<\/span> buf\u5b58\u50a8\u6570\u636e<br \/>\n            <span class=\"token operator\">&#8211;<\/span> IPC_SET <span class=\"token operator\">:<\/span> buf\u4e2d\u9700\u8981\u521d\u59cb\u5316\u6570\u636e&#xff0c;\u8bbe\u7f6e\u5230\u5185\u6838\u4e2d<br \/>\n            <span class=\"token operator\">&#8211;<\/span> IPC_RMID <span class=\"token operator\">:<\/span> \u6ca1\u6709\u7528&#xff0c;<span class=\"token constant\">NULL<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5&#xff08;\u53ef\u9009&#xff0c;\u901a\u5e38\u7531\u6700\u540e\u4e00\u4e2a\u4f7f\u7528\u5b83\u7684\u8fdb\u7a0b\u5b8c\u6210&#xff09;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/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 number\">0<\/span><span class=\"token punctuation\">)<\/span> <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 punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shmctl failed&#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>EXIT_FAILURE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Shared memory segment deleted.\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>1.5 ftok<\/h3>\n<p>key_t <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><br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u529f\u80fd&#xff1a;\u6839\u636e\u6307\u5b9a\u7684\u8def\u5f84\u540d&#xff0c;\u548c<span class=\"token keyword\">int<\/span>\u503c&#xff0c;\u751f\u6210\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u7684key<br \/>\n    <span class=\"token operator\">&#8211;<\/span> \u53c2\u6570&#xff1a;<br \/>\n        <span class=\"token operator\">&#8211;<\/span> pathname<span class=\"token operator\">:<\/span>\u6307\u5b9a\u4e00\u4e2a\u5b58\u5728\u7684\u8def\u5f84<br \/>\n            <span class=\"token operator\">\/<\/span>home<span class=\"token operator\">\/<\/span>nowcoder<span class=\"token operator\">\/<\/span>Linux<span class=\"token operator\">\/<\/span>a<span class=\"token punctuation\">.<\/span>txt<br \/>\n            <span class=\"token operator\">\/<\/span><br \/>\n        <span class=\"token operator\">&#8211;<\/span> proj_id<span class=\"token operator\">:<\/span> <span class=\"token keyword\">int<\/span>\u7c7b\u578b\u7684\u503c&#xff0c;\u4f46\u662f\u8fd9\u7cfb\u7edf\u8c03\u7528\u53ea\u4f1a\u4f7f\u7528\u5176\u4e2d\u7684<span class=\"token number\">1<\/span>\u4e2a\u5b57\u8282<br \/>\n                   \u8303\u56f4 &#xff1a; <span class=\"token number\">0<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">255<\/span>  \u4e00\u822c\u6307\u5b9a\u4e00\u4e2a\u5b57\u7b26 <span class=\"token char\">&#039;a&#039;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5b9a\u4e49\u4e00\u4e2a\u5b58\u5728\u7684\u8def\u5f84\u540d<\/span><br \/>\n    <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span>pathname <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;\/home\/nowcoder\/Linux\/a.txt&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5b9a\u4e49\u4e00\u4e2a\u9879\u76eeID&#xff0c;\u901a\u5e38\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u7684ASCII\u503c<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> proj_id <span class=\"token operator\">&#061;<\/span> <span class=\"token char\">&#039;a&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8c03\u7528 ftok \u751f\u6210\u5171\u4eab\u5185\u5b58\u7684\u952e\u503c<\/span><br \/>\n    key_t key <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">ftok<\/span><span class=\"token punctuation\">(<\/span>pathname<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>key <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 punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;ftok failed&#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>EXIT_FAILURE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Generated key: %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u4f7f\u7528\u751f\u6210\u7684\u952e\u503c\u521b\u5efa\u6216\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5<\/span><br \/>\n    size_t size <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1024<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u5b9a\u4e49\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> shmflg <span class=\"token operator\">&#061;<\/span> IPC_CREAT <span class=\"token operator\">|<\/span> <span class=\"token number\">0664<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u5171\u4eab\u5185\u5b58\u7684\u6807\u5fd7\u548c\u8bbf\u95ee\u6743\u9650<\/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>key<span class=\"token punctuation\">,<\/span> size<span class=\"token punctuation\">,<\/span> shmflg<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\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shmget failed&#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>EXIT_FAILURE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Shared memory created\/obtained successfully. ID: %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> shmid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<ul>\n<li>\u95ee\u98981&#xff1a;\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u77e5\u9053\u4e00\u5757\u5171\u4eab\u5185\u5b58\u88ab\u591a\u5c11\u4e2a\u8fdb\u7a0b\u5173\u8054&#xff1f;<\/li>\n<\/ul>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u7ed3\u6784\u4f53struct shmid_ds\u6765\u8ddf\u8e2a\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u72b6\u6001\u3002\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4e2d\u6709\u4e00\u4e2a\u6210\u5458shm_nattch&#xff0c;\u5b83\u8bb0\u5f55\u4e86\u5f53\u524d\u4e0e\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u5173\u8054\u7684\u8fdb\u7a0b\u4e2a\u6570\u3002\u6bcf\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u901a\u8fc7shmat\u51fd\u6570\u9644\u52a0\u5230\u5171\u4eab\u5185\u5b58\u6bb5\u65f6&#xff0c;shm_nattch\u7684\u503c\u4f1a\u589e\u52a0&#xff1b;\u800c\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u901a\u8fc7shmdt\u51fd\u6570\u5206\u79bb\u5171\u4eab\u5185\u5b58\u6bb5\u65f6&#xff0c;shm_nattch\u7684\u503c\u4f1a\u51cf\u5c11\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u5b9e\u65f6\u4e86\u89e3\u6bcf\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<ul>\n<li>\u95ee\u98982&#xff1a;\u53ef\u4e0d\u53ef\u4ee5\u5bf9\u5171\u4eab\u5185\u5b58\u8fdb\u884c\u591a\u6b21\u5220\u9664shmctl&#xff1f;<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u5bf9\u5171\u4eab\u5185\u5b58\u8fdb\u884c\u591a\u6b21\u8c03\u7528shmctl\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u3002\u8fd9\u662f\u56e0\u4e3ashmctl\u6807\u8bb0\u5171\u4eab\u5185\u5b58\u4e3a\u5220\u9664\u72b6\u6001&#xff0c;\u5e76\u4e0d\u662f\u7acb\u5373\u5220\u9664\u5b83\u3002\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5b9e\u9645\u5220\u9664\u53d1\u751f\u5728\u4e0e\u4e4b\u5173\u8054\u7684\u8fdb\u7a0b\u6570\u4e3a0\u65f6\u3002\u5f53shm_nattch\u7684\u503c\u964d\u4e3a0&#xff0c;\u8868\u793a\u6ca1\u6709\u8fdb\u7a0b\u518d\u4f7f\u7528\u8be5\u5171\u4eab\u5185\u5b58\u6bb5&#xff0c;\u6b64\u65f6\u64cd\u4f5c\u7cfb\u7edf\u624d\u4f1a\u771f\u6b63\u5220\u9664\u5b83\u3002\u6b64\u5916&#xff0c;\u5f53\u5171\u4eab\u5185\u5b58\u7684key\u503c\u88ab\u8bbe\u7f6e\u4e3a0\u65f6&#xff0c;\u8868\u793a\u8be5\u5171\u4eab\u5185\u5b58\u6bb5\u5df2\u88ab\u6807\u8bb0\u4e3a\u5220\u9664\u3002\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u4e0e\u5171\u4eab\u5185\u5b58\u53d6\u6d88\u5173\u8054&#xff0c;\u90a3\u4e48\u8be5\u8fdb\u7a0b\u5c06\u65e0\u6cd5\u7ee7\u7eed\u64cd\u4f5c\u8be5\u5171\u4eab\u5185\u5b58&#xff0c;\u4e5f\u4e0d\u80fd\u518d\u6b21\u8fdb\u884c\u5173\u8054\u3002<\/p>\n<h3>1.6 \u5171\u4eab\u5185\u5b58\u548c\u5185\u5b58\u6620\u5c04\u7684\u5173\u8054<\/h3>\n<p>\u5171\u4eab\u5185\u5b58\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528&#xff08;\u5982shmget&#xff09;\u521b\u5efa&#xff0c;\u800c\u5185\u5b58\u6620\u5c04\u901a\u5e38\u9700\u8981\u4e00\u4e2a\u78c1\u76d8\u6587\u4ef6\u4f5c\u4e3a\u540e\u7aef\u652f\u6301&#xff08;\u533f\u540d\u6620\u5c04\u9664\u5916&#xff09;\u3002\u5171\u4eab\u5185\u5b58\u7684\u521b\u5efa\u66f4\u4e3a\u76f4\u63a5\u548c\u7b80\u5355&#xff0c;\u800c\u5185\u5b58\u6620\u5c04\u5219\u9700\u8981\u989d\u5916\u7684\u6587\u4ef6\u652f\u6301\u3002<\/p>\n<p>\u5171\u4eab\u5185\u5b58\u901a\u5e38\u5177\u6709\u66f4\u9ad8\u7684\u6548\u7387\u3002\u7531\u4e8e\u5171\u4eab\u5185\u5b58\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u76f4\u63a5\u8bbf\u95ee\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58&#xff0c;\u56e0\u6b64\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u65f6&#xff0c;\u6570\u636e\u4f20\u8f93\u7684\u5f00\u9500\u8f83\u5c0f\u3002\u76f8\u6bd4\u4e4b\u4e0b&#xff0c;\u5185\u5b58\u6620\u5c04\u867d\u7136\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u5185\u5b58\u8bbf\u95ee&#xff0c;\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u6587\u4ef6\u64cd\u4f5c&#xff0c;\u8fd9\u4f1a\u589e\u52a0\u4e00\u5b9a\u7684\u5f00\u9500\u3002<\/p>\n<p>\u5171\u4eab\u5185\u5b58\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u64cd\u4f5c\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58&#xff0c;\u8fd9\u610f\u5473\u7740\u6240\u6709\u8fdb\u7a0b\u770b\u5230\u7684\u662f\u540c\u4e00\u4e2a\u6570\u636e\u526f\u672c\u3002\u800c\u5185\u5b58\u6620\u5c04\u5219\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u81ea\u5df1\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u72ec\u7acb\u7684\u5185\u5b58\u6620\u5c04\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u6620\u5c04\u53ef\u80fd\u6307\u5411\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58&#xff0c;\u4f46\u6bcf\u4e2a\u8fdb\u7a0b\u7684\u64cd\u4f5c\u662f\u72ec\u7acb\u7684&#xff0c;\u4e0d\u4f1a\u76f4\u63a5\u5f71\u54cd\u5176\u4ed6\u8fdb\u7a0b\u7684\u5185\u5b58\u6620\u5c04\u3002<\/p>\n<p>\u5373\u4f7f\u4e00\u4e2a\u8fdb\u7a0b\u7a81\u7136\u9000\u51fa&#xff0c;\u5171\u4eab\u5185\u5b58\u4ecd\u7136\u5b58\u5728&#xff0c;\u5176\u4ed6\u8fdb\u7a0b\u4ecd\u7136\u53ef\u4ee5\u7ee7\u7eed\u8bbf\u95ee\u548c\u64cd\u4f5c\u5171\u4eab\u5185\u5b58\u3002\u4f46\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u7a81\u7136\u9000\u51fa&#xff0c;\u8be5\u8fdb\u7a0b\u7684\u5185\u5b58\u6620\u5c04\u533a\u4f1a\u88ab\u9500\u6bc1&#xff0c;\u5176\u4ed6\u8fdb\u7a0b\u65e0\u6cd5\u518d\u8bbf\u95ee\u8be5\u6620\u5c04\u533a\u3002<\/p>\n<p>\u5982\u679c\u8fd0\u884c\u5171\u4eab\u5185\u5b58\u7684\u7535\u8111\u6b7b\u673a\u6216\u5b95\u673a&#xff0c;\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u6570\u636e\u4f1a\u4e22\u5931&#xff0c;\u56e0\u4e3a\u5171\u4eab\u5185\u5b58\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u7684\u5185\u5b58\u7ba1\u7406\u3002\u5982\u679c\u7535\u8111\u6b7b\u673a\u6216\u5b95\u673a&#xff0c;\u5185\u5b58\u6620\u5c04\u533a\u7684\u6570\u636e\u4ecd\u7136\u5b58\u5728&#xff0c;\u56e0\u4e3a\u8fd9\u4e9b\u6570\u636e\u662f\u57fa\u4e8e\u78c1\u76d8\u6587\u4ef6\u7684\u3002\u53ea\u8981\u78c1\u76d8\u6587\u4ef6\u672a\u88ab\u635f\u574f&#xff0c;\u6570\u636e\u4ecd\u7136\u53ef\u4ee5\u6062\u590d\u3002<\/p>\n<p>\u5f53\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u8be5\u8fdb\u7a0b\u7684\u5185\u5b58\u6620\u5c04\u533a\u4f1a\u88ab\u9500\u6bc1\u3002\u8fd9\u610f\u5473\u7740\u5176\u4ed6\u8fdb\u7a0b\u65e0\u6cd5\u518d\u8bbf\u95ee\u8be5\u6620\u5c04\u533a\u3002 \u5f53\u4e00\u4e2a\u8fdb\u7a0b\u9000\u51fa\u65f6&#xff0c;\u5b83\u4f1a\u81ea\u52a8\u4e0e\u5171\u4eab\u5185\u5b58\u53d6\u6d88\u5173\u8054&#xff0c;\u4f46\u5171\u4eab\u5185\u5b58\u672c\u8eab\u4ecd\u7136\u5b58\u5728\u3002\u5171\u4eab\u5185\u5b58\u53ea\u6709\u5728\u6240\u6709\u5173\u8054\u7684\u8fdb\u7a0b\u90fd\u9000\u51fa\u540e\u624d\u4f1a\u88ab\u6807\u8bb0\u4e3a\u5220\u9664\u3002\u5982\u679c\u7cfb\u7edf\u5173\u673a&#xff0c;\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u6570\u636e\u4e5f\u4f1a\u4e22\u5931\u3002<\/p>\n<h3>1.7 \u5c0fdemo<\/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\">&lt;stdio.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 macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string.h&gt;<\/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> <span class=\"token punctuation\">{<\/span>    <\/p>\n<p>    <span class=\"token comment\">\/\/ 1.\u83b7\u53d6\u4e00\u4e2a\u5171\u4eab\u5185\u5b58<\/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><span class=\"token number\">100<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> IPC_CREAT<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shmid : %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> shmid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 2.\u548c\u5f53\u524d\u8fdb\u7a0b\u8fdb\u884c\u5173\u8054<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span> ptr <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 constant\">NULL<\/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 comment\">\/\/ 3.\u8bfb\u6570\u636e<\/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> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">char<\/span> <span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>ptr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6309\u4efb\u610f\u952e\u7ee7\u7eed\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">getchar<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 4.\u89e3\u9664\u5173\u8054<\/span><br \/>\n    <span class=\"token function\">shmdt<\/span><span class=\"token punctuation\">(<\/span>ptr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 5.\u5220\u9664\u5171\u4eab\u5185\u5b58<\/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 constant\">NULL<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <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><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;stdio.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 macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;string.h&gt;<\/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> <span class=\"token punctuation\">{<\/span>    <\/p>\n<p>    <span class=\"token comment\">\/\/ 1.\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u5185\u5b58<\/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><span class=\"token number\">100<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">4096<\/span><span class=\"token punctuation\">,<\/span> IPC_CREAT<span class=\"token operator\">|<\/span><span class=\"token number\">0664<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;shmid : %d\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> shmid<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 2.\u548c\u5f53\u524d\u8fdb\u7a0b\u8fdb\u884c\u5173\u8054<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token operator\">*<\/span> ptr <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 constant\">NULL<\/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\">char<\/span> <span class=\"token operator\">*<\/span> str <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;helloworld&#034;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 3.\u5199\u6570\u636e<\/span><br \/>\n    <span class=\"token function\">memcpy<\/span><span class=\"token punctuation\">(<\/span>ptr<span class=\"token punctuation\">,<\/span> str<span class=\"token punctuation\">,<\/span> <span class=\"token function\">strlen<\/span><span class=\"token punctuation\">(<\/span>str<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/\u8bbe\u7f6e\u4e00\u4e2a\u7b49\u5f85\u64cd\u4f5c&#xff0c;\u4e0d\u7136\u4f1a\u8fdb\u7a0b\u76f4\u63a5\u7ed3\u675f&#xff0c;\u90a3\u4e48\u5171\u4eab\u5185\u5b58\u4e5f\u6ca1\u4e86\u3002<\/span><br \/>\n    <span class=\"token function\">printf<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6309\u4efb\u610f\u952e\u7ee7\u7eed\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">getchar<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 4.\u89e3\u9664\u5173\u8054<\/span><br \/>\n    <span class=\"token function\">shmdt<\/span><span class=\"token punctuation\">(<\/span>ptr<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 5.\u5220\u9664\u5171\u4eab\u5185\u5b58<\/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 constant\">NULL<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <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>\u4e8c\u3001\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u547d\u4ee4<\/h2>\n<p>ipcs <span class=\"token operator\">&#8211;<\/span>a <span class=\"token comment\">\/\/ \u6253\u5370\u5f53\u524d\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u65b9\u5f0f\u7684\u4fe1\u606f<\/span><br \/>\nipcs <span class=\"token operator\">&#8211;<\/span>m <span class=\"token comment\">\/\/ \u6253\u5370\u51fa\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u4fe1\u606f<\/span><br \/>\nipcs <span class=\"token operator\">&#8211;<\/span>q <span class=\"token comment\">\/\/ \u6253\u5370\u51fa\u4f7f\u7528\u6d88\u606f\u961f\u5217\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u4fe1\u606f<\/span><br \/>\nipcs <span class=\"token operator\">&#8211;<\/span>s <span class=\"token comment\">\/\/ \u6253\u5370\u51fa\u4f7f\u7528\u4fe1\u53f7\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u4fe1\u606f<\/span><\/p>\n<p>\u56fe\u4e2d\u7684\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u952e\u662f0x0000&#xff0c;\u5df2\u7ecf\u88ab\u6807\u8bb0\u5220\u9664\u4e86&#xff0c;\u4f46\u662f\u56e0\u4e3a\u8fde\u63a5\u6570\u8fd8\u5b58\u5728&#xff0c;\u6240\u4ee5\u6ca1\u6709\u88ab\u5220\u9664\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250611085052-6849436cbc33d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>ipcrm <span class=\"token operator\">&#8211;<\/span>M shmkey <span class=\"token comment\">\/\/ \u79fb\u9664\u7528shmkey\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u6bb5<\/span><br \/>\nipcrm <span class=\"token operator\">&#8211;<\/span>m shmid <span class=\"token comment\">\/\/ \u79fb\u9664\u7528shmid\u6807\u8bc6\u7684\u5171\u4eab\u5185\u5b58\u6bb5<\/span><br \/>\nipcrm <span class=\"token operator\">&#8211;<\/span>Q msgkey <span class=\"token comment\">\/\/ \u79fb\u9664\u7528msqkey\u521b\u5efa\u7684\u6d88\u606f\u961f\u5217<\/span><br \/>\nipcrm <span class=\"token operator\">&#8211;<\/span>q msqid <span class=\"token comment\">\/\/ \u79fb\u9664\u7528msqid\u6807\u8bc6\u7684\u6d88\u606f\u961f\u5217<\/span><br \/>\nipcrm <span class=\"token operator\">&#8211;<\/span>S semkey <span class=\"token comment\">\/\/ \u79fb\u9664\u7528semkey\u521b\u5efa\u7684\u4fe1\u53f7<\/span><br \/>\nipcrm <span class=\"token operator\">&#8211;<\/span>s semid <span class=\"token comment\">\/\/ \u79fb\u9664\u7528semid\u6807\u8bc6\u7684\u4fe1\u53f7<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u63a2\u8ba8\u4e86\u5171\u4eab\u5185\u5b58\u7684\u5b9e\u73b0\u4e0e\u5e94\u7528\uff0c\u6db5\u76d6\u4ece\u57fa\u7840\u51fd\u6570\u5230\u5b9e\u9645\u6848\u4f8b\u7684\u591a\u4e2a\u65b9\u9762\u3002\u9996\u5148\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u4e86shmget\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u6216\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\uff0c\u4ee5\u53cashmat\u548cshmdt\u51fd\u6570\u5206\u522b\u7528\u4e8e\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u548c\u5206\u79bb\u3002shmctl\u51fd\u6570\u7684\u4f7f\u7528\u5c55\u793a\u4e86\u5982\u4f55\u63a7\u5236\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5c5e\u6027\uff0c\u5305\u62ec\u5220\u9664\u64cd\u4f5c\u3002ftok\u51fd\u6570\u7684\u8bb2\u89e3\u5219\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u5982\u4f55\u751f\u6210\u7528\u4e8e\u6807\u8bc6\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u952e\u503c\u3002\u6b64\u5916\uff0c\u5bf9\u6bd4\u4e86\u5171\u4eab\u5185\u5b58\u4e0e\u5185\u5b58\u6620\u5c04\u7684\u5f02\u540c\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5c0fdemo\u5c55\u793a\u4e86\u5171\u4eab\u5185\u5b58\u7684\u5b9e\u9645\u5e94\u7528_c++ \u5171\u4eab\u5185\u5b58<\/p>\n","protected":false},"author":2,"featured_media":43285,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[251,55,58,43],"topic":[],"class_list":["post-43288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-webserver","tag-c","tag-linux","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\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\/43288.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u63a2\u8ba8\u4e86\u5171\u4eab\u5185\u5b58\u7684\u5b9e\u73b0\u4e0e\u5e94\u7528\uff0c\u6db5\u76d6\u4ece\u57fa\u7840\u51fd\u6570\u5230\u5b9e\u9645\u6848\u4f8b\u7684\u591a\u4e2a\u65b9\u9762\u3002\u9996\u5148\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u4e86shmget\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u6216\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\uff0c\u4ee5\u53cashmat\u548cshmdt\u51fd\u6570\u5206\u522b\u7528\u4e8e\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u548c\u5206\u79bb\u3002shmctl\u51fd\u6570\u7684\u4f7f\u7528\u5c55\u793a\u4e86\u5982\u4f55\u63a7\u5236\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5c5e\u6027\uff0c\u5305\u62ec\u5220\u9664\u64cd\u4f5c\u3002ftok\u51fd\u6570\u7684\u8bb2\u89e3\u5219\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u5982\u4f55\u751f\u6210\u7528\u4e8e\u6807\u8bc6\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u952e\u503c\u3002\u6b64\u5916\uff0c\u5bf9\u6bd4\u4e86\u5171\u4eab\u5185\u5b58\u4e0e\u5185\u5b58\u6620\u5c04\u7684\u5f02\u540c\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5c0fdemo\u5c55\u793a\u4e86\u5171\u4eab\u5185\u5b58\u7684\u5b9e\u9645\u5e94\u7528_c++ \u5171\u4eab\u5185\u5b58\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/43288.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-11T08:50:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250611085052-6849436c2da08.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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43288.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/43288.html\",\"name\":\"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-11T08:50:54+00:00\",\"dateModified\":\"2025-06-11T08:50:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/43288.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/43288.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43288.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\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":"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\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\/43288.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb4k\u6b21\uff0c\u70b9\u8d5e30\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u63a2\u8ba8\u4e86\u5171\u4eab\u5185\u5b58\u7684\u5b9e\u73b0\u4e0e\u5e94\u7528\uff0c\u6db5\u76d6\u4ece\u57fa\u7840\u51fd\u6570\u5230\u5b9e\u9645\u6848\u4f8b\u7684\u591a\u4e2a\u65b9\u9762\u3002\u9996\u5148\uff0c\u8be6\u7ec6\u4ecb\u7ecd\u4e86shmget\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u6216\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\uff0c\u4ee5\u53cashmat\u548cshmdt\u51fd\u6570\u5206\u522b\u7528\u4e8e\u5c06\u5171\u4eab\u5185\u5b58\u6bb5\u9644\u52a0\u5230\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u548c\u5206\u79bb\u3002shmctl\u51fd\u6570\u7684\u4f7f\u7528\u5c55\u793a\u4e86\u5982\u4f55\u63a7\u5236\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5c5e\u6027\uff0c\u5305\u62ec\u5220\u9664\u64cd\u4f5c\u3002ftok\u51fd\u6570\u7684\u8bb2\u89e3\u5219\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u5982\u4f55\u751f\u6210\u7528\u4e8e\u6807\u8bc6\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u952e\u503c\u3002\u6b64\u5916\uff0c\u5bf9\u6bd4\u4e86\u5171\u4eab\u5185\u5b58\u4e0e\u5185\u5b58\u6620\u5c04\u7684\u5f02\u540c\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5c0fdemo\u5c55\u793a\u4e86\u5171\u4eab\u5185\u5b58\u7684\u5b9e\u9645\u5e94\u7528_c++ \u5171\u4eab\u5185\u5b58","og_url":"https:\/\/www.wsisp.com\/helps\/43288.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-11T08:50:54+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250611085052-6849436c2da08.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/43288.html","url":"https:\/\/www.wsisp.com\/helps\/43288.html","name":"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\u5171\u4eab\u5185\u5b58 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-11T08:50:54+00:00","dateModified":"2025-06-11T08:50:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/43288.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/43288.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/43288.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u9ad8\u5e76\u53d1\u670d\u52a1\u5668WebServer\u3011-7\uff1a\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\/43288","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=43288"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/43285"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=43288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=43288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=43288"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=43288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}