{"id":163,"date":"2017-08-11T14:13:14","date_gmt":"2017-08-11T06:13:14","guid":{"rendered":"http:\/\/www.sinkland.cn\/?p=163"},"modified":"2019-08-17T15:04:38","modified_gmt":"2019-08-17T07:04:38","slug":"smlos%e5%9b%9b-fifo%e7%bc%93%e5%86%b2%e5%8c%ba%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/www.sinkland.cn\/?p=163","title":{"rendered":"SmlOS\u56db-FIFO\u7f13\u51b2\u533a\u5b9e\u73b0"},"content":{"rendered":"<h3>\u5148\u8fdb\u5148\u51fa\u961f\u5217<\/h3>\n<blockquote><p>\n  FIFO(First in First out)\n<\/p><\/blockquote>\n<p>\u7ed3\u6784\u4f53<\/p>\n<pre><code class=\"language-c \">struct FIFO32 \n{\n    int *buf;       \/\/\u7f13\u51b2\u533a\u6307\u9488\n    int p;          \/\/\u4e0b\u4e00\u4e2a\u6570\u636e\u5199\u5165\u4f4d\u7f6e\n    int q;          \/\/\u4e0b\u4e00\u4e2a\u6570\u636e\u8bfb\u51fa\u4f4d\u7f6e\n    int size;       \/\/\u7f13\u51b2\u533a\u7684\u603b\u5b57\u8282\u6570\n    int free;       \/\/\u7f13\u51b2\u533a\u7684\u7a7a\u95f2\u5b57\u8282\u6570\n    int flags;      \/\/\u8bb0\u5f55\u7f13\u51b2\u533a\u662f\u5426\u6ea2\u51fa\n    struct TASK *task;  \/\/\u5f53\u961f\u5217\u7f13\u51b2\u533a\u5199\u5165\u6570\u636e\u65f6\u9700\u8981\u5524\u9192\u7684\u4efb\u52a1\n};\n<\/code><\/pre>\n<p>\u76f8\u5173\u64cd\u4f5c\u51fd\u6570<\/p>\n<pre><code class=\"language-c \">\/\/\u7f13\u51b2\u533a\u521d\u59cb\u5316\u51fd\u6570 \nvoid fifo32_init(struct FIFO32 *fifo, int size, int *buf, struct TASK *task)\n{\n    fifo-&gt;size = size;\n    fifo-&gt;buf = buf;\n    fifo-&gt;free = size; \n    fifo-&gt;flags = 0;\n    fifo-&gt;p = 0; \/\/\u4e0b\u4e00\u4e2a\u6570\u636e\u5199\u5165\u4f4d\u7f6e \n    fifo-&gt;q = 0; \/\/\u4e0b\u4e00\u4e2a\u6570\u636e\u8bfb\u51fa\u4f4d\u7f6e\n    fifo-&gt;task = task; \n    return;\n}\n\n\/\/\u7f13\u51b2\u533a\u5199\u5165\u51fd\u6570\nint fifo32_put(struct FIFO32 *fifo, int data)\n{\n    \/\/\u5982\u679c\u7f13\u51b2\u533a\u5df2\u6ee1 \n    if (fifo-&gt;free == 0)\n    {               \n        fifo-&gt;flags |= FLAGS_OVERRUN;\n        return -1;\n    }\n    fifo-&gt;buf[fifo-&gt;p] = data;      \n    fifo-&gt;p++;\n    if (fifo-&gt;p == fifo-&gt;size) \n    {\n        fifo-&gt;p = 0;\n    }\n    fifo-&gt;free--;   \n     \/\/\u5982\u679c\u6709\u9700\u8981\u5524\u9192\u7684\u4efb\u52a1\n    if (fifo-&gt;task != 0)\n    {               \n        \/\/\u5982\u679c\u4efb\u52a1\u5904\u4e8e\u4f11\u7720 \n        if (fifo-&gt;task-&gt;flags != 2)\n        { \n            task_run(fifo-&gt;task, -1, 0); \n        }\n    }\n    return 0;\n}\n\n\/\/\u7f13\u51b2\u533a\u8bfb\u51fa\u51fd\u6570\nint fifo32_get(struct FIFO32 *fifo)\n{\n    int data;\n    \/\/\u7f13\u51b2\u533a\u4e3a\u7a7a \u6ca1\u6709\u6570\u636e\u53ef\u8bfb \n    if (fifo-&gt;free == fifo-&gt;size) \n    {\n        return -1;\n    }\n    data = fifo-&gt;buf[fifo-&gt;q];          \n    fifo-&gt;q++;                      \n    if (fifo-&gt;q == fifo-&gt;size)\n    {\n        fifo-&gt;q = 0;\n    }\n    fifo-&gt;free++;                   \n    return data;\n}\n\n<\/code><\/pre>\n<p>\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u5728\u4e00\u5757\u7f13\u51b2\u533a\u4e0a\uff0c\u6709\u4e00\u4e2a\u6307\u9488\u6307\u5411\u5199\u5165\u7684\u5730\u5740\uff0c\u4e00\u4e2a\u6307\u9488\u6307\u5411\u8bfb\u53d6\u7684\u5730\u5740\uff0c\u5982\u679c\u5199\u5165\u6ee1\u4e86\uff0c\u5219\u8be5\u5199\u5165\u7684\u6570\u636e\u4f1a\u88ab\u4e22\u5f03\u3002<\/p>\n<p>\u4ece\u800c\u5f62\u6210\u4e00\u4e2aIO\u73af\u3002<\/p>\n<p>\u4ee3\u7801\u548c\u539f\u7406\u662f\u6bd4\u8f83\u7b80\u5355\u7684\u3002<\/p>\n<p>\u4f46\u662f\u89e3\u51b3\u7684\u95ee\u9898\u5374\u662f\u4e0d\u7b80\u5355\u7684\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u4e3b\u8981\u89e3\u51b3\u4e86\u4e24\u4e2a\u95ee\u9898:<\/p>\n<hr \/>\n<h3>\u9f20\u6807\u4e2d\u65ad\u6570\u636e\u7684\u5904\u7406<\/h3>\n<p>\u56e0\u4e3a\u4e2d\u65ad\u51fd\u6570\u7684\u7279\u6b8a\u6027\uff0c\u4e2d\u65ad\u4e2d\u4e0d\u80fd\u5904\u7406\u8fc7\u591a\u4e8b\u60c5\uff0c\u6bd4\u5982\u8bf4\u5728\u9f20\u6807\u4e2d\u65ad\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5c06\u9f20\u6807\u7684\u6570\u636e\uff08\u79fb\u52a8\uff0c\u6309\u952e\u4fe1\u606f\uff09\u76f4\u63a5\u585e\u8fdbFIFO\u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c31\u9000\u51fa\u4e2d\u65ad\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p>\u7b49\u5f85\u5230\u5185\u6838\u7ebf\u7a0b\u8c03\u5ea6\u7684\u65f6\u5019\u81ea\u884c\u8bfb\u53d6FIFO\u4e2d\u7684\u6570\u636e\uff0c\u64cd\u4f5c\u663e\u5b58\u79fb\u52a8\u9f20\u6807\uff0c\u7a97\u4f53\u7b49\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u6837\u7684\u597d\u5904\u662f\uff0c\u5373\u4f7f\u4e2d\u65ad\u9891\u7e41\u53d1\u751f\uff0c\u5185\u6838\u7ebf\u7a0b\u90fd\u65e0\u529b\u5904\u7406\u7684\u65f6\u5019\uff0c\u9876\u591a\u4f1a\u4e22\u5f03\u4e00\u4e9b\u6570\u636e\uff0c\u4f46\u4e0d\u4f1a\u5f71\u54cd\u7cfb\u7edf\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<hr \/>\n<h3>\u8fdb\u7a0b\/\u7ebf\u7a0b\u95f4\u6d88\u606f\u901a\u8baf<\/h3>\n<p>\u73b0\u5728\u7684\u8bbe\u8ba1\u662f\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u4e00\u4e2aFIFO\u7ed3\u6784\u4f53\u7528\u4e8e\u5b58\u5728\u901a\u8baf\u6d88\u606f\u3002<\/p>\n<p>\u5411\u7ebf\u7a0b\u53d1\u9001\u6d88\u606f\u5c31\u662f\u5411\u8be5\u8fdb\u7a0bFIFO\u7ed3\u6784\u4f53\u5199\u5165\u6570\u636e\u3002<\/p>\n<p>\u7b49\u8be5\u7ebf\u7a0b\u6709\u6267\u884c\u673a\u4f1a\u7684\u65f6\u5019\uff0c\u81ea\u884c\u8bfb\u53d6\u5904\u7406\uff0c\u8fd9\u6837\u8fd9\u4e2a\u7cfb\u7edf\u7684\u8bbe\u8ba1\u90fd\u662f\u57fa\u4e8e\u6d88\u606f\u7684\u5f02\u6b65\u901a\u77e5\u673a\u5236\uff0c\u548c\u73b0\u4ee3\u7684\u64cd\u4f5c\u7cfb\u7edf\u601d\u60f3\u8bbe\u8ba1\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u8fdb\u5148\u51fa\u961f\u5217 FIFO(First in First out) \u7ed3\u6784\u4f53 struct FIFO32 { int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts\/163"}],"collection":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":1,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":164,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/164"}],"wp:attachment":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}