{"id":1835,"date":"2010-10-25T21:40:52","date_gmt":"2010-10-25T13:40:52","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1835"},"modified":"2010-11-30T15:48:08","modified_gmt":"2010-11-30T07:48:08","slug":"%e7%94%9f%e4%ba%a7%e8%80%85-%e6%b6%88%e8%b4%b9%e8%80%85%e6%a8%a1%e5%9e%8b%e4%bd%bf%e7%94%a8%e5%86%85%e6%a0%b8%e7%ba%bf%e7%a8%8b%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1835","title":{"rendered":"\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b(\u4f7f\u7528\u5185\u6838\u7ebf\u7a0b\u5b9e\u73b0)"},"content":{"rendered":"<p>\u5728IPC\u7cfb\u5217<a href=\"http:\/\/edsionte.com\/techblog\/archives\/1248\" target=\"_blank\">\u6587\u7ae0<\/a>\u5f53\u4e2d\uff0c\u5229\u7528\u4fe1\u53f7\u91cf\u96c6\u5b9e\u73b0\u7684\u662f\u7528\u6237\u6001\u4e0b\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\u7684\u6a21\u578b\u3002\u672c\u6587\u4ee5\u5185\u6838\u6a21\u5757\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u521b\u5efa\u5185\u6838\u7ebf\u7a0b\u6765\u4e3a\u5927\u5bb6\u6f14\u793a\u5185\u6838\u6001\u4e0b\u7684\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u3002\u672c\u6a21\u578b\u5c5e\u4e8enp-nc-nb\uff0c\u5373\u591a\u4e2a\u751f\u4ea7\u8005\u591a\u4e2a\u6d88\u8d39\u7740\u591a\u4e2a\u7f13\u51b2\u533a\u3002<\/p>\n<p>\u5728\u52a0\u8f7d\u51fd\u6570\u4e2d\uff0c\u5b8c\u6210\u4e00\u4e9b\u521d\u59cb\u5316\u7684\u5de5\u4f5c\uff0c\u5e76\u5206\u522b\u521b\u5efa\u4e865\u4e2a\u751f\u4ea7\u8005\u7ebf\u7a0b\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002kernel_thread\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6240\u6240\u521b\u5efa\u7ebf\u7a0b\u8981\u505a\u7684\u52a8\u4f5c\uff1b\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012i\u53d8\u91cf\u3002<\/p>\n<pre class=\"brush:c\">static int __init np_nc_init(void)\r\n{\r\n\tint i;\r\n\r\n\tprintk(\"np_nc module is working..\\n\");\r\n\tin=out=0;\r\n\tcflag=0;\r\n\tinit_MUTEX(&amp;mutex);\r\n\tsema_init(&amp;s1,BUF_NUM);\r\n\tsema_init(&amp;s2,0);\r\n\r\n\tfor(i=0;i&lt; N;i++)\r\n\t{\r\n\t\tindex[i]=i+1;\r\n\t\tkernel_thread(productor_thread,&amp;(index[i]),CLONE_KERNEL);\r\n\t\tkernel_thread(consumer_thread,&amp;(index[i]),CLONE_KERNEL);\r\n\t}\r\n\r\n\treturn 0;\r\n}<\/pre>\n<p>\u5728\u751f\u4ea7\u8005\u51fd\u6570\u4e2d\uff0cPNUM\u662f\u6bcf\u4e2a\u751f\u4ea7\u8005\u8981\u751f\u4ea7\u8d27\u7269\u7684\u6570\u91cf\u3002\u8bed\u53e5buf[in]=i*100+(PNUM-p_num+1)\u5373\u662f\u4ea7\u751f\u8d27\u7269\u7684\u8fc7\u7a0b\u3002<\/p>\n<pre class=\"brush:c\">int productor_thread(void *p)\r\n{\r\n\tint i=*(int *)p;\r\n\tint p_num=PNUM;\r\n\r\n\twhile(p_num)\r\n\t{\r\n\t\tif((s1.count)&lt;=0)\r\n\t\t{\r\n\t\t\tprintk(\"[producer %d,%d]:I will be waiting for producting..\\n\",i,s1.count);\r\n\t\t}\r\n\t\tdown(&amp;s1);\r\n\t        down(&amp;mutex);\r\n          \tbuf[in]=i*100+(PNUM-p_num+1);\r\n          \tprintk(\"[producer %d,%d]:I producted a goods \\\"%d\\\" to buf[%d]..\\n\",i,s1.count,buf[in],in);\r\n            \tin=(in+1)%BUF_NUM;\r\n         \tup(&amp;mutex);\r\n          \tup(&amp;s2);\r\n\t\tp_num--;\r\n\t}\r\n\treturn 0;\r\n}<\/pre>\n<p>\u5728\u6d88\u8d39\u8005\u51fd\u6570\u4e2d\uff0c\u901a\u8fc7\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfCNUM\u6765\u63a7\u5236\u6d88\u8d39\u8005\u603b\u5171\u7684\u6d88\u8d39\u6b21\u6570\u3002<\/p>\n<pre class=\"brush:c\">int consumer_thread(void *p)\r\n{\r\n\tint i=*(int *)p;\r\n\tint goods;\r\n\r\n\twhile(cflag!=CNUM)\r\n\t{\r\n\t\tif((s2.count)&lt;=0)\r\n\t\t{\r\n\t\t\tprintk(\"[consumer %d,%d]:I will be waiting for goods..\\n\",i,s2.count);\r\n\t\t}\r\n\t        down(&amp;s2);\r\n            \tdown(&amp;mutex);\r\n          \tgoods=buf[out];\r\n           \tprintk(\"[consumer %d,%d]:I consumed a goods \\\"%d\\\" from buf[%d]..\\n\",i,s2.count,goods,(out%BUF_NUM));\r\n           \tout=(out+1)%BUF_NUM;\r\n            \tup(&amp;mutex);\r\n            \tup(&amp;s1);\r\n\t\tcflag++;\r\n\t}\r\n\treturn 0;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728IPC\u7cfb\u5217\u6587\u7ae0\u5f53\u4e2d\uff0c\u5229\u7528\u4fe1\u53f7\u91cf\u96c6\u5b9e\u73b0\u7684\u662f\u7528\u6237\u6001\u4e0b\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\u7684\u6a21\u578b\u3002\u672c\u6587\u4ee5\u5185\u6838\u6a21\u5757\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u521b\u5efa\u5185\u6838 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[170,168,169],"class_list":["post-1835","post","type-post","status-publish","format-standard","hentry","category-linux","tag-170","tag-168","tag-169"],"views":13731,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1835","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=1835"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1835\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1835"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}