{"id":142,"date":"2021-03-24T18:38:51","date_gmt":"2021-03-24T10:38:51","guid":{"rendered":"http:\/\/blog.kijk.top\/?p=142"},"modified":"2022-05-31T14:59:12","modified_gmt":"2022-05-31T06:59:12","slug":"linux%e7%94%9f%e4%ba%a7-%e6%b6%88%e8%b4%b9%e8%80%85%e6%a8%a1%e5%9e%8b%e5%ba%93-b%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"http:\/\/blog.kijk.top\/?p=142","title":{"rendered":"Linux\u751f\u4ea7\/\u6d88\u8d39\u8005\u6a21\u578b\u5e93-B\u961f\u5217"},"content":{"rendered":"\n<p>\u8fd9\u662f\u6211\u5199\u7684\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\u5e93\uff0c\u867d\u7136\u7b80\u5355\uff0c\u4f46\u6211\u8fd8\u662f\u7ed9\u5b83\u8d77\u4e86\u4e2a\u540d\u5b57&#8212;&#8211;<strong><span style=\"color:#f80f03\" class=\"has-inline-color\">B\u961f\u5217<\/span><\/strong>\uff0c\u610f\u4e3aBlocking queue(\u963b\u585e\u961f\u5217)\u3002\u5185\u90e8\u662f\u91c7\u7528Linux\u4fe1\u53f7\u91cf\u5b9e\u73b0\u7684\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u57fa\u7840\u7684\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\uff0c\u6bd4\u5982\uff1a\u7b80\u5355\u7684\u7ebf\u7a0b\u6c60\u7b49\u3002<\/p>\n\n\n\n<p>\u9879\u76ee\u5730\u5740\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/liangdodo\/bqueue\" target=\"_blank\"><span class=\"has-inline-color has-vivid-cyan-blue-color\">https:\/\/github.com\/liangdodo\/bqueue<\/span><\/a><\/p>\n\n\n\n<p>\u9759\u6001\u5e93\uff1a<a href=\"http:\/\/blog.kijk.top\/wp-content\/uploads\/2021\/03\/bqueue.zip\"><span class=\"has-inline-color has-vivid-cyan-blue-color\">bqueue.zip<\/span><\/a><\/p>\n\n\n\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\n#include \"bqueue.h\"\n\n\/\/\u751f\u4ea7\u8005\u7ebf\u7a0b\nvoid* producer_thread(void *ptr){\n\tstruct bqueue_t *bq=(struct bqueue_t*)ptr;\n\tint data,i=1001;\n\t\n\tprintf(\"\u751f\u4ea7\u8005\u5df2\u542f\u52a8\u751f\u4ea7...\\n\");\n\t\n\tfor(;;){\n\t\t\/\/\u751f\u4ea7\u8005\u96945\u79d2\u751f\u4ea7\u4e00\u4e2aint\u6570\u636e \u653e\u5165\u5230\u961f\u5217\n\t\tsleep(5);\n\t\tbqueue_push(bq,&amp;i);\n\t\ti++;\n\t}\n\t\n\treturn NULL;\n}\n\n\/\/\u6d88\u8d39\u8005\u7ebf\u7a0b\nvoid* consumer_thread(void *ptr){\n\tstruct bqueue_t *bq=(struct bqueue_t*)ptr;\n\tint data;\n\t\n\tprintf(\"\u6d88\u8d39\u8005\u5df2\u542f\u52a8\u6d88\u8d39...\\n\");\n\t\n\tfor(;;){\n\t\t\/\/\u4eceb\u961f\u5217\u91cc\u9762\u53d6\u51fa\u4e00\u4e2a\u5143\u7d20\u653e\u5165\u5230data\u53d8\u91cf\u4e2d\uff0c\u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u963b\u585e\n\t\tbqueue_pop(bq,&amp;data);\n\t\tprintf(\"\u6d88\u8d39\u8005\u4ece\u961f\u5217\u91cc\u9762\u62ff\u51fa\u4e86\u4e00\u4e2a\u5143\u7d20:%d\\n\",data);\n\t}\n\t\n\treturn NULL;\n}\n\n\nint main(int argc,char *argv&#91;]){\n\t\n\tstruct bqueue_t bqueue;\/\/\u5b9a\u4e49\u4e00\u4e2ab\u961f\u5217\n\tpthread_t producer_tid,consumer_tid;\/\/\u751f\u4ea7\u3001\u6d88\u8d39\u8005\u7684\u7ebf\u7a0btid\n\t\n\t\/\/\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u91cf\u4e3a100b\u961f\u5217\uff0c\u961f\u5217\u4e2d\u7684\u6bcf\u4e2a\u6570\u636e\u5143\u7d20\u4e3aint\u7c7b\u578b\uff08sizeof(int)\uff09\n\tbqueue_init(&amp;bqueue,sizeof(int),100);\n\t\/\/\u521b\u5efa\u751f\u4ea7\u8005\u7ebf\u7a0b\n\tpthread_create(&amp;producer_tid,NULL,producer_thread,&amp;bqueue);\n\t\/\/\u521b\u5efa\u6d88\u8d39\u8005\u7ebf\u7a0b\n\tpthread_create(&amp;consumer_tid,NULL,consumer_thread,&amp;bqueue);\n\t\n\t\/\/\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\n\tpthread_join(producer_tid,NULL);\n\tpthread_join(consumer_tid,NULL);\n\t\/\/\u9500\u6bc1b\u961f\u5217\n\tbqueue_destroy(&amp;bqueue);\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>\u7f16\u8bd1+\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc -o demo main.c -l pthread -L .\/ -l bqueue\n.\/demo<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"575\" height=\"121\" src=\"http:\/\/blog.kijk.top\/wp-content\/uploads\/2021\/03\/Dingtalk_20210324183629.jpg\" alt=\"\" class=\"wp-image-156\" srcset=\"http:\/\/blog.kijk.top\/wp-content\/uploads\/2021\/03\/Dingtalk_20210324183629.jpg 575w, http:\/\/blog.kijk.top\/wp-content\/uploads\/2021\/03\/Dingtalk_20210324183629-300x63.jpg 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/figure>\n\n\n\n<p>\u5f53\u7136\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u6c38\u8fdc\u65e0\u6cd5\u7ed3\u675f\uff0c\u6240\u4ee5\u5c31\u8bf7\u60a8Ctrl+c \u4e00\u4e0b\u4e86\uff0c(#^.^#)<\/p>\n\n\n\n<p>\u57fa\u672c\u64cd\u4f5c\u8bf4\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n* \u521d\u59cb\u5316B\u961f\u5217\n* bq : B\u961f\u5217\u6307\u9488\n* data_size:\u961f\u5217\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5c3a\u5bf8\uff0c\u901a\u5e38\u4f7f\u7528sizeof(\u6570\u636e\u7c7b\u578b)\u5f97\u5230\n* max_length:\u961f\u5217\u4e2d\u53ef\u5bb9\u7eb3\u7684\u6700\u5927\u5143\u7d20\u4e2a\u6570\n* \u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de 0 \uff0c\u5931\u8d25\u8fd4\u56de -1\n*\/\nint bqueue_init(struct bqueue_t *bq,int data_size,int max_length);\n\n\/**\n* \u9500\u6bc1B\u961f\u5217\n* bq:B\u961f\u5217\u6307\u9488\n*\/\nvoid bqueue_destroy(struct bqueue_t *bq);\n\n\/**\n* \u5728B\u961f\u5217\u672b\u5c3e\u589e\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u82e5\u961f\u5217\u5df2\u7ecf\u6ee1\u4e86\uff0c\u5219\u8be5\u51fd\u6570\u4f1a\u963b\u585e\n* bq:B\u961f\u5217\u6307\u9488\n* data:\u589e\u52a0\u7684\u6570\u636e\u5143\u7d20\u6307\u9488\uff0c\u6ce8\uff1a\u8be5\u5730\u5740\u6240\u6307\u5411\u7684\u7c7b\u578b\u5c3a\u5bf8\u5e94\u8be5\u548cbqueue_init()\u521d\u59cb\u5316\u51fd\u6570\u4e2d\u7684data_size\u503c\u76f8\u540c\u3002\n* \u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de 0 \uff0c\u5931\u8d25\u8fd4\u56de -1\n*\/\nint bqueue_push(struct bqueue_t *bq,void *data);\n\n\/**\n* \u5c06B\u961f\u5217\u6700\u524d\u9762\u7684\u4e00\u4e2a\u5143\u7d20\u53d6\u51fa\n* bq:B\u961f\u5217\u6307\u9488\n* data:\u53d6\u51fa\u5143\u7d20\u6240\u5b58\u653e\u7684\u5730\u5740\uff0c\u5982\u679c\u4e0d\u5173\u5fc3\u5176\u503c\uff0c\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3aNULL\u3002\u6ce8\uff1a\u8be5\u5730\u5740\u6240\u6307\u5411\u7684\u7c7b\u578b\u5c3a\u5bf8\u5e94\u8be5\u548cbqueue_init()\u521d\u59cb\u5316\u51fd\u6570\u4e2d\u7684data_size\u503c\u76f8\u540c\u3002\n* \u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de 0 \uff0c\u5931\u8d25\u8fd4\u56de -1\n*\/\nint bqueue_pop(struct bqueue_t *bq,void *data);\n\n\/**\n* \u83b7\u5f97B\u961f\u5217\u4e2d\u6709\u6548\u5143\u7d20\u4e2a\u6570\n* bq:B\u961f\u5217\u6307\u9488\n* \u8fd4\u56de\u503c \uff1a\u5143\u7d20\u4e2a\u6570\n*\/\nint bqueue_length(struct bqueue_t *bq);\n\n\/**\n* \u83b7\u5f97B\u961f\u5217\u5934\u90e8\u7684\u5143\u7d20\u5185\u5bb9\uff08\u4e0d\u53d6\u51fa\uff09\n* bq:B\u961f\u5217\u6307\u9488\n* buf: \u53d6\u51fa\u5143\u7d20\u6240\u5b58\u653e\u7684\u5730\u5740\n* \u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de 0 \uff0c\u5931\u8d25\u8fd4\u56de -1\n*\/\nint bqueue_head(struct bqueue_t *bq,void *buf);\n\n\/**\n* \u83b7\u5f97B\u961f\u5217\u5c3e\u90e8\u7684\u5143\u7d20\u5185\u5bb9\uff08\u4e0d\u53d6\u51fa\uff09\n* bq:B\u961f\u5217\u6307\u9488\n* buf: \u53d6\u51fa\u5143\u7d20\u6240\u5b58\u653e\u7684\u5730\u5740\n* \u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de 0 \uff0c\u5931\u8d25\u8fd4\u56de -1\n*\/\nint bqueue_tail(struct bqueue_t *bq,void *buf);\n\n\/**\n* \u6e05\u7a7aB\u961f\u5217\n* bq:B\u961f\u5217\u6307\u9488\n*\/\nvoid bqueue_clear(struct bqueue_t *bq);\n\n\/**\n* \u521b\u5efa\u4e00\u4e2aB\u961f\u5217\u8282\u70b9\n* bq:B\u961f\u5217\u6307\u9488\n* data:\/\/\u8282\u70b9\u4e2d\u7f29\u653e\u6570\u636e\u5185\u5bb9\u6307\u9488\n* \u8fd4\u56de\u503c\uff0c\u6210\u529f\u8fd4\u56de\u8282\u70b9\u6307\u9488\uff0c\u5931\u8d25\u8fd4\u56deNULL\n*\/\nstruct bqueue_node_t * bqueue_make_node(struct bqueue_t *bq,void *data);\n\n\/**\n* \u91ca\u653e\u4e00\u4e2aB\u961f\u5217\u8282\u70b9\n* bq_node:\u8282\u70b9\u6307\u9488\n*\/\nvoid bqueue_free_node(struct bqueue_node_t *bq_node);\n\n\/**\n* \u904d\u5386B\u961f\u5217\uff0c\u4e14\u6267\u884c\u56de\u8c03\n* bq:B\u961f\u5217\u6307\u9488\n* call:\u56de\u8c03\u51fd\u6570\u6307\u9488\uff0c\u58f0\u660e\u4e3a\uff1avoid xxx(void *data)\n*\/\nvoid bqueue_traverse(struct bqueue_t *bq,void (*call)(void *data));\n\n\/**\n* \u83b7\u5f97B\u961f\u5217\u4e2d\u8fd8\u80fd\u5bb9\u7eb3\u4e0b\u591a\u5c11\u5143\u7d20\u4e2a\u6570\uff08\u5269\u4f59\u7a7a\u95f4\uff09\n* bq:B\u961f\u5217\u6307\u9488\n* \u8fd4\u56de\u503c\uff1a\u8fd8\u80fd\u5bb9\u7eb3\u4e0b\u7684\u7a7a\u95f2\u5143\u7d20\u4e2a\u6570\n*\/\nint bqueue_get_free(struct bqueue_t *bq);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u6211\u5199\u7684\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\u5e93\uff0c\u867d\u7136\u7b80\u5355\uff0c\u4f46\u6211\u8fd8\u662f\u7ed9\u5b83\u8d77\u4e86\u4e2a\u540d\u5b57&#8212;&#8211;B\u961f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts\/142"}],"collection":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":18,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":285,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions\/285"}],"wp:attachment":[{"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.kijk.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}