{"id":1866,"date":"2018-10-24T15:45:22","date_gmt":"2018-10-24T07:45:22","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1866"},"modified":"2018-10-30T11:10:21","modified_gmt":"2018-10-30T03:10:21","slug":"linux%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98%e4%bd%bf%e7%94%a8%e5%b8%b8%e8%a7%81%e9%99%b7%e9%98%b1%e4%b8%8e%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1866","title":{"rendered":"Linux\u5171\u4eab\u5185\u5b58\u4f7f\u7528\u5e38\u89c1\u9677\u9631\u4e0e\u5206\u6790"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<div class=\"article-info-box\">\n<div class=\"article-bar-top\">\n<div class=\"tags-box space\">\u6240\u8c13\u5171\u4eab\u5185\u5b58\u5c31\u662f\u4f7f\u5f97\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u540c\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\uff0c\u662f\u6700\u5feb\u7684\u53ef\u7528IPC\u5f62\u5f0f\u3002\u662f\u9488\u5bf9\u5176\u4ed6\u901a\u4fe1\u673a\u5236\u8fd0\u884c\u6548\u7387\u8f83\u4f4e\u800c\u8bbe\u8ba1\u7684\u3002\u5f80\u5f80\u4e0e\u5176\u5b83\u901a\u4fe1\u673a\u5236\uff0c\u5982\u4fe1\u53f7\u91cf\u7ed3\u5408\u4f7f\u7528\uff0c\u6765\u8fbe\u5230\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\u53ca\u4e92\u65a5\u3002\u5176\u4ed6\u8fdb\u7a0b\u80fd\u628a\u540c\u4e00\u6bb5\u5171\u4eab\u5185\u5b58\u6bb5\u201c\u8fde\u63a5\u5230\u201d\u4ed6\u4eec\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u91cc\u53bb\u3002\u6240\u6709\u8fdb\u7a0b\u90fd\u80fd\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u5411\u8fd9\u6bb5\u5171\u4eab\u5185\u5b58\u5199\u4e86\u6570\u636e\uff0c\u6240\u505a\u7684\u6539\u52a8\u4f1a\u5373\u65f6\u88ab\u6709\u8bbf\u95ee\u540c\u4e00\u6bb5\u5171\u4eab\u5185\u5b58\u7684\u5176\u4ed6\u8fdb\u7a0b\u770b\u5230\u3002\u5171\u4eab\u5185\u5b58\u7684\u4f7f\u7528\u5927\u5927\u964d\u4f4e\u4e86\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\u5185\u5b58\u7684\u6d88\u8017\uff0c\u4f46\u662f\u5171\u4eab\u5185\u5b58\u7684\u4f7f\u7528\u4e2d\u6709\u5f88\u591a\u7684\u9677\u9631\uff0c\u4e00\u4e0d\u6ce8\u610f\u5c31\u5f88\u5bb9\u6613\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<article>\n<div id=\"article_content\" class=\"article_content clearfix csdn-tracking-statistics\" data-pid=\"blog\" data-mod=\"popu_307\" data-dsm=\"post\">\n<div class=\"htmledit_views\">\n<p>\u8d85\u8fc7\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u9650\u5236\uff1f<\/p>\n<p>\u5728\u4e00\u4e2alinux\u670d\u52a1\u5668\u4e0a\uff0c\u5171\u4eab\u5185\u5b58\u7684\u603b\u4f53\u5927\u5c0f\u662f\u6709\u9650\u5236\u7684\uff0c\u8fd9\u4e2a\u5927\u5c0f\u901a\u8fc7SHMMAX\u53c2\u6570\u6765\u5b9a\u4e49\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u786e\u5b9a SHMMAX \u7684\u503c\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#\u00a0cat\u00a0\/proc\/sys\/kernel\/shmmax<\/li>\n<\/ol>\n<p>\u5982\u679c\u673a\u5668\u4e0a\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u7684\u603b\u5171\u5927\u5c0f\u8d85\u51fa\u4e86\u8fd9\u4e2a\u9650\u5236\uff0c\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u6807\u51c6\u9519\u8befperror\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u7684\u4fe1\u606f\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">unable\u00a0to\u00a0attach\u00a0to\u00a0shared\u00a0memory<\/li>\n<\/ol>\n<p>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p>1\u3001\u8bbe\u7f6e SHMMAX<\/p>\n<p>SHMMAX \u7684\u9ed8\u8ba4\u503c\u662f 32MB \u3002\u4e00\u822c\u4f7f\u7528\u4e0b\u5217\u65b9\u6cd5\u4e4b\u4e00\u79cd\u5c06 SHMMAX \u53c2\u6570\u8bbe\u4e3a 2GB \uff1a<\/p>\n<p>\u901a\u8fc7\u76f4\u63a5\u66f4\u6539 \/proc \u6587\u4ef6\u7cfb\u7edf\uff0c\u4f60\u4e0d\u9700\u91cd\u65b0\u542f\u52a8\u673a\u5668\u5c31\u53ef\u4ee5\u6539\u53d8 SHMMAX \u7684\u9ed8\u8ba4\u8bbe\u7f6e\u3002\u6211\u4f7f\u7528\u7684\u65b9\u6cd5\u662f\u5c06\u4ee5\u4e0b\u547d\u4ee4\u653e\u5165 \/&gt;etc\/rc.local \u542f\u52a8\u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre><\/pre>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#\u00a0echo\u00a0&#8220;2147483648&#8221;\u00a0<span class=\"tag\">&amp;gt;<\/span>\u00a0\/proc\/sys\/kernel\/shmmax<\/li>\n<\/ol>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 sysctl \u547d\u4ee4\u6765\u66f4\u6539 SHMMAX \u7684\u503c\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#\u00a0sysctl\u00a0-w\u00a0<span class=\"attribute\">kernel.shmmax<\/span>=<span class=\"attribute-value\">2147483648<\/span><\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u901a\u8fc7\u5c06\u8be5\u5185\u6838\u53c2\u6570\u63d2\u5165\u5230 \/etc\/sysctl.conf \u542f\u52a8\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u8fd9\u79cd\u66f4\u6539\u6c38\u4e45\u6709\u6548\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#\u00a0echo\u00a0&#8220;<span class=\"attribute\">kernel.shmmax<\/span>=<span class=\"attribute-value\">2147483648<\/span>&#8221;\u00a0<span class=\"tag\">&amp;gt;<\/span><span class=\"tag\">&amp;gt;<\/span>\u00a0\/etc\/sysctl.conf<\/li>\n<\/ol>\n<p>2\u3001\u8bbe\u7f6e SHMMNI<\/p>\n<p>\u6211\u4eec\u73b0\u5728\u6765\u770b SHMMNI \u53c2\u6570\u3002\u8fd9\u4e2a\u5185\u6838\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u7cfb\u7edf\u8303\u56f4\u5185\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u6700\u5927\u6570\u91cf\u3002\u8be5\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\u662f 4096 \u3002\u8fd9\u4e00\u6570\u503c\u5df2\u7ecf\u8db3\u591f\uff0c\u901a\u5e38\u4e0d\u9700\u8981\u66f4\u6539\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u786e\u5b9a SHMMNI \u7684\u503c\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#\u00a0cat\u00a0\/proc\/sys\/kernel\/shmmni<\/li>\n<li>4096<\/li>\n<\/ol>\n<p>3\u3001\u8bbe\u7f6e SHMALL<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u6765\u770b SHMALL \u5171\u4eab\u5185\u5b58\u5185\u6838\u53c2\u6570\u3002\u8be5\u53c2\u6570\u63a7\u5236\u7740\u7cfb\u7edf\u4e00\u6b21\u53ef\u4ee5\u4f7f\u7528\u7684\u5171\u4eab\u5185\u5b58\u603b\u91cf\uff08\u4ee5\u9875\u4e3a\u5355\u4f4d\uff09\u3002\u7b80\u8a00\u4e4b\uff0c\u8be5\u53c2\u6570\u7684\u503c\u59cb\u7ec8\u5e94\u8be5\u81f3\u5c11\u4e3a\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">ceil(SHMMAX\/PAGE_SIZE)<\/li>\n<\/ol>\n<p>SHMALL \u7684\u9ed8\u8ba4\u5927\u5c0f\u4e3a 2097152 \uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u67e5\u8be2\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#\u00a0cat\u00a0\/proc\/sys\/kernel\/shmall<\/li>\n<li>2097152<\/li>\n<\/ol>\n<p>SHMALL \u7684\u9ed8\u8ba4\u8bbe\u7f6e\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u5e94\u8be5\u8db3\u591f\u4f7f\u7528\u3002<\/p>\n<p>\u6ce8\u610f\uff1a \u5728 i386 \u5e73\u53f0\u4e0a Red Hat Linux \u7684 \u9875\u9762\u5927\u5c0f \u4e3a 4096 \u5b57\u8282\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 bigpages \uff0c\u5b83\u652f\u6301\u914d\u7f6e\u66f4\u5927\u7684\u5185\u5b58\u9875\u9762\u5c3a\u5bf8\u3002<\/p>\n<p>\u591a\u6b21\u8fdb\u884cshmat\u4f1a\u51fa\u73b0\u4ec0\u4e48\u95ee\u9898\uff1f<\/p>\n<p>\u5f53\u9996\u6b21\u521b\u5efa\u5171\u4eab\u5185\u5b58\u6bb5\u65f6\uff0c\u5b83\u5e76\u4e0d\u80fd\u88ab\u4efb\u4f55\u8fdb\u7a0b\u6240\u8bbf\u95ee\u3002\u4e3a\u4e86\u4f7f\u5171\u4eab\u5185\u5b58\u533a\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u5219\u5fc5\u987b\u901a\u8fc7 shmat \u51fd\u6570\u5c06\u5176\u9644\u52a0( attach )\u5230\u81ea\u5df1\u7684\u8fdb\u7a0b\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u6837\u8fdb\u7a0b\u5c31\u4e0e\u5171\u4eab\u5185\u5b58\u5efa\u7acb\u4e86\u8fde\u63a5\u3002\u8be5\u51fd\u6570\u58f0\u660e\u5728 linux\/shm.h\u4e2d\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#include<\/li>\n<li>#include<\/li>\n<li class=\"alt\">void\u00a0*shmat(int\u00a0shmid,\u00a0const\u00a0void\u00a0*shmaddr,\u00a0int\u00a0shmflg);<\/li>\n<\/ol>\n<p>\u53c2\u6570 shmid \u662f shmget() \u7684\u8fd4\u56de\u503c\uff0c\u662f\u4e2a\u6807\u8bc6\u7b26\uff1b<\/p>\n<p>\u53c2\u6570 shmflg \u662f\u5b58\u53d6\u6743\u9650\u6807\u5fd7\uff1b\u5982\u679c\u4e3a 0 \uff0c\u5219\u4e0d\u8bbe\u7f6e\u4efb\u4f55\u9650\u5236\u6743\u9650\u3002\u5728 \u4e2d\u5b9a\u4e49\u4e86\u51e0\u4e2a\u6743\u9650\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">#define\u00a0SHM_RDONLY\u00a0010000\u00a0\/*\u00a0attach\u00a0read-only\u00a0else\u00a0read-write\u00a0*\/<\/li>\n<li>#define\u00a0SHM_RND\u00a0020000\u00a0\/*\u00a0round\u00a0attach\u00a0address\u00a0to\u00a0SHMLBA\u00a0*\/<\/li>\n<li class=\"alt\">#define\u00a0SHM_REMAP\u00a0040000\u00a0\/*\u00a0take-over\u00a0region\u00a0on\u00a0attach\u00a0*\/<\/li>\n<\/ol>\n<p>\u5982\u679c\u6307\u5b9a SHM_RDONLY \uff0c\u90a3\u4e48\u5171\u4eab\u5185\u5b58\u533a\u53ea\u6709\u8bfb\u53d6\u6743\u9650\u3002<\/p>\n<p>\u53c2\u6570 shmaddr \u662f\u5171\u4eab\u5185\u5b58\u7684\u9644\u52a0\u70b9\uff0c\u4e0d\u540c\u7684\u53d6\u503c\u6709\u4e0d\u540c\u7684\u542b\u4e49\uff1a<\/p>\n<p>?\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u7531\u5185\u6838\u9009\u62e9\u4e00\u4e2a\u7a7a\u95f2\u7684\u5185\u5b58\u533a\uff1b\u5982\u679c\u975e\u7a7a\uff0c\u8fd4\u56de\u5730\u5740\u53d6\u51b3\u4e8e\u8c03\u7528\u8005\u662f\u5426\u7ed9 shmflg \u53c2\u6570\u6307\u5b9a SHM_RND \u503c\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\uff0c\u5219\u5171\u4eab\u5185\u5b58\u533a\u9644\u52a0\u5230\u7531 shmaddr \u6307\u5b9a\u7684\u5730\u5740\uff1b\u5426\u5219\u9644\u52a0\u5730\u5740\u4e3a shmaddr \u5411\u4e0b\u820d\u5165\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u4f4e\u7aef\u8fb9\u754c\u5730\u5740\u540e\u7684\u5730\u5740 (SHMLBA \uff0c\u4e00\u4e2a\u5e38\u5740)\u3002<\/p>\n<p>\u00d8\u901a\u5e38\u5c06\u53c2\u6570 shmaddr \u8bbe\u7f6e\u4e3a NULL \u3002<\/p>\n<p>shmat() \u8c03\u7528\u6210\u529f\u540e\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5171\u4eab\u5185\u5b58\u533a\u7684\u6307\u9488\uff0c\u4f7f\u7528\u8be5\u6307\u9488\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u533a\u4e86\uff0c\u5982\u679c\u5931\u8d25\u5219\u8fd4\u56de -1\u3002<\/p>\n<p>\u5176\u6620\u5c04\u5173\u7cfb\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><a href=\"http:\/\/s4.51cto.com\/wyfs01\/M02\/33\/00\/wKioJlKUWEbS_kyKAACf8a9TgX4617.jpg\" target=\"_blank\" rel=\"nofollow noopener\"><img decoding=\"async\" class=\"fit-image\" src=\"http:\/\/s4.51cto.com\/wyfs01\/M02\/33\/00\/wKioJlKUWEbS_kyKAACf8a9TgX4617.jpg\" alt=\"\" width=\"498\" \/><\/a><\/p>\n<p>\u56fe1.1 \u5171\u4eab\u5185\u5b58\u6620\u5c04\u56fe<\/p>\n<p>\u5176\u4e2d\uff0cshmaddr\u8868\u793a\u7684\u662f\u7269\u7406\u5185\u5b58\u7a7a\u95f4\u6620\u5c04\u5230\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u65f6\u5019\uff0c\u865a\u62df\u5185\u5b58\u7a7a\u95f4\u4e2d\u8be5\u5757\u5185\u5b58\u7684\u8d77\u59cb\u5730\u5740\uff0c\u5728\u4f7f\u7528\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u4e00\u822c\u4e0d\u6e05\u695a\u8fdb\u7a0b\u4e2d\u54ea\u4e9b\u5730\u5740\u6ca1\u6709\u88ab\u5360\u7528\uff0c\u6240\u4ee5\u4e0d\u597d\u6307\u5b9a\u7269\u7406\u7a7a\u95f4\u7684\u5185\u5b58\u8981\u6620\u5c04\u5230\u672c\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58\u5730\u5740\uff0c\u4e00\u822c\u4f1a\u8ba9\u5185\u6838\u81ea\u5df1\u6307\u5b9a\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">void\u00a0<span class=\"attribute\">ptr<\/span>\u00a0=\u00a0<span class=\"attribute-value\">shmat<\/span>(shmid,\u00a0NULL,0);<\/li>\n<\/ol>\n<p>\u8fd9\u6837\u6302\u8f7d\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u5982\u679c\u662f\u4e00\u6b21\u8c03\u7528\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u4f46\u662f\u4e00\u4e2a\u8fdb\u7a0b\u662f\u53ef\u4ee5\u5bf9\u540c\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u591a\u6b21 shmat\u8fdb\u884c\u6302\u8f7d\u7684\uff0c\u7269\u7406\u5185\u5b58\u662f\u6307\u5411\u540c\u4e00\u5757\uff0c\u5982\u679cshmaddr\u4e3aNULL\uff0c\u5219\u6bcf\u6b21\u8fd4\u56de\u7684\u7ebf\u6027\u5730\u5740\u7a7a\u95f4\u90fd\u4e0d\u540c\u3002\u800c\u4e14\u6307\u5411\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u7684\u5f15\u7528\u8ba1\u6570\u4f1a\u589e\u52a0\u3002\u4e5f\u5c31\u662f\u8fdb\u7a0b\u591a\u5757\u7ebf\u6027\u7a7a\u95f4\u4f1a\u6307\u5411\u540c\u4e00\u5757\u7269\u7406\u5730\u5740\u3002\u8fd9\u6837\uff0c\u5982\u679c\u4e4b\u524d\u6302\u8f7d\u8fc7\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u7684\u8fdb\u7a0b\u7684\u7ebf\u6027\u5730\u5740\u6ca1\u6709\u88abshmdt\u6389\uff0c\u5373\u7533\u8bf7\u7684\u7ebf\u6027\u5730\u5740\u90fd\u6ca1\u6709\u91ca\u653e\uff0c\u5c31\u4f1a\u4e00\u76f4\u6d88\u8017\u8fdb\u7a0b\u7684\u865a\u62df\u5185\u5b58\u7a7a\u95f4\uff0c\u5f88\u6709\u53ef\u80fd\u4f1a\u6700\u540e\u5bfc\u81f4\u8fdb\u7a0b\u7ebf\u6027\u7a7a\u95f4\u88ab\u4f7f\u7528\u5b8c\u800c\u5bfc\u81f4\u4e0b\u6b21shmat\u6216\u8005\u5176\u4ed6\u64cd\u4f5c\u5931\u8d25\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad\u9700\u8981\u7533\u8bf7\u7684\u5171\u4eab\u5185\u5b58\u6307\u9488\u662f\u5426\u4e3a\u7a7a\u6765\u6807\u8bc6\u662f\u5426\u662f\u7b2c\u4e00\u6b21\u6302\u8f7d\u5171\u4eab\u5185\u5b58\uff0c\u82e5\u662f\u5219\u4f7f\u7528\u8fdb\u884c\u6302\u8f7d\uff0c\u82e5\u4e0d\u662f\u5219\u9000\u51fa\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void*\u00a0ptr\u00a0=\u00a0NULL;\r\n...\r\nif\u00a0(NULL\u00a0!=\u00a0ptr)\r\n    return;\r\nptr\u00a0=\u00a0shmat(shmid,ptr,0666);<\/pre>\n<p>\u9644\uff1a<\/p>\n<p>\u51fd\u6570shmat\u5c06\u6807\u8bc6\u53f7\u4e3ashmid\u5171\u4eab\u5185\u5b58\u6620\u5c04\u5230\u8c03\u7528\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u4e2d\uff0c\u6620\u5c04\u7684\u5730\u5740\u7531\u53c2\u6570shmaddr\u548cshmflg\u5171\u540c\u786e\u5b9a\uff0c\u5176\u51c6\u5219\u4e3a\uff1a<\/p>\n<p>(1) \u5982\u679c\u53c2\u6570shmaddr\u53d6\u503c\u4e3aNULL\uff0c\u7cfb\u7edf\u5c06\u81ea\u52a8\u786e\u5b9a\u5171\u4eab\u5185\u5b58\u94fe\u63a5\u5230\u8fdb\u7a0b\u7a7a\u95f4\u7684\u9996\u5730\u5740\u3002<\/p>\n<p>(2) \u5982\u679c\u53c2\u6570shmaddr\u53d6\u503c\u4e0d\u4e3aNULL\u4e14\u53c2\u6570shmflg\u6ca1\u6709\u6307\u5b9aSHM_RND\u6807\u5fd7\uff0c\u7cfb\u7edf\u5c06\u8fd0\u7528\u5730\u5740shmaddr\u94fe\u63a5\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>(3) \u5982\u679c\u53c2\u6570shmaddr\u53d6\u503c\u4e0d\u4e3aNULL\u4e14\u53c2\u6570shmflg\u6307\u5b9a\u4e86SHM_RND\u6807\u5fd7\u4f4d\uff0c\u7cfb\u7edf\u5c06\u5730\u5740shmaddr\u5bf9\u9f50\u540e\u94fe\u63a5\u5171\u4eab\u5185\u5b58\u3002\u5176\u4e2d\u9009\u9879SHM_RND\u7684\u610f\u601d\u662f\u53d6\u6574\u5bf9\u9f50\uff0c\u5e38\u6570SHMLBA\u4ee3\u8868\u4e86\u4f4e\u8fb9\u754c\u5730\u5740\u7684\u500d\u6570\uff0c\u516c\u5f0f\u201cshmaddr \u2013 (shmaddr % SHMLBA)\u201d\u7684\u610f\u601d\u662f\u5c06\u5730\u5740shmaddr\u79fb\u52a8\u5230\u4f4e\u8fb9\u754c\u5730\u5740\u7684\u6574\u6570\u500d\u4e0a\u3002<\/p>\n<p>Shmget\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u5f53key\u76f8\u540c\u65f6\uff0c\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f1a\u51fa\u9519\uff1f<\/p>\n<p>shmget() \u7528\u6765\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u533a\uff0c\u6216\u8005\u8bbf\u95ee\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5171\u4eab\u5185\u5b58\u533a\u3002\u8be5\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6 linux\/shm.h\u4e2d\uff0c\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">int\u00a0shmget(key_t\u00a0key,\u00a0size_t\u00a0size,\u00a0int\u00a0shmflg);<\/li>\n<\/ol>\n<p>\u53c2\u6570 key\u662f\u7531 ftok() \u5f97\u5230\u7684\u952e\u503c\uff1b<\/p>\n<p>\u53c2\u6570 size \u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u6307\u5b9a\u5185\u5b58\u7684\u5927\u5c0f\uff1b<\/p>\n<p>\u53c2\u6570 shmflg \u662f\u64cd\u4f5c\u6807\u5fd7\u4f4d\uff0c\u5b83\u7684\u4e00\u4e9b\u5b8f\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<p>IPC_CREATE : \u8c03\u7528 shmget \u65f6\uff0c\u7cfb\u7edf\u5c06\u6b64\u503c\u4e0e\u5176\u4ed6\u5171\u4eab\u5185\u5b58\u533a\u7684 key \u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u5b58\u5728\u76f8\u540c\u7684 key \uff0c\u8bf4\u660e\u5171\u4eab\u5185\u5b58\u533a\u5df2\u5b58\u5728\uff0c\u6b64\u65f6\u8fd4\u56de\u8be5\u5171\u4eab\u5185\u5b58\u533a\u7684\u6807\u8bc6\u7b26\uff0c\u5426\u5219\u65b0\u5efa\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u533a\u5e76\u8fd4\u56de\u5176\u6807\u8bc6\u7b26\u3002<\/p>\n<p>IPC_EXCL : \u8be5\u5b8f\u5fc5\u987b\u548c IPC_CREATE \u4e00\u8d77\u4f7f\u7528\uff0c\u5426\u5219\u6ca1\u610f\u4e49\u3002\u5f53 shmflg \u53d6 IPC_CREATE | IPC_EXCL \u65f6\uff0c\u8868\u793a\u5982\u679c\u53d1\u73b0\u5185\u5b58\u533a\u5df2\u7ecf\u5b58\u5728\u5219\u8fd4\u56de -1\uff0c\u9519\u8bef\u4ee3\u7801\u4e3a EEXIST \u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u5f53\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u533a\u65f6\uff0csize \u7684\u503c\u5fc5\u987b\u5927\u4e8e 0 \uff1b\u5982\u679c\u662f\u8bbf\u95ee\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5185\u5b58\u5171\u4eab\u533a\uff0c\u5219\u7f6e size \u4e3a 0 \u3002<\/p>\n<p>\u4e00\u822c\u6211\u4eec\u521b\u5efa\u5171\u4eab\u5185\u5b58\u7684\u65f6\u5019\u4f1a\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u4f7f\u7528shmget\u6765\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">Int\u00a0<span class=\"attribute\">shmid<\/span>\u00a0=\u00a0<span class=\"attribute-value\">shmget<\/span>(key,\u00a0size,\u00a0IPC_CREATE|0666);<\/li>\n<\/ol>\n<p>\u800c\u5728\u53e6\u5916\u7684\u8fdb\u7a0b\u4e2d\uff0c\u4f7f\u7528shmget\u548c\u540c\u6837\u7684key\u6765\u83b7\u53d6\u5230\u8fd9\u4e2a\u5df2\u7ecf\u521b\u5efa\u4e86\u7684\u5171\u4eab\u5185\u5b58,<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">Int\u00a0<span class=\"attribute\">shmid<\/span>\u00a0=\u00a0<span class=\"attribute-value\">shmget<\/span>(key,\u00a0size,\u00a0IPC_CREATE|0666);<\/li>\n<\/ol>\n<p>\u5982\u679c\u521b\u5efa\u8fdb\u7a0b\u548c\u6302\u63a5\u8fdb\u7a0bkey\u76f8\u540c\uff0c\u800c\u5bf9\u5e94\u7684size\u5927\u5c0f\u4e0d\u540c\uff0c\u662f\u5426\u4f1ashmget\u5931\u8d25\uff1f<\/p>\n<p>\u00d8\u00a0\u5df2\u7ecf\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u662f\u53ef\u4ee5\u8c03\u6574\u7684\uff0c\u4f46\u662f\u5df2\u7ecf\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u53ea\u80fd\u8c03\u5c0f\uff0c\u4e0d\u80fd\u8c03\u5927<\/p>\n<p>\u5982\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\"><span class=\"attribute\">shm_id<\/span>\u00a0=\u00a0<span class=\"attribute-value\">shmget<\/span>(key,4194304,IPC_CREAT);<\/li>\n<\/ol>\n<p>\u521b\u5efa\u4e86\u4e00\u4e2a4M\u5927\u5c0f\u7684\u5171\u4eab\u5185\u5b58\uff0c\u5982\u679c\u8fd9\u4e2a\u5171\u4eab\u5185\u5b58\u6ca1\u6709\u5220\u6389\uff0c\u6211\u4eec\u518d\u4f7f\u7528<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\"><span class=\"attribute\">shm_id<\/span>\u00a0=\u00a0<span class=\"attribute-value\">shmget<\/span>(key,10485760,IPC_CREAT);<\/li>\n<\/ol>\n<p>\u6765\u521b\u5efa\u4e00\u4e2a10M\u5927\u5c0f\u7684\u5171\u4eab\u5185\u5b58\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u4f1a\u6709\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">shmget\u00a0error:\u00a0Invalid\u00a0argument<\/li>\n<\/ol>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u4f7f\u7528\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\"><span class=\"attribute\">shm_id<\/span>\u00a0=\u00a0<span class=\"attribute-value\">shmget<\/span>(key,3145728,IPC_CREAT);<\/li>\n<\/ol>\n<p>\u6765\u521b\u5efa\u4e00\u4e2a3M\u5927\u5c0f\u7684\u5171\u4eab\u5185\u5b58\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u4f1a\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u53ea\u662f\u5171\u4eab\u5185\u5b58\u5927\u5c0f\u4f1a\u88ab\u4fee\u6539\u4e3a3145728\uff0c\u8fd9\u4e5f\u8bf4\u660e\uff0c\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u7684\u65f6\u5019\uff0c\u662f\u7528key\u6765\u4f5c\u4e3a\u5171\u4eab\u5185\u5b58\u7684\u552f\u4e00\u6807\u8bc6\u7684\uff0c\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u4e0d\u80fd\u533a\u5206\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u8fd9\u6837\u4f1a\u5bfc\u81f4\u4ec0\u4e48\u95ee\u9898\uff1f<\/p>\n<p>\u5f53\u591a\u4e2a\u8fdb\u7a0b\u90fd\u80fd\u521b\u5efa\u5171\u4eab\u5185\u5b58\u7684\u65f6\u5019\uff0c\u5982\u679ckey\u51fa\u73b0\u76f8\u540c\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u4e00\u4e2a\u8fdb\u7a0b\u9700\u8981\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u8981\u6bd4\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u8981\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u5c0f\uff0c\u5171\u4eab\u5185\u5b58\u5927\u7684\u8fdb\u7a0b\u5148\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u5171\u4eab\u5185\u5b58\u5c0f\u7684\u8fdb\u7a0b\u540e\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u5c0f\u5171\u4eab\u5185\u5b58\u7684\u8fdb\u7a0b\u5c31\u4f1a\u83b7\u53d6\u5230\u5927\u7684\u5171\u4eab\u5185\u5b58\u8fdb\u7a0b\u7684\u5171\u4eab\u5185\u5b58\uff0c \u5e76\u4fee\u6539\u5176\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u548c\u5185\u5bb9\uff08\u7559\u610f\u4e0b\u9762\u7684\u8bc4\u8bba\u8865\u5145\uff09\uff0c\u4ece\u800c\u53ef\u80fd\u5bfc\u81f4\u5927\u7684\u5171\u4eab\u5185\u5b58\u8fdb\u7a0b\u5d29\u6e83\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p>\u65b9\u6cd5\u4e00\uff1a<\/p>\n<p>\u5728\u6240\u6709\u7684\u5171\u4eab\u5185\u5b58\u521b\u5efa\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u6392\u4ed6\u6027\u521b\u5efa\uff0c\u5373\u4f7f\u7528IPC_EXCL\u6807\u8bb0\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">Shmget(key,\u00a0size,IPC_CREATE|IPC_EXCL);<\/li>\n<\/ol>\n<p>\u5728\u5171\u4eab\u5185\u5b58\u6302\u63a5\u7684\u65f6\u5019\uff0c\u5148\u4f7f\u7528\u6392\u4ed6\u6027\u521b\u5efa\u5224\u65ad\u5171\u4eab\u5185\u5b58\u662f\u5426\u5df2\u7ecf\u521b\u5efa\uff0c\u5982\u679c\u8fd8\u6ca1\u521b\u5efa\u5219\u8fdb\u884c\u51fa\u9519\u5904\u7406\uff0c\u82e5\u5df2\u7ecf\u521b\u5efa\uff0c\u5219\u6302\u63a5\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Shmid\u00a0=\u00a0Shmget(key,\u00a0size,IPC_CREATE|IPC_EXCL);\r\nIf\u00a0(-1\u00a0!=\u00a0shmid)\r\n{\r\n  Printf(\"error\");\r\n}\r\nShmid\u00a0=\u00a0Shmget(key,\u00a0size,IPC_CREATE);<\/pre>\n<p>\u65b9\u6cd5\u4e8c\uff1a<\/p>\n<p>\u867d\u7136\u90fd\u5e0c\u671b\u81ea\u5df1\u7684\u7a0b\u5e8f\u80fd\u548c\u5176\u4ed6\u7684\u7a0b\u5e8f\u9884\u5148\u7ea6\u5b9a\u4e00\u4e2a\u552f\u4e00\u7684\u952e\u503c\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u603b\u53ef\u80fd\u7684\u6210\u884c\u7684\uff0c\u56e0\u4e3a\u81ea\u5df1\u7684\u7a0b\u5e8f\u65e0\u6cd5\u4e3a\u4e00\u5757\u5171\u4eab\u5185\u5b58\u9009\u62e9\u4e00\u4e2a\u952e\u503c\u3002\u56e0\u6b64\uff0c\u5728\u6b64\u628akey\u8bbe\u4e3aIPC_PRIVATE\uff0c\u8fd9\u6837\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c06\u5ffd\u7565\u952e\uff0c\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\uff0c\u6307\u5b9a\u4e00\u4e2a\u952e\u503c\uff0c\u7136\u540e\u8fd4\u56de\u8fd9\u5757\u5171\u4eab\u5185\u5b58IPC\u6807\u8bc6\u7b26ID\u3002\u800c\u5c06\u8fd9\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58\u7684\u6807\u8bc6\u7b26ID\u544a\u8bc9\u5176\u4ed6\u8fdb\u7a0b\u53ef\u4ee5\u5728\u5efa\u7acb\u5171\u4eab\u5185\u5b58\u540e\u901a\u8fc7\u6d3e\u751f\u5b50\u8fdb\u7a0b\uff0c\u6216\u5199\u5165\u6587\u4ef6\u6216\u7ba1\u9053\u6765\u5b9e\u73b0\uff0c\u5373\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f7f\u7528key\u6765\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u7531\u64cd\u4f5c\u7cfb\u7edf\u6765\u4fdd\u8bc1\u552f\u4e00\u6027\u3002<\/p>\n<p>ftok\u662f\u5426\u4e00\u5b9a\u4f1a\u4ea7\u751f\u552f\u4e00\u7684key\u503c\uff1f<\/p>\n<p>\u7cfb\u7edf\u5efa\u7acbIPC\u901a\u8baf\uff08\u5982\u6d88\u606f\u961f\u5217\u3001\u5171\u4eab\u5185\u5b58\u65f6\uff09\u5fc5\u987b\u6307\u5b9a\u4e00\u4e2aID\u503c\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8be5id\u503c\u901a\u8fc7ftok\u51fd\u6570\u5f97\u5230\u3002<\/p>\n<p>ftok\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">key_t\u00a0ftok(\u00a0char\u00a0*\u00a0pathname,\u00a0int\u00a0proj_id)<\/li>\n<\/ol>\n<p>pathname\u5c31\u65f6\u4f60\u6307\u5b9a\u7684\u6587\u4ef6\u540d\uff0cproj_id\u662f\u5b50\u5e8f\u53f7\u3002<\/p>\n<p>\u5728\u4e00\u822c\u7684UNIX\u5b9e\u73b0\u4e2d\uff0c\u662f\u5c06\u6587\u4ef6\u7684\u7d22\u5f15\u8282\u70b9\u53f7\u53d6\u51fa\uff0c\u524d\u9762\u52a0\u4e0a\u5b50\u5e8f\u53f7\u5f97\u5230key_t\u7684\u8fd4\u56de\u503c\u3002\u5982\u6307\u5b9a\u6587\u4ef6\u7684\u7d22\u5f15\u8282\u70b9\u53f7\u4e3a65538\uff0c\u6362\u7b97\u621016\u8fdb\u5236\u4e3a0\u00d7010002\uff0c\u800c\u4f60\u6307\u5b9a\u7684proj_id\u503c\u4e3a38\uff0c\u6362\u7b97\u621016\u8fdb\u5236\u4e3a0\u00d726\uff0c\u5219\u6700\u540e\u7684key_t\u8fd4\u56de\u503c\u4e3a0\u00d726010002\u3002<\/p>\n<p>\u67e5\u8be2\u6587\u4ef6\u7d22\u5f15\u8282\u70b9\u53f7\u7684\u65b9\u6cd5\u662f\uff1a ls -i<\/p>\n<p>\u4f46\u5f53\u5220\u9664\u91cd\u5efa\u6587\u4ef6\u540e\uff0c\u7d22\u5f15\u8282\u70b9\u53f7\u7531\u64cd\u4f5c\u7cfb\u7edf\u6839\u636e\u5f53\u65f6\u6587\u4ef6\u7cfb\u7edf\u7684\u4f7f\u7528\u60c5\u51b5\u5206\u914d\uff0c\u56e0\u6b64\u4e0e\u539f\u6765\u4e0d\u540c\uff0c\u6240\u4ee5\u5f97\u5230\u7684\u7d22\u5f15\u8282\u70b9\u53f7\u4e5f\u4e0d\u540c\u3002<\/p>\n<p>\u6839\u636epathname\u6307\u5b9a\u7684\u6587\u4ef6\uff08\u6216\u76ee\u5f55\uff09\u540d\u79f0\uff0c\u4ee5\u53caproj_id\u53c2\u6570\u6307\u5b9a\u7684\u6570\u5b57\uff0cftok\u51fd\u6570\u4e3aIPC\u5bf9\u8c61\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u6027\u7684\u952e\u503c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f88\u5bb9\u6613\u4ea7\u751f\u7684\u4e00\u4e2a\u7406\u89e3\u662f\uff0c\u5728proj_id\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\uff0c\u53ea\u8981\u6587\u4ef6\uff08\u6216\u76ee\u5f55\uff09\u540d\u79f0\u4e0d\u53d8\uff0c\u5c31\u53ef\u4ee5\u786e\u4fddftok\u8fd4\u56de\u59cb\u7ec8\u4e00\u81f4\u7684\u952e\u503c\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u7406\u89e3\u5e76\u975e\u5b8c\u5168\u6b63\u786e\uff0c\u6709\u53ef\u80fd\u7ed9\u5e94\u7528\u5f00\u53d1\u57cb\u4e0b\u5f88\u9690\u6666\u7684\u9677\u9631\u3002\u56e0\u4e3aftok\u7684\u5b9e\u73b0\u5b58\u5728\u8fd9\u6837\u7684\u98ce\u9669\uff0c\u5373\u5728\u8bbf\u95ee\u540c\u4e00\u5171\u4eab\u5185\u5b58\u7684\u591a\u4e2a\u8fdb\u7a0b\u5148\u540e\u8c03\u7528ftok\u51fd\u6570\u7684\u65f6\u95f4\u6bb5\u4e2d\uff0c\u5982\u679cpathname\u6307\u5b9a\u7684\u6587\u4ef6\uff08\u6216\u76ee\u5f55\uff09\u88ab\u5220\u9664\u4e14\u91cd\u65b0\u521b\u5efa\uff0c\u5219\u6587\u4ef6\u7cfb\u7edf\u4f1a\u8d4b\u4e88\u8fd9\u4e2a\u540c\u540d\u6587\u4ef6\uff08\u6216\u76ee\u5f55\uff09\u65b0\u7684i\u8282\u70b9\u4fe1\u606f\uff0c\u4e8e\u662f\u8fd9\u4e9b\u8fdb\u7a0b\u6240\u8c03\u7528\u7684ftok\u867d\u7136\u90fd\u80fd\u6b63\u5e38\u8fd4\u56de\uff0c\u4f46\u5f97\u5230\u7684\u952e\u503c\u5374\u5e76\u4e0d\u80fd\u4fdd\u8bc1\u76f8\u540c\u3002\u7531\u6b64\u53ef\u80fd\u9020\u6210\u7684\u540e\u679c\u662f\uff0c\u539f\u672c\u8fd9\u4e9b\u8fdb\u7a0b\u610f\u56fe\u8bbf\u95ee\u4e00\u4e2a\u76f8\u540c\u7684\u5171\u4eab\u5185\u5b58\u5bf9\u8c61\uff0c\u7136\u800c\u7531\u4e8e\u5b83\u4eec\u5404\u81ea\u5f97\u5230\u7684\u952e\u503c\u4e0d\u540c\uff0c\u5b9e\u9645\u4e0a\u8fdb\u7a0b\u6307\u5411\u7684\u5171\u4eab\u5185\u5b58\u4e0d\u518d\u4e00\u81f4\uff1b\u5982\u679c\u8fd9\u4e9b\u5171\u4eab\u5185\u5b58\u90fd\u5f97\u5230\u521b\u5efa\uff0c\u5219\u5728\u6574\u4e2a\u5e94\u7528\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\u8868\u9762\u4e0a\u4e0d\u4f1a\u62a5\u51fa\u4efb\u4f55\u9519\u8bef\uff0c\u7136\u800c\u901a\u8fc7\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u5bf9\u8c61\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u7684\u76ee\u7684\u5c06\u65e0\u6cd5\u5b9e\u73b0\u3002<\/p>\n<p>\u6240\u4ee5\u5982\u679c\u8981\u786e\u4fddkey_t\u503c\u4e0d\u53d8\uff0c \u8981\u4e48\u786e\u4fddftok\u7684\u6587\u4ef6\u4e0d\u88ab\u5220\u9664\uff0c\u8981\u4e48\u4e0d\u7528ftok\uff0c\u6307\u5b9a\u4e00\u4e2a\u56fa\u5b9a\u7684key_t\u503c\u3002<\/p>\n<p>\u5982\u679c\u5b58\u5728\u751f\u6210key_t\u503c\u7684\u6587\u4ef6\u88ab\u5220\u9664\u8fc7\uff0c\u5219\u5f88\u6709\u53ef\u80fd\u81ea\u5df1\u73b0\u5728\u4f7f\u7528\u7684\u5171\u4eab\u5185\u5b58key_t\u503c\u4f1a\u548c\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u7684key_t\u503c\u51b2\u7a81\uff0c\u5982\u4e0b\u9762\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n<p><a href=\"http:\/\/s2.51cto.com\/wyfs01\/M00\/33\/04\/wKioJlKUWkSB9XL8AACeOtS_IEI037.jpg\" target=\"_blank\" rel=\"nofollow noopener\"><img decoding=\"async\" class=\"fit-image\" src=\"http:\/\/s2.51cto.com\/wyfs01\/M00\/33\/04\/wKioJlKUWkSB9XL8AACeOtS_IEI037.jpg\" alt=\"\" width=\"498\" \/><\/a><\/p>\n<p>\u8fdb\u7a0b1\u4f7f\u7528\u6587\u4ef61\u6765ftok\u751f\u6210\u4e86key10000\uff0c\u8fdb\u7a0b2\u4f7f\u7528\u6587\u4ef62\u6765ftok\u751f\u6210\u4e86key 11111\uff0c\u6b64\u65f6\u5982\u679c\u8fdb\u7a0b1\u548c\u8fdb\u7a0b2\u90fd\u9700\u8981\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u7684\u5185\u5bb9\u66f4\u65b0\u5230\u5171\u4eab\u5185\u5b58\uff0c\u6b64\u65f6\u8fdb\u7a0b1\u548c2\u90fd\u9700\u8981\u5148\u4e0b\u6587\u4ef6\uff0c\u518d\u5220\u6389\u4e4b\u524d\u7684\u5171\u4eab\u5185\u5b58\uff0c\u518d\u4f7f\u7528ftok\u751f\u6210\u65b0\u7684key\uff0c\u518d\u7528\u8fd9\u4e2akey\u53bb\u7533\u8bf7\u65b0\u7684\u5171\u4eab\u5185\u5b58\u6765\u88c5\u8f7d\u65b0\u7684\u95ee\u9898\uff0c\u4f46\u662f\u53ef\u80fd\u6587\u4ef62\u6bd4\u8f83\u5927\uff0c\u4e0b\u8f7d\u6162\uff0c\u800c\u6587\u4ef61\u6bd4\u8f83\u5c0f\uff0c\u4e0b\u8f7d\u6bd4\u8f83\u6162\uff0c\u7531\u4e8e\u6587\u4ef61\u548c\u6587\u4ef62\u90fd\u88ab\u4fee\u6539\uff0c\u6b64\u65f6\u6587\u4ef61\u6240\u5360\u7528\u7684\u6587\u4ef6\u8282\u70b9\u53f7\u53ef\u80fd\u662f\u6587\u4ef62\u4e4b\u524d\u6240\u5360\u7528\u7684\uff0c\u6b64\u65f6\u5982\u679c\u4e0b\u8f7d\u7684\u6587\u4ef61\u7684ftok\u751f\u6210\u7684key\u4e3a11111\u7684\u8bdd\uff0c\u5c31\u4f1a\u548c\u6b64\u65f6\u8fd8\u6ca1\u6709\u662f\u542611111\u8fd9\u4e2akey\u7684\u8fdb\u7a0b2\u7684\u5171\u4eab\u5185\u5b58\u51b2\u7a81\uff0c\u5bfc\u81f4\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<p>\u65b9\u6cd5\u4e00\uff1a<\/p>\n<p>\u5728\u6709\u4e0b\u8f7d\u6587\u4ef6\u64cd\u4f5c\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5bf9\u4e0b\u8f7d\u7684\u6587\u4ef6\u4f7f\u7528ftok\u83b7\u53d6key\u7684\u65f6\u5019\uff0c\u9700\u8981\u8fdb\u884c\u51b2\u7a81\u907f\u514d\u7684\u63aa\u65bd\uff0c\u5982\u4f7f\u7528\u72ec\u5360\u7684\u65b9\u5f0f\u83b7\u53d6\u5171\u4eab\u5185\u5b58\uff0c\u5982\u679c\u4e0d\u6210\u529f\uff0c\u5219\u5bf9key\u8fdb\u884c\u52a0\u4e00\u64cd\u4f5c\uff0c\u518d\u8fdb\u884c\u83b7\u53d6\u5171\u4eab\u5185\u5b58\uff0c\u4e00\u76f4\u5230\u4e0d\u4f1a\u4ea7\u751f\u51b2\u7a81\u4e3a\u6b62\u3002<\/p>\n<p>\u65b9\u6cd5\u4e8c\uff1a<\/p>\n<p>\u4e0b\u8f7d\u6587\u4ef6\u4e4b\u524d\uff0c\u5c06\u4e4b\u524d\u7684\u6587\u4ef6\u8fdb\u884cmv\u4e00\u4e0b\uff0c\u5148\u201c\u5360\u201d\u7740\u8fd9\u4e2a\u6587\u4ef6\u8282\u70b9\u53f7\uff0c\u9632\u6b62\u5176\u4ed6\u5171\u4eab\u5185\u5b58\u7533\u8bf7key\u7684\u65f6\u5019\u83b7\u53d6\u5230\u3002<\/p>\n<p>\u53e6\u5916\uff1a<\/p>\n<p>\u521b\u5efa\u8fdb\u7a0b\u5728\u901a\u77e5\u5176\u4ed6\u8fdb\u7a0b\u6302\u63a5\u7684\u65f6\u5019\uff0c\u5efa\u8bae\u4e0d\u4f7f\u7528ftok\u65b9\u5f0f\u6765\u83b7\u53d6Key\uff0c\u800c\u4f7f\u7528\u6587\u4ef6\u6216\u8005\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u65b9\u5f0f\u544a\u77e5\u3002<\/p>\n<p>\u5171\u4eab\u5185\u5b58\u5220\u9664\u7684\u9677\u9631\uff1f<\/p>\n<p>\u5f53\u8fdb\u7a0b\u7ed3\u675f\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u533a\u65f6\uff0c\u8981\u901a\u8fc7\u51fd\u6570 shmdt \u65ad\u5f00\u4e0e\u5171\u4eab\u5185\u5b58\u533a\u7684\u8fde\u63a5\u3002\u8be5\u51fd\u6570\u58f0\u660e\u5728 sys\/shm.h \u4e2d\uff0c\u5176\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">int\u00a0shmdt(const\u00a0void\u00a0*shmaddr);<\/li>\n<\/ol>\n<p>\u53c2\u6570 shmaddr \u662f shmat \u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u8fdb\u7a0b\u8131\u79bb\u5171\u4eab\u5185\u5b58\u533a\u540e\uff0c\u6570\u636e\u7ed3\u6784 shmid_ds \u4e2d\u7684 shm_nattch \u5c31\u4f1a\u51cf 1 \u3002\u4f46\u662f\u5171\u4eab\u6bb5\u5185\u5b58\u4f9d\u7136\u5b58\u5728\uff0c\u53ea\u6709 shm_attch \u4e3a 0 \u540e\uff0c\u5373\u6ca1\u6709\u4efb\u4f55\u8fdb\u7a0b\u518d\u4f7f\u7528\u8be5\u5171\u4eab\u5185\u5b58\u533a\uff0c\u5171\u4eab\u5185\u5b58\u533a\u624d\u5728\u5185\u6838\u4e2d\u88ab\u5220\u9664\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u5b83\u6240\u9644\u52a0\u7684\u5171\u4eab\u5185\u5b58\u533a\u90fd\u4f1a\u81ea\u52a8\u8131\u79bb\u3002<\/p>\n<p>\u6211\u4eec\u901a\u8fc7\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">int\u00a0shmctl(\u00a0int\u00a0shmid\u00a0,\u00a0int\u00a0cmd\u00a0,\u00a0struct\u00a0shmid_ds\u00a0*buf\u00a0);<\/li>\n<\/ol>\n<p>\u6765\u5220\u9664\u5df2\u7ecf\u5b58\u5728\u7684\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0cshmid\uff0c\u662f\u7531shmget\u6240\u8fd4\u56de\u7684\u6807\u8bb0\u7b26\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0ccmd\uff0c\u662f\u8981\u6267\u884c\u7684\u52a8\u4f5c\u3002\u4ed6\u53ef\u4ee5\u6709\u4e09\u4e2a\u503c\uff1a<\/p>\n<p>\u547d\u4ee4 \u63cf\u8ff0<\/p>\n<ul>\n<li>IPC_STAT \u8bbe\u7f6eshmid_ds\u7ed3\u6784\u4e2d\u7684\u6570\u636e\u53cd\u5c04\u4e0e\u5171\u4eab\u5185\u5b58\u76f8\u5173\u8054\u7684\u503c\u3002<\/li>\n<li>IPC_SET \u5982\u679c\u8fdb\u7a0b\u6709\u76f8\u5e94\u7684\u6743\u9650\uff0c\u5c06\u4e0e\u5171\u4eab\u5185\u5b58\u76f8\u5173\u8054\u7684\u503c\u8bbe\u7f6e\u4e3ashmid_ds\u6570\u636e\u7ed3\u6784\u4e2d\u6240\u63d0\u4f9b\u7684\u503c\u3002<\/li>\n<li>IPC_RMID \u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5\u3002<\/li>\n<\/ul>\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0cbuf\uff0c\u662f\u4e00\u4e2a\u6307\u5411\u5305\u542b\u5171\u4eab\u5185\u5b58\u6a21\u5f0f\u4e0e\u6743\u9650\u7684\u7ed3\u6784\u7684\u6307\u9488\uff0c\u5220\u9664\u7684\u65f6\u5019\u53ef\u4ee5\u9ed8\u8ba4\u4e3a0\u3002<\/p>\n<p>\u5982\u679c\u5171\u4eab\u5185\u5b58\u5df2\u7ecf\u4e0e\u6240\u6709\u8bbf\u95ee\u5b83\u7684\u8fdb\u7a0b\u65ad\u5f00\u4e86\u8fde\u63a5\uff0c\u5219\u8c03\u7528IPC_RMID\u5b50\u547d\u4ee4\u540e\uff0c\u7cfb\u7edf\u5c06\u7acb\u5373\u5220\u9664\u5171\u4eab\u5185\u5b58\u7684\u6807\u8bc6\u7b26\uff0c\u5e76\u5220\u9664\u8be5\u5171\u4eab\u5185\u5b58\u533a\uff0c\u4ee5\u53ca\u6240\u6709\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784\uff1b<\/p>\n<p>\u5982\u679c\u4ecd\u6709\u522b\u7684\u8fdb\u7a0b\u4e0e\u8be5\u5171\u4eab\u5185\u5b58\u4fdd\u6301\u8fde\u63a5\uff0c\u5219\u8c03\u7528IPC_RMID\u5b50\u547d\u4ee4\u540e\uff0c\u8be5\u5171\u4eab\u5185\u5b58\u5e76\u4e0d\u4f1a\u88ab\u7acb\u5373\u4ece\u7cfb\u7edf\u4e2d\u5220\u9664\uff0c\u800c\u662f\u88ab\u8bbe\u7f6e\u4e3aIPC_PRIVATE\u72b6\u6001\uff0c\u5e76\u88ab\u6807\u8bb0\u4e3a\u201d\u5df2\u88ab\u5220\u9664\u201d\uff08\u4f7f\u7528ipcs\u547d\u4ee4\u53ef\u4ee5\u770b\u5230dest\u5b57\u6bb5\uff09\uff1b\u76f4\u5230\u5df2\u6709\u8fde\u63a5\u5168\u90e8\u65ad\u5f00\uff0c\u8be5\u5171\u4eab\u5185\u5b58\u624d\u4f1a\u6700\u7ec8\u4ece\u7cfb\u7edf\u4e2d\u6d88\u5931\u3002<\/p>\n<p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1a\u4e00\u65e6\u901a\u8fc7shmctl\u5bf9\u5171\u4eab\u5185\u5b58\u8fdb\u884c\u4e86\u5220\u9664\u64cd\u4f5c\uff0c\u5219\u8be5\u5171\u4eab\u5185\u5b58\u5c06\u4e0d\u80fd\u518d\u63a5\u53d7\u4efb\u4f55\u65b0\u7684\u8fde\u63a5\uff0c\u5373\u4f7f\u5b83\u4f9d\u7136\u5b58\u5728\u4e8e\u7cfb\u7edf\u4e2d\uff01\u6240\u4ee5\uff0c\u53ef\u4ee5\u786e\u77e5\uff0c \u5728\u5bf9\u5171\u4eab\u5185\u5b58\u5220\u9664\u4e4b\u540e\u4e0d\u53ef\u80fd\u518d\u6709\u65b0\u7684\u8fde\u63a5\uff0c\u5219\u6267\u884c\u5220\u9664\u64cd\u4f5c\u662f\u5b89\u5168\u7684\uff1b\u5426\u5219\uff0c\u5728\u5220\u9664\u64cd\u4f5c\u4e4b\u540e\u5982\u4ecd\u6709\u65b0\u7684\u8fde\u63a5\u53d1\u751f\uff0c\u5219\u8fd9\u4e9b\u8fde\u63a5\u90fd\u5c06\u53ef\u80fd\u5931\u8d25\uff01<\/p>\n<p>Shmdt\u548cshmctl\u7684\u533a\u522b\uff1a<\/p>\n<p>Shmdt \u662f\u5c06\u5171\u4eab\u5185\u5b58\u4ece\u8fdb\u7a0b\u7a7a\u95f4detach\u51fa\u6765\uff0c\u4f7f\u8fdb\u7a0b\u4e2d\u7684shmid\u65e0\u6548\u5316\uff0c\u4e0d\u53ef\u4ee5\u4f7f\u7528\u3002\u4f46\u662f\u4fdd\u7559\u7a7a\u95f4\u3002<\/p>\n<p>\u800cshmctl(sid,IPC_RMID,0)\u5219\u662f\u5220\u9664\u5171\u4eab\u5185\u5b58\uff0c\u5f7b\u5e95\u4e0d\u53ef\u7528\uff0c\u91ca\u653e\u7a7a\u95f4\u3002<\/p>\n<p>\u8f6c\u81eahttp:\/\/os.51cto.com\/art\/201311\/418977.htm<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u6240\u8c13\u5171\u4eab\u5185\u5b58\u5c31\u662f\u4f7f\u5f97\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u540c\u4e00<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119,101],"class_list":["post-1866","post","type-post","status-publish","format-standard","hentry","tag-03-","tag-101"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1866"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1866\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}