{"id":125104,"date":"2022-09-01T16:45:12","date_gmt":"2022-09-01T08:45:12","guid":{"rendered":"http:\/\/www.idc.net\/help\/125104\/"},"modified":"2022-09-01T16:45:12","modified_gmt":"2022-09-01T08:45:12","slug":"%e3%80%8clinux%e7%b3%bb%e7%bb%9f%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98%e4%bf%ae%e6%94%b9%e3%80%8d%ef%bc%9a%e8%af%a6%e8%a7%a3%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95%e5%8f%8a%e6%b3%a8%e6%84%8f%e4%ba%8b","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/125104\/","title":{"rendered":"\u300cLinux\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u4fee\u6539\u300d\uff1a\u8be6\u89e3\u64cd\u4f5c\u65b9\u6cd5\u53ca\u6ce8\u610f\u4e8b\u9879 (linux\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u7684\u4fee\u6539)"},"content":{"rendered":"<p>Linux\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u4fee\u6539\uff1a\u8be6\u89e3\u64cd\u4f5c\u65b9\u6cd5\u53ca\u6ce8\u610f\u4e8b\u9879<\/p>\n<p>Linux\u7cfb\u7edf\u662f\u76ee\u524d\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u5f00\u6e90\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u4e00\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u662f\u975e\u5e38\u5e38\u89c1\u7684\u3002\u5bf9\u4e8e\u8bb8\u591a\u5f00\u53d1\u8005\u548c\u7cfb\u7edf\u7ba1\u7406\u5458\u6765\u8bf4\uff0c\u4fee\u6539\u5171\u4eab\u5185\u5b58\u6765\u6ee1\u8db3\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8bb2\u89e3Linux\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u7684\u4fee\u6539\u64cd\u4f5c\u65b9\u6cd5\u53ca\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<p>\u4e00\u3001Linux\u7cfb\u7edf\u5171\u4eab\u5185\u5b58<\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u5171\u4eab\u5185\u5b58\u662f\u6307\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u7684\u540c\u4e00\u5757\u5185\u5b58\u533a\u57df\u3002\u5728\u8fd9\u6837\u7684\u5185\u5b58\u533a\u57df\u4e2d\uff0c\u53ef\u4ee5\u5b58\u50a8\u4e00\u4e9b\u6570\u636e\uff0c\u4f9b\u591a\u4e2a\u8fdb\u7a0b\u5171\u540c\u4f7f\u7528\u3002\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u6548\u7387\uff0c\u5e76\u4e14\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8fdb\u7a0b\u901a\u4fe1\u5f00\u9500\u3002<\/p>\n<p>\u4e8c\u3001\u4fee\u6539\u5171\u4eab\u5185\u5b58\u7684\u65b9\u6cd5<\/p>\n<p>Linux\u7cfb\u7edf\u4e2d\uff0c\u4fee\u6539\u5171\u4eab\u5185\u5b58\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4e00\u79cd\u662f\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53e6\u4e00\u79cd\u662f\u4f7f\u7528C\u8bed\u8a00\u7f16\u5199\u7a0b\u5e8f\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u5206\u522b\u8bb2\u8ff0\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7684\u5b9e\u73b0\u6b65\u9aa4\u53ca\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<p>1. \u547d\u4ee4\u884c\u4fee\u6539\u5171\u4eab\u5185\u5b58<\/p>\n<p>Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528ipcs\u548cipcrm\u547d\u4ee4\u6765\u83b7\u53d6\u548c\u5220\u9664\u5171\u4eab\u5185\u5b58\u3002\u672c\u8282\u5c06\u91cd\u70b9\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528ipcrm\u547d\u4ee4\u6765\u4fee\u6539\u5171\u4eab\u5185\u5b58\u5927\u5c0f\u3002<\/p>\n<p>\u6b65\u9aa4\u4e00\uff1a\u6253\u5f00\u7ec8\u7aef\u5e76\u767b\u5f55Linux\u7cfb\u7edf\u3002<\/p>\n<p>\u6b65\u9aa4\u4e8c\uff1a\u4f7f\u7528ipcs\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u201c`sh<\/p>\n<p>ipcs -m<\/p>\n<p>\u201c`<\/p>\n<p>\u8fd0\u884c\u8be5\u547d\u4ee4\u540e\uff0c\u5c06\u4f1a\u770b\u5230\u7cfb\u7edf\u4e2d\u7684\u5171\u4eab\u5185\u5b58\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u62ec\u5185\u5b58ID\u3001\u5927\u5c0f\u3001\u62e5\u6709\u8005\u7b49\u4fe1\u606f\u3002<\/p>\n<p>\u6b65\u9aa4\u4e09\uff1a\u4f7f\u7528ipcrm\u547d\u4ee4\u5220\u9664\u539f\u6765\u7684\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u201c`sh<\/p>\n<p>ipcrm -m [\u5185\u5b58ID]<\/p>\n<p>\u201c`<\/p>\n<p>\u7531\u4e8e\u9700\u8981\u4fee\u6539\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\uff0c\u5fc5\u987b\u5148\u5220\u9664\u539f\u6765\u7684\u5171\u4eab\u5185\u5b58\uff0c\u518d\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u3002\u5728\u5220\u9664\u5171\u4eab\u5185\u5b58\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u9700\u8981\u5220\u9664\u7684\u5185\u5b58ID\uff0c\u53ef\u4ee5\u901a\u8fc7ipcs\u547d\u4ee4\u67e5\u770b\u5230\u3002<\/p>\n<p>\u6b65\u9aa4\u56db\uff1a\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u201c`sh<\/p>\n<p>ipcs -m<\/p>\n<p>ipcmk -m [\u65b0\u7684\u5185\u5b58\u5927\u5c0f] [\u65b0\u7684\u5185\u5b58\u952e\u503c]<\/p>\n<p>\u201c`<\/p>\n<p>\u5728\u521b\u5efa\u65b0\u7684\u5171\u4eab\u5185\u5b58\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u65b0\u7684\u5185\u5b58\u5927\u5c0f\u548c\u952e\u503c\u3002\u952e\u503c\u7528\u4e8e\u6807\u8bc6\u5171\u4eab\u5185\u5b58\uff0c\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7\u8be5\u6807\u8bc6\u627e\u5230\u540c\u4e00\u5757\u5185\u5b58\u533a\u57df\u3002\u5185\u5b58\u5927\u5c0f\u4e00\u822c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u3002<\/p>\n<p>2. C\u8bed\u8a00\u7f16\u5199\u7a0b\u5e8f\u4fee\u6539\u5171\u4eab\u5185\u5b58<\/p>\n<p>\u9664\u4e86\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528C\u8bed\u8a00\u7f16\u5199\u7a0b\u5e8f\u6765\u4fee\u6539\u5171\u4eab\u5185\u5b58\u3002\u6253\u5f00\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u5668\uff0c\u65b0\u5efa\u4e00\u4e2aC\u8bed\u8a00\u6e90\u6587\u4ef6\uff0c\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>\u201c`c<\/p>\n<p>#include <\/p>\n<\/p>\n<p>#include <\/p>\n<\/p>\n<p>#include <\/p>\n<\/p>\n<p>#include <\/p>\n<\/p>\n<p>#define SIZE 1024<\/p>\n<p>int mn(int argc, char **argv)<\/p>\n<p>{<\/p>\n<p> int shmid;<\/p>\n<p> key_t key;<\/p>\n<p> char *shm;<\/p>\n<p> \/* \u751f\u6210key *\/<\/p>\n<p> if ((key = ftok(\u201c.\u201d, \u2018R\u2019)) == -1) {<\/p>\n<p> perror(\u201cftok\u201d);<\/p>\n<p> exit(1);<\/p>\n<p> }<\/p>\n<p> \/* \u521b\u5efa\u5171\u4eab\u5185\u5b58 *\/<\/p>\n<p> if ((shmid = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0666)) == -1) {<\/p>\n<p> perror(\u201cshmget\u201d);<\/p>\n<p> exit(1);<\/p>\n<p> }<\/p>\n<p> \/* \u6620\u5c04\u5171\u4eab\u5185\u5b58 *\/<\/p>\n<p> if ((shm = shmat(shmid, 0, 0)) == (char *)-1) {<\/p>\n<p> perror(\u201cshmat\u201d);<\/p>\n<p> exit(1);<\/p>\n<p> }<\/p>\n<p> \/* \u53d6\u6d88\u6620\u5c04\u5171\u4eab\u5185\u5b58 *\/<\/p>\n<p> if (shmdt(shm) == -1) {<\/p>\n<p> perror(\u201cshmdt\u201d);<\/p>\n<p> exit(1);<\/p>\n<p> }<\/p>\n<p> \/* \u5220\u9664\u5171\u4eab\u5185\u5b58 *\/<\/p>\n<p> if (shmctl(shmid, IPC_RMID, 0) == -1) {<\/p>\n<p> perror(\u201cshmctl\u201d);<\/p>\n<p> exit(1);<\/p>\n<p> }<\/p>\n<p> \/* \u91cd\u65b0\u521b\u5efa\u5171\u4eab\u5185\u5b58 *\/<\/p>\n<p> if ((shmid = shmget(key, SIZE, IPC_CREAT | IPC_EXCL | 0666)) == -1) {<\/p>\n<p> perror(\u201cshmget\u201d);<\/p>\n<p> exit(1);<\/p>\n<p> }<\/p>\n<p> return 0;<\/p>\n<p>}<\/p>\n<p>\u201c`<\/p>\n<p>\u5728\u8be5\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u7cfb\u7edf\u63d0\u4f9b\u7684shmget\u3001shmat\u3001shmdt\u548cshmctl\u7b49\u51fd\u6570\u6765\u5b8c\u6210\u5171\u4eab\u5185\u5b58\u7684\u64cd\u4f5c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<p>\u6b65\u9aa4\u4e00\uff1a\u5b9a\u4e49\u9700\u8981\u4f7f\u7528\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u5728\u7a0b\u5e8f\u5f00\u5934\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u9700\u8981\u4f7f\u7528\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u5171\u4eab\u5185\u5b58ID\u3001\u952e\u503c\u548c\u5185\u5b58\u5927\u5c0f\u7b49\u4fe1\u606f\u3002<\/p>\n<p>\u6b65\u9aa4\u4e8c\uff1a\u751f\u6210key\u3002<\/p>\n<p>\u5728\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ftok\u51fd\u6570\u751f\u6210\u5185\u5b58\u6807\u8bc6\u7b26key\u3002\u8be5\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4e4b\u4e00\u4e2a\u662f\u5305\u542b\u8def\u5f84\u540d\u53ca\u6587\u4ef6\u540d\u7684\u5b57\u7b26\u4e32\u6307\u9488\uff0c\u7b2c\u4e8c\u4e2a\u662f\u9879\u76ee\u6807\u8bc6\u7b26\u3002\u8be5\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u4f5c\u4e3a\u5171\u4eab\u5185\u5b58\u7684key\u3002<\/p>\n<p>\u6b65\u9aa4\u4e09\uff1a\u521b\u5efa\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u4f7f\u7528shmget\u51fd\u6570\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u5e76\u6307\u5b9a\u5185\u5b58\u5927\u5c0f\u3001\u6807\u8bc6\u548c\u8bbf\u95ee\u6743\u9650\u7b49\u4fe1\u606f\u3002\u5982\u679c\u5171\u4eab\u5185\u5b58\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u8be5\u5171\u4eab\u5185\u5b58\u7684ID\u3002<\/p>\n<p>\u6b65\u9aa4\u56db\uff1a\u6620\u5c04\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u4f7f\u7528shmat\u51fd\u6570\u5c06\u5171\u4eab\u5185\u5b58\u6620\u5c04\u5230\u672c\u5730\u8fdb\u7a0b\u4e2d\uff0c\u8fd4\u56de\u6307\u5411\u5171\u4eab\u5185\u5b58\u7684\u6307\u9488\u3002<\/p>\n<p>\u6b65\u9aa4\u4e94\uff1a\u53d6\u6d88\u6620\u5c04\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u4f7f\u7528shmdt\u51fd\u6570\u5c06\u5171\u4eab\u5185\u5b58\u4e0e\u672c\u5730\u8fdb\u7a0b\u7684\u6620\u5c04\u5173\u7cfb\u53d6\u6d88\uff0c\u91ca\u653e\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u6b65\u9aa4\u516d\uff1a\u5220\u9664\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u4f7f\u7528shmctl\u51fd\u6570\u5220\u9664\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u6b65\u9aa4\u4e03\uff1a\u91cd\u65b0\u521b\u5efa\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u4f7f\u7528shmget\u51fd\u6570\u91cd\u65b0\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u5e76\u6307\u5b9a\u65b0\u7684\u5185\u5b58\u5927\u5c0f\u3002<\/p>\n<p>\u4e09\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<p>\u5728\u4fee\u6539\u5171\u4eab\u5185\u5b58\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<p>1. \u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u5fc5\u987b\u662f2\u7684\u6574\u6b21\u5e42\uff0c\u5982256\u3001512\u30011024\u7b49\u3002<\/p>\n<p>2. \u53ea\u6709\u5171\u4eab\u5185\u5b58\u7684\u6240\u6709\u8005\u6216\u8d85\u7ea7\u7528\u6237\u624d\u6709\u6743\u9650\u4fee\u6539\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>3. \u4fee\u6539\u5171\u4eab\u5185\u5b58\u5927\u5c0f\u65f6\uff0c\u5fc5\u987b\u5148\u5220\u9664\u539f\u6765\u7684\u5171\u4eab\u5185\u5b58\uff0c\u5728\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>4. \u5fc5\u987b\u4fdd\u8bc1\u6240\u6709\u8fdb\u7a0b\u4f7f\u7528\u7684\u5185\u5b58\u6807\u8bc6\u7b26key\u662f\u4e00\u81f4\u7684\uff0c\u5426\u5219\u65e0\u6cd5\u8bbf\u95ee\u540c\u4e00\u5757\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>5. \u5bf9\u4e8e\u4f7f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u7a0b\u5e8f\uff0c\u5fc5\u987b\u5148\u628a\u76f8\u5e94\u7684\u5934\u6587\u4ef6\u5305\u542b\u8fdb\u6765\uff0c\u624d\u80fd\u4f7f\u7528\u76f8\u5173\u51fd\u6570\u3002<\/p>\n<p>\u7ed3\u8bba<\/p>\n<p><strong>\u76f8\u5173\u95ee\u9898\u62d3\u5c55\u9605\u8bfb\uff1a<\/strong><\/p>\n<ul>\n<li>linux\u4e24\u79cd\u5171\u4eab\u5185\u5b58\u7684\u533a\u522b<\/li>\n<li>linux\u5171\u4eab\u5185\u5b58\u7684\u5206\u914d<\/li>\n<\/ul>\n<h3>linux\u4e24\u79cd\u5171\u4eab\u5185\u5b58\u7684\u533a\u522b<\/h3>\n<p>\u6301\u4e45\u6027\u4e0d\u540c\u3001\u6587\u4ef6\u53cd\u5c04\u65b9\u5f0f\u4e0d\u540c\u3002<\/p>\n<p>1\u3001\u6301\u4e45\u6027\u4e0d\u540c\u3002sysvshm\u662f\u6301\u4e45\u5316\u7684\uff0c\u9664\u975e\u88ab\u4e00\u4e2a\u8fdb\u7a0b\u660e\u786e\u7684\u82b9\u54d7\u5220\u9664\uff0c\u5426\u5219\u5b83\u59cb\u7ec8\u5b58\u5728\u5dde\u7c97\u4e8e\u5185\u5b58\u91cc\uff0c\u76f4\u5230\u7cfb\u7edf\u5173\u673a\u3002mmap\u6620\u5c04\u7684\u5185\u5b58\u5728\u4e0d\u662f\u6301\u4e45\u5316\u7684\uff0c\u5047\u5982\u8fdb\u7a0b\u5173\u95ed\uff0c\u6620\u5c04\u968f\u5373\u5931\u6548\uff0c\u9664\u975e\u4e8b\u524d\u5df2\u7ecf\u6620\u5c04\u5230\u4e86\u4e00\u4e2a\u6587\u4ef6\u4e0a\u3002<\/p>\n<p>2\u3001\u6587\u4ef6\u53cd\u5c04\u65b9\u5f0f\u4e0d\u540c\u3002\u524d\u8005\u7528COW\u7684\u65b9\u5f0f\uff0c\u628a\u6587\u4ef6\u6620\u5c04\u5230\u5f53\u524d\u7684\u8fdb\u7a0b\u7a7a\u95f4\uff0c\u4fee\u6539\u64cd\u4f5c\u4e0d\u4f1a\u6539\u52a8\u6e90\u6587\u4ef6\u3002\u540e\u8005\u76f4\u63a5\u628a\u6587\u4ef6\u6620\u5c04\u5230\u5f53\u524d\u7684\u8fdb\u7a0b\u7a7a\u95f4\uff0c\u6240\u6709\u7684\u4fee\u6539\u4f1a\u76f4\u63a5\u53cd\u5e94\u5230\u6587\u4ef6\u7684page cache\uff0c\u800c\u540e\u7531\u518c\u9996\u9547\u5185\u6838\u81ea\u52a8\u540c\u6b65\u5230\u6620\u5c04\u6587\u4ef6\u4e0a\u3002<\/p>\n<h3>linux\u5171\u4eab\u5185\u5b58\u7684\u5206\u914d<\/h3>\n<p>\u8fdb\u7a0b\u901a\u8fc7\u8c03\u7528shmget\uff08Shared Memory GET\uff0c\u83b7\u53d6\u5171\u4eab\u5185\u5b58\uff09\u6765\u5206\u914d\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u5757\u3002<\/p>\n<p>\u8be5\u51fd\u6570\u7684\u4e4b\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u7528\u6765\u6807\u8bc6\u5171\u4eab\u5185\u5b58\u5757\u7684\u952e\u503c\u3002\u5f7c\u6b64\u65e0\u5173\u7684\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u540c\u4e00\u4e2a\u952e\u4ee5\u83b7\u53d6\u5bf9\u540c\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u5757\u7684\u8bbf\u95ee\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u5176\u5b83\u7a0b\u5e8f\u4e5f\u53ef\u80fd\u6311\u9009\u4e86\u540c\u6837\u7684\u7279\u5b9a\u503c\u4f5c\u4e3a\u81ea\u5df1\u5206\u914d\u5171\u4eab\u5185\u5b58\u7684\u952e\u503c\uff0c\u4ece\u800c\u4ea7\u751f\u51b2\u7a81\u3002\u7528\u7279\u6b8a\u5e38\u91cfIPC_PRIVATE\u4f5c\u4e3a\u952e\u503c\u53ef\u4ee5\u4fdd\u8bc1\u643a\u654f\u9965\u7cfb\u7edf\u5efa\u7acb\u4e00\u4e2a\u5168\u65b0\u7684\u5171\u4eab\u5185\u5b58\u5757\u3002<\/p>\n<p>\u8be5\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u4e86\u6240\u7533\u8bf7\u7684\u5185\u5b58\u5757\u7684\u5927\u5c0f\u3002\u56e0\u4e3a\u8fd9\u4e9b\u5185\u5b58\u5757\u662f\u4ee5\u9875\u9762\u4e3a\u5355\u4f4d\u8fdb\u884c\u5206\u914d\u7684\uff0c\u5b9e\u9645\u5206\u914d\u7684\u5185\u5b58\u5757\u5927\u5c0f\u5c06\u88ab\u6269\u5927\u5230\u9875\u9762\u5927\u5c0f\u7684\u6574\u6570\u500d\u3002<\/p>\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u4e00\u7ec4\u6807\u5fd7\uff0c\u901a\u8fc7\u7279\u5b9a\u5e38\u91cf\u7684\u6309\u4f4d\u6216\u64cd\u4f5c\u6765shmget\u3002\u8fd9\u4e9b\u7279\u5b9a\u5e38\u91cf\u5305\u62ec\uff1a<\/p>\n<p>IPC_CREAT\uff1a\u8fd9\u4e2a\u6807\u5fd7\u8868\u793a\u5e94\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u5757\u3002\u901a\u8fc7\u6307\u5b9a\u8fd9\u4e2a\u6807\u5fd7\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6307\u5b9a\u952e\u503c\u7684\u65b0\u5171\u4eab\u5185\u5b58\u5757\u3002<\/p>\n<p>IPC_EXCL\uff1a\u8fd9\u4e2a\u6807\u5fd7\u53ea\u80fd\u4e0e IPC_CREAT \u540c\u65f6\u4f7f\u7528\u3002\u5f53\u6307\u5b9a\u8fd9\u4e2a\u6807\u5fd7\u7684\u65f6\u5019\uff0c\u5982\u679c\u5df2\u6709\u4e00\u4e2a\u5177\u6709\u8fd9\u4e2a\u952e\u503c\u7684\u5171\u4eab\u5185\u5b58\u5757\u5b58\u5728\uff0c\u5219shmget\u4f1a\u8c03\u7528\u5931\u8d25\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u4e2a\u6807\u5fd7\u5c06\u4f7f\u7ebf\u7a0b\u83b7\u5f97\u4e00\u4e2a\u201c\u72ec\u6709\u201d\u7684\u5171\u4eab\u5185\u5b58\u5757\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u8fd9\u4e2a\u6807\u5fd7\u800c\u7cfb\u7edf\u4e2d\u5b58\u5728\u4e00\u4e2a\u5177\u6709\u76f8\u540c\u952e\u503c\u7684\u5171\u4eab\u5185\u5b58\u5757\uff0cshmget\u4f1a\u8fd4\u56de\u8fd9\u4e2a\u5df2\u7ecf\u5efa\u7acb\u7684\u5171\u4eab\u5185\u5b58\u5757\uff0c\u800c\u4e0d\u662f\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u3002<\/p>\n<p>\u6a21\u5f0f\u6807\u5fd7\uff1a\u8fd9\u4e2a\u503c\u75319\u4e2a\u4f4d\u7ec4\u6210\uff0c\u5206\u522b\u8868\u793a\u5c5e\u4e3b\u3001\u5c5e\u7ec4\u548c\u5176\u5b83\u7528\u6237\u5bf9\u8be5\u5185\u5b58\u5757\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5176\u4e2d\u8868\u793a\u6267\u884c\u6743\u9650\u7684\u4f4d\u5c06\u88ab\u62ff\u886b\u5ffd\u7565\u3002\u6307\u660e\u8bbf\u95ee\u6743\u9650\u7684\u4e00\u4e2a\u7b80\u5355\u529e\u6cd5\u662f\u5229\u7528\u4e2d\u6307\u5b9a\uff0c\u5e76\u4e14\u5728\u624b\u518c\u9875\u7b2c\u4e8c\u8282stat\u6761\u76ee\u4e2d\u8bf4\u660e\u4e86\u7684\u5e38\u91cf\u6307\u5b9a\u3002\u4f8b\u5982\uff0cS_IRUSR\u548cS_IWUSR\u5206\u522b\u6307\u5b9a\u4e86\u8be5\u5185\u5b58\u5757\u5c5e\u4e3b\u7684\u8bfb\u5199\u6743\u9650\uff0c\u800c S_IROTH\u548cS_IWOTH\u5219\u6307\u5b9a\u4e86\u5176\u5b83\u8fa9\u8fd4\u7528\u6237\u7684\u8bfb\u5199\u6743\u9650\u3002 \u4e0b\u9762\u4f8b\u5b50\u4e2dshmget\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u5757\uff08\u5f53shm_key\u5df2\u88ab\u5360\u7528\u65f6\u5219\u83b7\u53d6\u5bf9\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u5171\u4eab\u5185\u5b58\u5757\u7684\u8bbf\u95ee\uff09\uff0c\u4e14\u53ea\u6709\u5c5e\u4e3b\u5bf9\u8be5\u5185\u5b58\u5757\u5177\u6709\u8bfb\u5199\u6743\u9650\uff0c\u5176\u5b83\u7528\u6237\u4e0d\u53ef\u8bfb\u5199\u3002<\/p>\n<p>int segment_id = shmget (shm_key, getpagesize (), IPC_CREAT | S_IRUSR| S_IWUSR ); \u5982\u679c\u8c03\u7528\u6210\u529f\uff0cshmget\u5c06\u8fd4\u56de\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6807\u8bc6\u7b26\u3002\u5982\u679c\u8be5\u5171\u4eab\u5185\u5b58\u5757\u5df2\u7ecf\u5b58\u5728\uff0c\u7cfb\u7edf\u4f1a\u68c0\u67e5\u8bbf\u95ee\u6743\u9650\uff0c\u540c\u65f6\u4f1a\u68c0\u67e5\u8be5\u5185\u5b58\u5757\u662f\u5426\u88ab\u6807\u8bb0\u4e3a\u7b49\u5f85\u6467\u6bc1\u72b6\u6001\u3002<\/p>\n<p>\u5173\u4e8elinux\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u7684\u4fee\u6539\u7684\u4ecb\u7ecd\u5230\u6b64\u5c31\u7ed3\u675f\u4e86\uff0c\u4e0d\u77e5\u9053\u4f60\u4ece\u4e2d\u627e\u5230\u4f60\u9700\u8981\u7684\u4fe1\u606f\u4e86\u5417 \uff1f\u5982\u679c\u4f60\u8fd8\u60f3\u4e86\u89e3\u66f4\u591a\u8fd9\u65b9\u9762\u7684\u4fe1\u606f\uff0c\u8bb0\u5f97\u6536\u85cf\u5173\u6ce8\u672c\u7ad9\u3002<\/p>\n<p><a href=\"http:https:\/\/idc.net\/\">\u9999\u6e2f\u670d\u52a1\u5668<\/a>\u9996\u9009\u540e\u6d6a\u4e91\uff0c2H2G\u9996\u670810\u5143\u5f00\u901a\u3002<br \/>\u540e\u6d6a\u4e91\uff08www.IDC.Net\uff09\u63d0\u4f9b\u7b80\u5355\u597d\u7528\uff0c\u4ef7\u683c\u539a\u9053\u7684\u9999\u6e2f\/\u7f8e\u56fd\u4e91\u670d\u52a1\u5668\u548c\u72ec\u7acb\u670d\u52a1\u5668\u3002IDC+ISP+ICP\u8d44\u8d28\u3002ARIN\u548cAPNIC\u4f1a\u5458\u3002\u6210\u719f\u6280\u672f\u56e2\u961f15\u5e74\u884c\u4e1a\u7ecf\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u7cfb\u7edf\u5171\u4eab\u5185\u5b58\u4fee\u6539\uff1a\u8be6\u89e3\u64cd\u4f5c\u65b9\u6cd5\u53ca\u6ce8\u610f\u4e8b\u9879 Linux\u7cfb\u7edf\u662f\u76ee\u524d\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u5f00\u6e90\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u4e00\uff0c\u5176\u4e2d\u6d89\u53ca [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-125104","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/125104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=125104"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/125104\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=125104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=125104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=125104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}