{"id":1248,"date":"2010-08-29T00:10:22","date_gmt":"2010-08-28T16:10:22","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1248"},"modified":"2010-08-30T12:01:58","modified_gmt":"2010-08-30T04:01:58","slug":"%e7%94%9f%e4%ba%a7%e8%80%85%e4%b8%8e%e6%b6%88%e8%b4%b9%e8%80%85%e7%9a%84%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1248","title":{"rendered":"\u751f\u4ea7\u8005\u4e0e\u6d88\u8d39\u8005\u6a21\u578b\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>\u5b66\u4e60\u4e86\u4fe1\u53f7\u91cf\u4ee5\u53ca\u5171\u4eab\u5185\u5b58\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9e\u73b0\u8fdb\u7a0b\u7684\u540c\u6b65\u4e0e\u4e92\u65a5\u4e86\u3002\u8bf4\u5230\u8fd9\u91cc\uff0c\u6700\u7ecf\u5178\u7684\u4f8b\u5b50\u83ab\u8fc7\u4e8e\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\u3002\u4e0b\u9762\u5c31\u548c\u5927\u5bb6\u4e00\u8d77\u5206\u6790\uff0c\u5982\u4f55\u4e00\u6b65\u6b65\u5b9e\u73b0\u8fd9\u4e2a\u7ecf\u5178\u6a21\u578b\u3002\u5b8c\u6574\u4ee3\u7801\u53ef\u4ee5\u5728\u8fd9\u91cc<a href=\"http:\/\/download.csdn.net\/source\/2656864\" target=\"_blank\">\u4e0b\u8f7d<\/a>\u3002<\/p>\n<p>\u4e0b\u9762\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u7684\u662f<strong>\u591a\u4e2a\u751f\u4ea7\u8005\u548c\u591a\u4e2a\u6d88\u8d39\u8005\u5bf9N\u4e2a\u7f13\u51b2\u533a\uff08N\u4e2a\u8d27\u67b6\uff09\u8fdb\u884c\u8bbf\u95ee<\/strong>\u7684\u4f8b\u5b50\u3002\u73b0\u5728\u5148\u60f3\u60f3\u6211\u4eec\u4ee5\u524d\u7684\u4f2a\u4ee3\u7801\u662f\u600e\u4e48\u5199\u7684\uff1f\u662f\u4e0d\u662f\u8fd9\u6837\uff1a<\/p>\n<pre class=\"brush:c\">\/\/\u751f\u4ea7\u8005\uff1a\r\n\twhile(1)\r\n\t{\r\n\t\tp(semid,1);\r\n\t\tsleep(3);\r\n\t\tp(semid,0);\r\n\t\t\/\/producer is producing a product\r\n\t\tgoods=rand()%10;\/\/product a goods\r\n\t\tshmaddr[indexaddr[0]]=goods;\/\/The goods is placed on a shelf\r\n\t\tprintf(\"producer:%d produces a product[%d]:%d\\n\",getpid(),indexaddr[0],goods);\r\n\t\tindexaddr[0]=(indexaddr[0]+1)%10;\r\n\t\tv(semid,0);\r\n\t\tsleep(3);\r\n\t\tv(semid,2);\r\n\t}\r\n\/\/\u6d88\u8d39\u8005\uff1a\r\n\twhile(1)\r\n\t{\r\n\t\tp(semid,2);\r\n\t\tsleep(1);\r\n\t\tp(semid,0);\r\n\t\t\/\/consumer is consuming a product\r\n\t\tgoods=shmaddr[indexaddr[1]];\/\/The goods on the shelf is taken down\r\n\t\tprintf(\"consumer:%d consumes a product[%d]:%d\\n\",getpid(),indexaddr[1],goods);\r\n\t\tindexaddr[1]=(indexaddr[1]+1)%num;\r\n\t\tv(semid,0);\r\n\t\tsleep(1);\r\n\t\tv(semid,1);\r\n\t}<\/pre>\n<p>\u53ef\u80fd\u4e0a\u9762\u7684\u4ee3\u7801\u4f60\u6709\u4e9b\u773c\u719f\uff0c\u53c8\u6709\u4e9b\u56f0\u60d1\uff0c\u56e0\u4e3a\u5b83\u548c\u8bfe\u672c\u4e0a\u7684\u4ee3\u7801\u4e0d\u5b8c\u5168\u4e00\u6837\uff0c\u5176\u5b9e\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u662f\u4f2a\u4ee3\u7801\u7684linuxC\u8bed\u8a00\u5177\u4f53\u5b9e\u73b0\u3002\u6211\u4eec\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6162\u6162\u5bfb\u627e\u4f2a\u4ee3\u7801\u7684\u8e2a\u8ff9\uff1ap(semid,0)\u548cv(semid,0)\u7684\u4f5c\u7528\u662f\u8ba9\u8fdb\u7a0b\u4e92\u65a5\u8bbf\u95ee\u4e34\u754c\u533a\u3002\u4e34\u754c\u533a\u4e2d\u5305\u542b\u7684\u6570\u636eindexaddr[0]\uff0cindexaddr[1]\uff0c\u4ee5\u53cashmaddr\u6570\u7ec4\u5206\u522b\u5bf9\u5e94\u4f2a\u4ee3\u7801\u4e2d\u7684in\uff0cout\uff0cbuffer\u3002p(semid,1)\u548cv(semid,2)\u4ee5\u53cap(semid,2)\u548cv(semid,1)\u5b9e\u73b0\u7684\u662f\u540c\u6b65\u4f5c\u7528\u3002<\/p>\n<p>\u5e76\u4e14\uff0c\u5728\u751f\u4ea7\u8005\u4e2d\uff0c\u751f\u4ea7\u8005\u751f\u4ea7\u4e86\u4e00\u4e2a\u8d27\u7269\uff08goods=rand()%10;\uff09\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u8d27\u7269\u653e\u4e0a\u8d27\u67b6(shmaddr[indexaddr[0]]=goods;)\u3002\u5728\u6d88\u8d39\u8005\u4e2d\uff0c\u6d88\u8d39\u548c\u4ece\u8d27\u67b6\u4e0a\u53d6\u4e0b\u8d27\u7269\uff08goods=shmaddr[indexaddr[1]];\uff09\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u73b0\u5728\u518d\u770b\u4e00\u8fb9\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u60f3\u4f60\u7684\u601d\u8def\u5c31\u6e05\u6670\u4e86\u3002<\/p>\n<p>\u4e86\u89e3\u4e86\u6838\u5fc3\u4ee3\u7801\uff0c\u5e76\u4e0d\u80fd\u7b97\u5c31\u5b8c\u6210\u4e86\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\uff0c\u56e0\u4e3a\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6838\u5fc3\u4ee3\u7801\u524d\u8fd8\u5f97\u505a\u4e00\u4e9b\u4e9b\u51c6\u5907\u5de5\u4f5c\uff0c\u5177\u4f53\u8981\u51c6\u5907\u4e9b\u4ec0\u4e48\uff0c\u6211\u4eec\u5177\u4f53\u6765\u5206\u6790\u3002<\/p>\n<p>\u9996\u5148\u7533\u8bf7\u4e00\u5757\u5171\u4eab\u5185\u5b58\uff0c\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u7528\u4e8e\u5b58\u653e\u751f\u4ea7\u8005\u6240\u751f\u4ea7\u7684\u8d27\u7269\u3002\u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u5927\u5c0f\u4e3a10\u5b57\u8282\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff0c\u6bcf\u4e2a\u751f\u4ea7\u7740\u6216\u6d88\u8d39\u8005\u8fd0\u884c\u540e\uff0c\u90fd\u8981\u53bb\u8bd5\u7740\u5206\u914d\u8fd9\u6837\u7684\u4e00\u5757\u5171\u4eab\u5185\u5b58\u3002\u5982\u679c\u5728\u5f53\u524d\u8fdb\u7a0b\u8fd0\u884c\u524d\u5df2\u7ecf\u6709\u67d0\u4e2a\u8fdb\u7a0b\u5df2\u7ecf\u521b\u5efa\u4e86\u8fd9\u5757\u5171\u4eab\u5185\u5b58\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8fdb\u7a0b\u5c31\u4e0d\u518d\u521b\u5efa\uff08\u6b64\u65f6createshm\u4f1a\u8fd4\u56de-1\u5e76\u4e14\u9519\u8bef\u4ee3\u7801\u4e3aEEXIST\uff09\uff0c\u53ea\u662f\u6253\u5f00\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u3002\u521b\u5efa\u540e\uff0c\u518d\u5c06\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u6dfb\u52a0\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u3002<\/p>\n<pre class=\"brush:c\">\tnum=10;\r\n\t\/\/create a shared memory as goods buffer\r\n\tif((shmid_goods=createshm(\".\",'s',num))==-1)\r\n\t{\r\n\t\tif(errno==EEXIST)\r\n\t\t{\r\n\t\t\tif((shmid_goods=openshm(\".\",'s'))==-1)\r\n\t\t\t{\r\n\t\t\t\texit(1);\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tperror(\"create shared memory failed\\n\");\r\n\t\t        exit(1);\r\n\t\t}\r\n\t}\r\n\t\/\/attach the shared memory to the current process\r\n\tif((shmaddr=shmat(shmid_goods,(char*)0,0))==(char*)-1)\r\n\t{\r\n\t\tperror(\"attach shared memory error\\n\");\r\n\t\texit(1);\r\n\t}<\/pre>\n<p>\u63a5\u4e0b\u6765\u8fd8\u8981\u518d\u7533\u8bf7\u4e00\u5757\u5171\u4eab\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u653e\u4e24\u4e2a\u6574\u5f62\u53d8\u91cfin\u548cout\uff08\u5176\u5b9e\u5c31\u662f\u7533\u8bf7\u4e00\u4e2a\u542b\u67092\u4e2a\u6574\u5f62\u53d8\u91cf\u7684\u6570\u7ec4\u800c\u5df2\uff09\u3002\u4ed6\u4eec\u8bb0\u5f55\u7684\u662f\u751f\u4ea7\u548c\u6d88\u8d39\u8d27\u7269\u65f6\u201c\u8d27\u67b6\u201d\u7684\u7d22\u5f15\u3002\u4e0e\u4e0a\u9762\u60c5\u51b5\u76f8\u540c\uff0c\u5982\u679c\u5df2\u7ecf\u6709\u5176\u4ed6\u8fdb\u7a0b\u521b\u5efa\u4e86\u6b64\u5757\u5171\u4eab\u5185\u5b58\uff0c\u90a3\u4e48\u5f53\u524d\u8fdb\u7a0b\u53ea\u662f\u6253\u5f00\u5b83\u800c\u5df2\u3002<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u5bf9\u4e24\u4e2a\u6574\u5f62\u53d8\u91cf\u7684\u521d\u59cb\u5316\u65f6\u7684\u503c\u5747\u4e3a0\u3002<\/p>\n<pre class=\"brush:c\">\t\/\/create a shared memory as index\r\n\tif((shmid_index=createshm(\".\",'z',8))==-1)\r\n\t{\r\n\t\tif(errno==EEXIST)\r\n\t\t{\r\n\t\t\tif((shmid_index=openshm(\".\",'z'))==-1)\r\n\t\t\t{\r\n\t\t\t\texit(1);\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tperror(\"create shared memory failed\\n\");\r\n\t\t        exit(1);\r\n\t\t}\r\n\t}\r\n\telse\r\n\t{\r\n\t\tis_noexist=1;\r\n\t}\r\n\t\/\/attach the shared memory to the current process\r\n\tif((indexaddr=shmat(shmid_index,(int*)0,0))==(int*)-1)\r\n\t{\r\n\t\tperror(\"attach shared memory error\\n\");\r\n\t\texit(1);\r\n\t}\r\n\tif(is_noexist)\r\n\t{\r\n\t\tindexaddr[0]=0;\r\n\t\tindexaddr[1]=0;\r\n\t}<\/pre>\n<p>\u63a5\u4e0b\u6765\u5c31\u662f\u521b\u5efa\u4e00\u4e2a\u4fe1\u53f7\u91cf\u96c6\uff0c\u8fd9\u4e2a\u4fe1\u53f7\u91cf\u96c6\u4e2d\u5305\u542b\u4e09\u4e2a\u4fe1\u53f7\u91cf\u3002\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\u5b9e\u73b0\u7684\u4e92\u65a5\u4f5c\u7528\uff0c\u5373\u8fdb\u7a0b\u5bf9\u4e34\u754c\u533a\u7684\u4e92\u65a5\u8bbf\u95ee\u3002\u5269\u4e0b\u4e24\u4e2a\u5747\u5b9e\u73b0\u7684\u662f\u540c\u6b65\u4f5c\u7528\uff0c\u534f\u8c03\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7684\u5408\u7406\u8fd0\u884c\uff0c\u5373\u8d27\u67b6\u4e0a\u6ca1\u6709\u7a7a\u4f4d\u65f6\u5019\u751f\u4ea7\u8005\u4e0d\u518d\u751f\u4ea7\uff0c\u8d27\u67b6\u4e0a\u65e0\u5546\u54c1\u65f6\u6d88\u8d39\u8005\u4e0d\u518d\u6d88\u8d39\u3002<\/p>\n<p>\u6ce8\u610f\u4e0b\u9762\u5bf9\u6bcf\u4e2a\u4fe1\u53f7\u91cf\u7684\u8d4b\u503c\u60c5\u51b5\u3002\u4e92\u65a5\u4fe1\u53f7\u91cf\u5f53\u7136\u521d\u503c\u4e3a1\u3002\u800c\u540c\u6b65\u4fe1\u53f7\u91cf\u4e24\u8005\u4e4b\u548c\u4e0d\u80fd\u5927\u4e8enum\u7684\u503c\u3002<\/p>\n<pre class=\"brush:c\">\t\/\/create a semaphore set including 3 semaphores\r\n\tif((semid=createsem(\".\",'t',3,0))==-1)\r\n\t{\r\n\t\tif(errno==EEXIST)\r\n\t\t{\r\n\t\t\tif((semid=opensem(\".\",'t'))==-1)\r\n\t\t\t{\r\n\t\t\t\texit(1);\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tperror(\"semget error:\");\r\n\t\t\texit(1);\r\n\t\t}\r\n\t}\r\n\telse\r\n\t{\r\n\t\tunion semun arg;\r\n\t        \/\/seting value for mutex semaphore\r\n         \targ.val=1;\r\n         \tif(semctl(semid,0,SETVAL,arg)==-1)\r\n         \t{\r\n\t\t\tperror(\"setting semaphore value failed\\n\");\r\n\t        \treturn -1;\r\n            \t}\r\n          \t\/\/set value for synchronous semaphore\r\n\t        arg.val=num;\r\n            \t\/\/the num means that the producer can continue to produce num products\r\n          \tif(semctl(semid,1,SETVAL,arg)==-1)\r\n            \t{\r\n\t\t\tperror(\"setting semaphore value failed\\n\");\r\n\t         \treturn -1;\r\n         \t}\r\n           \t\/\/the last semaphore's value is default\r\n           \t\/\/the default value '0' means that the consumer is not use any product now\r\n      \t}<\/pre>\n<p>\u57fa\u672c\u4e0a\u8fd9\u6837\uff0c\u5c31\u7b97\u5b8c\u6210\u4e86\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7684\u524d\u671f\u5de5\u4f5c\u3002\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u6838\u5fc3\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u201c\u88c5\u6a21\u4f5c\u6837\u201d\u7684\u5c06\u4ee3\u7801\u201c\u5404\u5c31\u5404\u4f4d\u201d\u5373\u53ef\uff0c\u5f53\u7136\u8fd9\u9700\u8981\u4f60\u7406\u89e3\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u8fd9\u4e2a\u57fa\u672c\u6a21\u578b\u3002\u800c\u5728\u4e0b\u9762\u7684\u51c6\u5907\u4ee3\u7801\u4e2d\uff0c\u5219\u9700\u8981\u6211\u4eec\u7406\u89e3\u5173\u4e8e\u4fe1\u53f7\u91cf\u548c\u5171\u4eab\u5185\u5b58\u7684\u4e00\u4e9b\u57fa\u672c\u51fd\u6570\u3002<\/p>\n<p>\u6700\u540e\u518d\u8bf4\u8bf4\u4f7f\u7528\uff0c\u5efa\u8bae\u5148\u8fd0\u884c\u4e00\u4e2a\u751f\u4ea7\u8005\u548c\u4e00\u4e2a\u6d88\u8d39\u8005\uff0c\u89c2\u5bdf\u4e24\u8005\u662f\u5982\u4f55\u534f\u8c03\u5de5\u4f5c\u7684\u3002\u7136\u540e\u518d\u53ea\u8fd0\u884c\u4e00\u4e2a\u751f\u4ea7\u8005\u6216\u4e00\u4e2a\u6d88\u8d39\u8005\uff0c\u770b\u5176\u662f\u5426\u4f1a\u963b\u585e\u3002\u4e86\u89e3\u4e86\u4ee5\u4e0a\u60c5\u51b5\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60\u4e86\u4fe1\u53f7\u91cf\u4ee5\u53ca\u5171\u4eab\u5185\u5b58\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9e\u73b0\u8fdb\u7a0b\u7684\u540c\u6b65\u4e0e\u4e92\u65a5\u4e86\u3002\u8bf4\u5230\u8fd9\u91cc\uff0c\u6700\u7ecf\u5178\u7684\u4f8b\u5b50\u83ab\u8fc7\u4e8e\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[109,139,142,402],"class_list":["post-1248","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-c","tag-ipc","tag-142","tag-402"],"views":11746,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=1248"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1248\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}