{"id":79998,"date":"2026-03-03T23:56:54","date_gmt":"2026-03-03T15:56:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/79998.html"},"modified":"2026-03-03T23:56:54","modified_gmt":"2026-03-03T15:56:54","slug":"%e9%80%9a%e4%bf%a1%ef%bc%9a9-%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97%e4%b8%8e%e4%bc%98%e5%85%88%e7%ba%a7%e5%8f%8d%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/79998.html","title":{"rendered":"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c"},"content":{"rendered":"<h2>1.\u00a0\u6d88\u606f\u961f\u5217\u4f7f\u7528\u6d41\u7a0b<\/h2>\n<h3>1.1 \u6982\u5ff5<\/h3>\n<p>\u6d88\u606f\u961f\u5217\u662f\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;IPC&#xff09;\u4e2d\u81ea\u5e26\u540c\u6b65\u673a\u5236\u7684\u901a\u4fe1\u65b9\u5f0f&#xff0c;\u5929\u7136\u907f\u514d\u7ade\u6001\u6761\u4ef6&#xff0c;mq_receive() \u4f1a\u963b\u585e\u76f4\u5230\u6d88\u606f\u5230\u8fbe&#xff0c;\u65e0\u9700\u989d\u5916\u540c\u6b65\u539f\u8bed\u3002<\/p>\n<table>\n<tr>\n<p>\u7279\u6027<\/p>\n<p>\u5171\u4eab\u5185\u5b58<\/p>\n<p>\u6d88\u606f\u961f\u5217<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>\u6570\u636e\u6a21\u578b<\/p>\n<\/td>\n<td>\n<p>\u65e0\u7ed3\u6784\u7684\u5b57\u8282\u6d41<\/p>\n<\/td>\n<td>\n<p>\u6709\u8fb9\u754c\u7684\u6d88\u606f&#xff08;\u81ea\u52a8\u5206\u5305&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u540c\u6b65\u673a\u5236<\/p>\n<\/td>\n<td>\n<p>\u9700\u989d\u5916\u4fe1\u53f7\u91cf\/\u9501<\/p>\n<\/td>\n<td>\n<p>\u5185\u7f6e\u963b\u585e\/\u5524\u9192\u673a\u5236<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6570\u636e\u6301\u4e45\u6027<\/p>\n<\/td>\n<td>\n<p>\u8fdb\u7a0b\u9000\u51fa\u540e\u6570\u636e\u4fdd\u7559<\/p>\n<\/td>\n<td>\n<p>\u6d88\u606f\u5728\u63a5\u6536\u524d\u6301\u4e45\u5b58\u5728<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u901a\u4fe1\u6a21\u5f0f<\/p>\n<\/td>\n<td>\n<p>\u591a\u5bf9\u591a\u5171\u4eab<\/p>\n<\/td>\n<td>\n<p>\u70b9\u5bf9\u70b9\u6216\u5e7f\u64ad&#xff08;POSIX\u652f\u6301\u591a\u8bfb\u8005&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u5178\u578b\u573a\u666f<\/p>\n<\/td>\n<td>\n<p>\u9ad8\u9891\u5927\u6570\u636e\u4ea4\u6362<\/p>\n<\/td>\n<td>\n<p>\u547d\u4ee4\/\u4e8b\u4ef6\u901a\u77e5\u3001\u4f4e\u9891\u5c0f\u6570\u636e<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u7ba1\u7406\u65b9\u5f0f\u4e5f\u88ab\u89c6\u4e3a\u6587\u4ef6\u3002<\/p>\n<h3>1.2 mq_writer.c<\/h3>\n<p>\/\/ mq_writer.c &#8211; \u6d88\u606f\u961f\u5217\u5199\u8fdb\u7a0b&#xff08;\u53d1\u9001\u8005&#xff09;<br \/>\n\/\/ \u7f16\u8bd1&#xff1a;gcc mq_writer.c -o mq_writer -lrt<br \/>\n\/\/ \u8fd0\u884c&#xff1a;.\/mq_writer<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;mqueue.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;errno.h&gt;<\/p>\n<p>\/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u914d\u7f6e\u53c2\u6570 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n#define MQ_NAME     &#034;\/my_message_queue&#034;  \/\/ \u5fc5\u987b\u4ee5\/\u5f00\u5934<br \/>\n#define MQ_PERM     0664                 \/\/ \u6743\u9650&#xff1a;rw-rw-r&#8211;<br \/>\n#define MAX_MSG     10                   \/\/ \u961f\u5217\u6700\u5927\u6d88\u606f\u6570<br \/>\n#define MAX_MSG_SIZE 256                \/\/ \u5355\u6761\u6d88\u606f\u6700\u5927\u5b57\u8282\u6570<br \/>\n\/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>typedef struct {<br \/>\n    int type;          \/\/ \u6d88\u606f\u7c7b\u578b&#xff08;POSIX\u6807\u51c6\u4e2d\u5b9e\u9645\u672a\u4f7f\u7528&#xff0c;\u4f46\u53ef\u81ea\u5b9a\u4e49&#xff09;<br \/>\n    char data[256];    \/\/ \u6d88\u606f\u8d1f\u8f7d<br \/>\n} msg_t;<\/p>\n<p>int main() {<br \/>\n    printf(&#034;\\\\n&#061;&#061;&#061; [WRITER] \u6d88\u606f\u961f\u5217\u5199\u8fdb\u7a0b\u542f\u52a8 (PID&#061;%d) &#061;&#061;&#061;\\\\n&#034;, getpid());<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 1: \u521b\u5efa\u6d88\u606f\u961f\u5217\u5c5e\u6027\u5bf9\u8c61<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    struct mq_attr attr &#061; {0};<br \/>\n    attr.mq_flags &#061; 0;          \/\/ 0 &#061; \u963b\u585e\u6a21\u5f0f&#xff08;O_NONBLOCK \u53ef\u8bbe\u4e3a\u975e\u963b\u585e&#xff09;<br \/>\n    attr.mq_maxmsg &#061; MAX_MSG;   \/\/ \u961f\u5217\u6700\u5927\u6d88\u606f\u6570<br \/>\n    attr.mq_msgsize &#061; MAX_MSG_SIZE; \/\/ \u5355\u6761\u6d88\u606f\u6700\u5927\u5b57\u8282\u6570<br \/>\n    attr.mq_curmsgs &#061; 0;        \/\/ \u5f53\u524d\u6d88\u606f\u6570&#xff08;\u53ea\u8bfb&#xff0c;\u521b\u5efa\u65f6\u5ffd\u7565&#xff09;<\/p>\n<p>    printf(&#034;[1] \u914d\u7f6e\u6d88\u606f\u961f\u5217\u5c5e\u6027: max&#061;%d, size&#061;%d\\\\n&#034;,<br \/>\n           attr.mq_maxmsg, attr.mq_msgsize);<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 2: \u521b\u5efa\/\u6253\u5f00 POSIX \u6d88\u606f\u961f\u5217<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    mqd_t mq &#061; mq_open(MQ_NAME, O_CREAT | O_EXCL | O_WRONLY, MQ_PERM, &amp;attr);<br \/>\n    if (mq &#061;&#061; (mqd_t)-1) {<br \/>\n        if (errno &#061;&#061; EEXIST) {<br \/>\n            printf(&#034;  \u6d88\u606f\u961f\u5217 %s \u5df2\u5b58\u5728&#xff0c;\u5c1d\u8bd5\u6253\u5f00&#8230;\\\\n&#034;, MQ_NAME);<br \/>\n            mq &#061; mq_open(MQ_NAME, O_WRONLY);<br \/>\n            if (mq &#061;&#061; (mqd_t)-1) {<br \/>\n                perror(&#034;\u274c mq_open (fallback) \u5931\u8d25&#034;);<br \/>\n                exit(EXIT_FAILURE);<br \/>\n            }<br \/>\n        } else {<br \/>\n            perror(&#034;\u274c mq_open \u521b\u5efa\u6d88\u606f\u961f\u5217\u5931\u8d25&#034;);<br \/>\n            exit(EXIT_FAILURE);<br \/>\n        }<br \/>\n    }<br \/>\n    printf(&#034;[2] mq_open \u521b\u5efa\u6d88\u606f\u961f\u5217\u6210\u529f: %s, mqd&#061;%d\\\\n&#034;, MQ_NAME, mq);<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 3: \u53d1\u9001\u591a\u6761\u6d88\u606f&#xff08;\u81ea\u52a8\u963b\u585e\u76f4\u5230\u961f\u5217\u6709\u7a7a\u95f4&#xff09;<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    msg_t msg;<br \/>\n    for (int i &#061; 0; i &lt; 3; i&#043;&#043;) {<br \/>\n        msg.type &#061; i &#043; 1;<br \/>\n        snprintf(msg.data, sizeof(msg.data),<br \/>\n                 &#034;Message #%d from Writer (PID&#061;%d)&#034;, i&#043;1, getpid());<\/p>\n<p>        if (mq_send(mq, (char*)&amp;msg, sizeof(msg), 0) &#061;&#061; -1) { \/\/ \u4f18\u5148\u7ea7&#061;0<br \/>\n            perror(&#034;\u274c mq_send \u5931\u8d25&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n        printf(&#034;[3.%d]  mq_send \u6210\u529f: \\\\&#034;%s\\\\&#034;\\\\n&#034;, i&#043;1, msg.data);<br \/>\n        sleep(1); \/\/ \u6a21\u62df\u95f4\u9694\u53d1\u9001<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 4: \u53d1\u9001\u7ec8\u6b62\u4fe1\u53f7&#xff08;\u7279\u6b8a\u6d88\u606f&#xff09;<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    msg.type &#061; 999;<br \/>\n    strcpy(msg.data, &#034;TERMINATE&#034;);<br \/>\n    if (mq_send(mq, (char*)&amp;msg, sizeof(msg), 1) &#061;&#061; -1) { \/\/ \u9ad8\u4f18\u5148\u7ea7&#061;1<br \/>\n        perror(&#034;\u274c \u7ec8\u6b62\u6d88\u606f\u53d1\u9001\u5931\u8d25&#034;);<br \/>\n    } else {<br \/>\n        printf(&#034;[4]  \u53d1\u9001\u9ad8\u4f18\u5148\u7ea7\u7ec8\u6b62\u4fe1\u53f7 (priority&#061;1)\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 5: \u5173\u95ed\u6d88\u606f\u961f\u5217\u63cf\u8ff0\u7b26<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    if (mq_close(mq) &#061;&#061; -1) {<br \/>\n        perror(&#034;\u274c mq_close \u5931\u8d25&#034;);<br \/>\n    }<br \/>\n    printf(&#034;[5] mq_close \u5173\u95ed\u6d88\u606f\u961f\u5217\u63cf\u8ff0\u7b26\u6210\u529f\\\\n&#034;);<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 6: \u5220\u9664\u6d88\u606f\u961f\u5217\u540d\u79f0&#xff08;\u521b\u5efa\u8005\u8d1f\u8d23\u6e05\u7406&#xff09;<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6ce8\u610f&#xff1a;\u5b9e\u9645\u9500\u6bc1\u9700\u7b49\u5f85\u6240\u6709\u8fdb\u7a0b mq_close \u540e<br \/>\n    if (mq_unlink(MQ_NAME) &#061;&#061; -1) {<br \/>\n        if (errno !&#061; ENOENT) { \/\/ \u53ef\u80fd\u5df2\u88ab\u5176\u4ed6\u8fdb\u7a0b unlink<br \/>\n            perror(&#034;\u274c mq_unlink \u5931\u8d25&#034;);<br \/>\n        }<br \/>\n    } else {<br \/>\n        printf(&#034;[6] mq_unlink \u5220\u9664\u6d88\u606f\u961f\u5217\u540d\u79f0\u6210\u529f: %s\\\\n&#034;, MQ_NAME);<br \/>\n    }<\/p>\n<p>    printf(&#034;&#061;&#061;&#061; [WRITER] \u5199\u8fdb\u7a0b\u6b63\u5e38\u9000\u51fa &#061;&#061;&#061;\\\\n\\\\n&#034;);<br \/>\n    return 0;<br \/>\n} <\/p>\n<h3>1.3\u00a0mq_reader.c<\/h3>\n<p>\/\/ mq_reader.c &#8211; \u6d88\u606f\u961f\u5217\u8bfb\u8fdb\u7a0b&#xff08;\u63a5\u6536\u8005&#xff09;<br \/>\n\/\/ \u7f16\u8bd1&#xff1a;gcc mq_reader.c -o mq_reader -lrt<br \/>\n\/\/ \u8fd0\u884c&#xff1a;\u5148\u8fd0\u884c .\/mq_writer&#xff0c;\u518d\u8fd0\u884c .\/mq_reader<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;mqueue.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;errno.h&gt;<\/p>\n<p>\/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u914d\u7f6e\u53c2\u6570&#xff08;\u5fc5\u987b\u4e0e writer \u4e00\u81f4&#xff09; &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n#define MQ_NAME     &#034;\/my_message_queue&#034;<br \/>\n#define MAX_MSG_SIZE 256<br \/>\n\/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>typedef struct {<br \/>\n    int type;<br \/>\n    char data[256];<br \/>\n} msg_t;<\/p>\n<p>int main() {<br \/>\n    printf(&#034;   \u8bfb\u8fdb\u7a0b\u7b49\u5f85 1 \u79d2&#xff0c;\u786e\u4fdd\u5199\u8fdb\u7a0b\u5148\u521b\u5efa\u961f\u5217&#8230;\\\\n&#034;);<br \/>\n    sleep(1);<\/p>\n<p>    printf(&#034;\\\\n&#061;&#061;&#061; [READER] \u6d88\u606f\u961f\u5217\u8bfb\u8fdb\u7a0b\u542f\u52a8 (PID&#061;%d) &#061;&#061;&#061;\\\\n&#034;, getpid());<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 1: \u6253\u5f00\u5df2\u5b58\u5728\u7684\u6d88\u606f\u961f\u5217&#xff08;\u53ea\u8bfb\u6a21\u5f0f&#xff09;<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    mqd_t mq &#061; mq_open(MQ_NAME, O_RDONLY);<br \/>\n    if (mq &#061;&#061; (mqd_t)-1) {<br \/>\n        perror(&#034;\u274c mq_open \u6253\u5f00\u6d88\u606f\u961f\u5217\u5931\u8d25&#xff08;\u786e\u8ba4\u5199\u8fdb\u7a0b\u5df2\u8fd0\u884c&#xff09;&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<br \/>\n    printf(&#034;[1] mq_open \u6253\u5f00\u6d88\u606f\u961f\u5217\u6210\u529f: %s, mqd&#061;%d\\\\n&#034;, MQ_NAME, mq);<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 2: \u83b7\u53d6\u961f\u5217\u5c5e\u6027&#xff08;\u9a8c\u8bc1\u914d\u7f6e&#xff09;<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    struct mq_attr attr;<br \/>\n    if (mq_getattr(mq, &amp;attr) &#061;&#061; -1) {<br \/>\n        perror(&#034;\u274c mq_getattr \u5931\u8d25&#034;);<br \/>\n    } else {<br \/>\n        printf(&#034;[2] \u961f\u5217\u72b6\u6001: \u5f53\u524d\u6d88\u606f\u6570&#061;%ld, \u6700\u5927\u6d88\u606f\u6570&#061;%ld, \u6d88\u606f\u5927\u5c0f&#061;%ld\\\\n&#034;,<br \/>\n               attr.mq_curmsgs, attr.mq_maxmsg, attr.mq_msgsize);<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 3: \u5faa\u73af\u63a5\u6536\u6d88\u606f&#xff08;\u81ea\u52a8\u963b\u585e\u76f4\u5230\u6d88\u606f\u5230\u8fbe&#xff09;<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    msg_t msg;<br \/>\n    unsigned int prio;<br \/>\n    ssize_t bytes_read;<\/p>\n<p>    printf(&#034;[3] \u5f00\u59cb\u63a5\u6536\u6d88\u606f&#xff08;\u963b\u585e\u6a21\u5f0f&#xff09;&#8230;\\\\n&#034;);<br \/>\n    while (1) {<br \/>\n        \/\/  \u7b2c\u4e09\u4e2a\u53c2\u6570\u5fc5\u987b &gt;&#061; mq_msgsize&#xff0c;\u5426\u5219\u53ef\u80fd\u622a\u65ad<br \/>\n        bytes_read &#061; mq_receive(mq, (char*)&amp;msg, MAX_MSG_SIZE, &amp;prio);<\/p>\n<p>        if (bytes_read &#061;&#061; -1) {<br \/>\n            if (errno &#061;&#061; EINTR) continue; \/\/ \u88ab\u4fe1\u53f7\u4e2d\u65ad&#xff0c;\u91cd\u8bd5<br \/>\n            perror(&#034;\u274c mq_receive \u5931\u8d25&#034;);<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        printf(&#034;[3.%ld]  \u6536\u5230\u6d88\u606f (priority&#061;%u, type&#061;%d): \\\\&#034;%s\\\\&#034;\\\\n&#034;,<br \/>\n               bytes_read, prio, msg.type, msg.data);<\/p>\n<p>        \/\/ \u68c0\u67e5\u7ec8\u6b62\u4fe1\u53f7<br \/>\n        if (msg.type &#061;&#061; 999 &amp;&amp; strcmp(msg.data, &#034;TERMINATE&#034;) &#061;&#061; 0) {<br \/>\n            printf(&#034;   [!] \u6536\u5230\u7ec8\u6b62\u4fe1\u53f7&#xff0c;\u9000\u51fa\u5faa\u73af&#8230;\\\\n&#034;);<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        sleep(1); \/\/ \u6a21\u62df\u5904\u7406\u5ef6\u8fdf<br \/>\n    }<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    \/\/ \u6b65\u9aa4 4: \u5173\u95ed\u6d88\u606f\u961f\u5217\u63cf\u8ff0\u7b26<br \/>\n    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n    if (mq_close(mq) &#061;&#061; -1) {<br \/>\n        perror(&#034;\u274c mq_close \u5931\u8d25&#034;);<br \/>\n    }<br \/>\n    printf(&#034;[4] mq_close \u5173\u95ed\u6d88\u606f\u961f\u5217\u63cf\u8ff0\u7b26\u6210\u529f\\\\n&#034;);<\/p>\n<p>    printf(&#034;&#061;&#061;&#061; [READER] \u8bfb\u8fdb\u7a0b\u6b63\u5e38\u9000\u51fa &#061;&#061;&#061;\\\\n\\\\n&#034;);<br \/>\n    return 0;<br \/>\n} <\/p>\n<h3>1.4 \u63a5\u53e3<\/h3>\n<table>\n<tr>\n<p>\u51fd\u6570<\/p>\n<p>\u4f5c\u7528<\/p>\n<p>\u5173\u952e\u53c2\u6570\u8bf4\u660e<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>mq_open(name, flags, mode, attr)<\/p>\n<\/td>\n<td>\n<p>\u521b\u5efa\/\u6253\u5f00\u961f\u5217<\/p>\n<\/td>\n<td>attr \u4ec5\u521b\u5efa\u65f6\u6709\u6548&#xff1b;O_NONBLOCK \u8bbe\u975e\u963b\u585e<\/td>\n<\/tr>\n<tr>\n<td>\n<p>mq_send(mqd, msg, len, prio)<\/p>\n<\/td>\n<td>\n<p>\u53d1\u9001\u6d88\u606f<\/p>\n<\/td>\n<td>\n<p>prio \u8303\u56f4 0~&#096;MQ_PRIO_MAX-1&#096;&#xff08;\u901a\u5e3832767&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>mq_receive(mqd, buf, size, prio)<\/p>\n<\/td>\n<td>\n<p>\u63a5\u6536\u6d88\u606f<\/p>\n<\/td>\n<td>\n<p>prio \u8fd4\u56de\u6d88\u606f\u4f18\u5148\u7ea7&#xff1b;\u963b\u585e\u76f4\u5230\u6709\u6d88\u606f<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>mq_getattr(mqd, attr)<\/p>\n<\/td>\n<td>\n<p>\u83b7\u53d6\u961f\u5217\u72b6\u6001<\/p>\n<\/td>\n<td>\n<p>attr.mq_curmsgs \u67e5\u770b\u5f53\u524d\u6d88\u606f\u6570<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>mq_setattr(mqd, new, old)<\/p>\n<\/td>\n<td>\n<p>\u8bbe\u7f6e\/\u83b7\u53d6\u5c5e\u6027<\/p>\n<\/td>\n<td>\u4ec5 mq_flags \u53ef\u4fee\u6539&#xff08;\u5207\u6362\u963b\u585e\u6a21\u5f0f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\n<p>mq_close(mqd)<\/p>\n<\/td>\n<td>\n<p>\u5173\u95ed\u63cf\u8ff0\u7b26<\/p>\n<\/td>\n<td>\n<p>\u8fdb\u7a0b\u9000\u51fa\u524d\u5fc5\u987b\u8c03\u7528<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>mq_unlink(name)<\/p>\n<\/td>\n<td>\n<p>\u5220\u9664\u961f\u5217\u540d\u79f0<\/p>\n<\/td>\n<td>\n<p>\u4ec5\u521b\u5efa\u8005\u8c03\u7528&#xff1b;\u5b9e\u9645\u9500\u6bc1\u9700\u6240\u6709\u8fdb\u7a0b close<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>1.5 \u6d41\u7a0b<\/h3>\n<p>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502                   POSIX \u6d88\u606f\u961f\u5217\u901a\u4fe1\u6d41\u7a0b&#xff08;\u963b\u585e\u6a21\u5f0f&#xff09;                 \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>  &#x1f7e6; \u5199\u8fdb\u7a0b (Sender)                          &#x1f7e9; \u8bfb\u8fdb\u7a0b (Receiver)<br \/>\n  \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500                         \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n         \u2502                                          \u2502<br \/>\n         \u25bc                                          \u25bc<br \/>\n  [1] mq_open(CREAT)                        [1] mq_open(OPEN)<br \/>\n      (\u521b\u5efa\u961f\u5217&#xff0c;\u8bbe max&#061;10)                     (\u6253\u5f00\u5df2\u6709\u961f\u5217)<br \/>\n         \u2502                                          \u2502<br \/>\n         \u2502                                          \u25bc<br \/>\n         \u2502                                   [2] mq_getattr<br \/>\n         \u2502                                      (\u67e5\u770b\u961f\u5217\u72b6\u6001)<br \/>\n         \u2502                                          \u2502<br \/>\n         \u25bc                                          \u2502<br \/>\n  [2] mq_send(msg1)  \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25ba  [3] mq_receive()<br \/>\n      (\u53d1\u9001\u6d88\u606f)       \u3010\u5185\u6838\u81ea\u52a8\u5165\u961f\u3011            (\u963b\u585e\u7b49\u5f85&#8230;)<br \/>\n         \u2502                   \u2502                      \u2502<br \/>\n         \u2502                   \u25bc                      \u2502<br \/>\n         \u2502            \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510              \u2502<br \/>\n         \u2502            \u2502 \u6d88\u606f\u961f\u5217     \u2502              \u2502<br \/>\n         \u2502            \u2502 [msg1]       \u2502\u25c4\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n         \u2502            \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n         \u2502                   \u2502<br \/>\n         \u25bc                   \u25bc<br \/>\n  [3] mq_send(msg2)  \u2500\u2500\u2500\u25ba  [msg1,msg2]  \u2500\u2500\u2500\u25ba  mq_receive() \u8fd4\u56de msg1<br \/>\n         \u2502                                          \u2502<br \/>\n         \u2502                                          \u25bc<br \/>\n         \u2502                                   \u5904\u7406 msg1 (sleep 1s)<br \/>\n         \u2502                                          \u2502<br \/>\n         \u25bc                                          \u25bc<br \/>\n  [4] mq_send(TERM)  \u2500\u2500\u2500\u25ba  [msg2,TERM]  \u2500\u2500\u2500\u25ba  mq_receive() \u8fd4\u56de msg2<br \/>\n         \u2502                                          \u2502<br \/>\n         \u2502                                          \u25bc<br \/>\n         \u2502                                   \u5904\u7406 msg2<br \/>\n         \u2502                                          \u2502<br \/>\n         \u2502                                          \u25bc<br \/>\n         \u2502                                   mq_receive() \u8fd4\u56de TERM<br \/>\n         \u2502                                          \u2502<br \/>\n         \u25bc                                          \u25bc<br \/>\n  [5] mq_close                              [4] \u68c0\u6d4b\u5230\u7ec8\u6b62\u4fe1\u53f7<br \/>\n         \u2502                                          \u2502<br \/>\n         \u25bc                                          \u25bc<br \/>\n  [6] mq_unlink (\u6807\u8bb0\u5220\u9664)                  [5] mq_close<br \/>\n         \u2502                                          \u2502<br \/>\n         \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                            \u25bc<br \/>\n                    \u5185\u6838\u56de\u6536\u8d44\u6e90<br \/>\n          (\u5f53\u6240\u6709\u8fdb\u7a0b mq_close \u540e\u81ea\u52a8\u9500\u6bc1) <\/p>\n<h2>2. \u539f\u7406<\/h2>\n<h3>2.1 \u751f\u547d\u5468\u671f<\/h3>\n<p>\u4e0e\u6709\u540d\u4fe1\u53f7\u91cf\u548c\u5171\u4eab\u5185\u5b58\u4e00\u6837&#xff0c;POSIX \u6d88\u606f\u961f\u5217\u662f &#034;\u4e00\u5207\u7686\u6587\u4ef6&#034;\u00a0\u7684\u7406\u5ff5&#xff1a;<\/p>\n<ul>\n<li>\u6302\u8f7d\u70b9&#xff1a;\/dev\/mqueue&#xff08;tmpfs \u6587\u4ef6\u7cfb\u7edf&#xff09;<\/li>\n<li>\u5185\u6838\u5bf9\u8c61&#xff1a;\u6bcf\u4e2a\u961f\u5217\u5bf9\u5e94\u4e00\u4e2a inode&#xff0c;\u5b58\u50a8\u5728\u5185\u5b58\u4e2d<\/li>\n<li>\u7528\u6237\u6001\u63a5\u53e3&#xff1a;mqd_t\u00a0\u672c\u8d28\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;<\/li>\n<\/ul>\n<table>\n<tr>\n<p>\u64cd\u4f5c<\/p>\n<p>POSIX \u6d88\u606f\u961f\u5217<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>\u521b\u5efa<\/p>\n<\/td>\n<td>mq_open(O_CREAT)<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6807\u8bb0\u5220\u9664<\/p>\n<\/td>\n<td>\n<p>mq_unlink()&#xff08;\u7acb\u5373\u79fb\u9664\u540d\u79f0&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u5b9e\u9645\u9500\u6bc1<\/p>\n<\/td>\n<td>\n<p>\u6240\u6709\u8fdb\u7a0b mq_close() \u540e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u8fdb\u7a0b\u5d29\u6e83<\/p>\n<\/td>\n<td>\n<p>\u5185\u6838\u81ea\u52a8 mq_close()<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6b8b\u7559\u98ce\u9669<\/p>\n<\/td>\n<td>\n<p>\u672a mq_unlink\u00a0\u4f1a\u6b8b\u7559\u6587\u4ef6<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u521b\u5efa\u8005\u8fdb\u7a0b\u5728 mq_open()\u00a0\u6210\u529f\u540e\u5e94\u7acb\u5373\u8c03\u7528\u00a0mq_unlink()&#xff0c;\u786e\u4fdd\u8d44\u6e90\u81ea\u52a8\u56de\u6536\u3002<\/p>\n<p>\u540c\u7406&#xff0c;\u5171\u4eab\u5185\u5b58\u548c\u4fe1\u53f7\u91cf\u4e5f\u5e94\u8fd9\u6837\u5b9e\u73b0&#xff0c;unlink \u4ec5\u5207\u65ad\u65b0\u8bbf\u95ee\u8def\u5f84&#xff0c;\u4e0d\u5f71\u54cd\u5df2\u6709\u5f15\u7528\u3002\u8d44\u6e90\u9500\u6bc1 &#061; unlink \u6807\u8bb0 &#043; \u5f15\u7528\u8ba1\u6570\u5f52\u96f6<\/p>\n<p>mqd_t mq &#061; mq_open(&#034;\/myq&#034;, O_CREAT | O_RDWR, 0664, &amp;attr);<br \/>\nmq_unlink(&#034;\/myq&#034;);  \/\/ \u7acb\u5373\u6807\u8bb0\u5220\u9664&#xff0c;\u4f46\u961f\u5217\u4ecd\u53ef\u7528<br \/>\n\/\/ &#8230; \u6b63\u5e38\u901a\u4fe1 &#8230;<br \/>\nmq_close(mq);       \/\/ \u6700\u540e\u4e00\u4e2a close \u89e6\u53d1\u5185\u6838\u9500\u6bc1 <\/p>\n<h3>2.2 \u963b\u585e\/\u975e\u963b\u585e<\/h3>\n<p>\/\/ \u521d\u59cb\u521b\u5efa\u4e3a\u963b\u585e\u6a21\u5f0f<br \/>\nmqd_t mq &#061; mq_open(&#034;\/myq&#034;, O_RDWR | O_CREAT, 0664, &amp;attr);<\/p>\n<p>\/\/ \u8fd0\u884c\u65f6\u5207\u6362\u4e3a\u975e\u963b\u585e<br \/>\nstruct mq_attr new_attr &#061; {0}, old_attr;<br \/>\nnew_attr.mq_flags &#061; O_NONBLOCK;<br \/>\nmq_setattr(mq, &amp;new_attr, &amp;old_attr);<\/p>\n<p>\/\/ \u975e\u963b\u585e\u63a5\u6536&#xff08;\u65e0\u6d88\u606f\u7acb\u5373\u8fd4\u56de EAGAIN&#xff09;<br \/>\nssize_t n &#061; mq_receive(mq, buf, size, NULL);<br \/>\nif (n &#061;&#061; -1 &amp;&amp; errno &#061;&#061; EAGAIN) {<br \/>\n    printf(&#034;\u65e0\u6d88\u606f\u53ef\u7528&#xff0c;\u7ee7\u7eed\u5176\u4ed6\u5de5\u4f5c&#8230;\\\\n&#034;);<br \/>\n} <\/p>\n<h3>2.3 \u4f18\u5148\u7ea7<\/h3>\n<h4>2.3.1 \u539f\u7406<\/h4>\n<table>\n<tr>\n<p>\u9879\u76ee<\/p>\n<p>\u8bf4\u660e<\/p>\n<p>\u7ec6\u8282<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td>\n<p>\u4f18\u5148\u7ea7\u8303\u56f4<\/p>\n<\/td>\n<td>\n<p>0 \u5230 MQ_PRIO_MAX &#8211; 1<\/p>\n<\/td>\n<td>\n<p>\u9ed8\u8ba4 MQ_PRIO_MAX &#061; 32768&#xff08;\u5373 0~32767&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6570\u503c<\/p>\n<\/td>\n<td>\n<p>\u6570\u503c\u8d8a\u5927&#xff0c;\u4f18\u5148\u7ea7\u8d8a\u9ad8<\/p>\n<\/td>\n<td>\n<p>prio&#061;32767 \u662f\u6700\u9ad8\u4f18\u5148\u7ea7&#xff0c;prio&#061;0 \u662f\u6700\u4f4e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u63a5\u6536\u987a\u5e8f<\/p>\n<\/td>\n<td>\n<p>\u603b\u662f\u8fd4\u56de\u5f53\u524d\u6700\u9ad8\u4f18\u5148\u7ea7\u6d88\u606f<\/p>\n<\/td>\n<td>\n<p>\u4e0e\u53d1\u9001\u987a\u5e8f\u65e0\u5173&#xff0c;\u5b8c\u5168\u7531\u4f18\u5148\u7ea7\u51b3\u5b9a<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u540c\u4f18\u5148\u7ea7<\/p>\n<\/td>\n<td>\n<p>FIFO \u987a\u5e8f&#xff08;\u5148\u53d1\u9001\u5148\u63a5\u6536&#xff09;<\/p>\n<\/td>\n<td>\n<p>\u5185\u6838\u7ea2\u9ed1\u6811\u4e2d\u540c\u4f18\u5148\u7ea7\u6d88\u606f\u6309\u65f6\u95f4\u6233\u6392\u5e8f<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u53d1\u9001\u63a5\u53e3<\/p>\n<\/td>\n<td>\n<p>mq_send(mqd, msg, len, prio)<\/p>\n<\/td>\n<td>\n<p>prio\u00a0\u662f\u72ec\u7acb\u53c2\u6570&#xff0c;\u4e0d\u5360\u7528\u6d88\u606f\u4f53\u7a7a\u95f4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u63a5\u6536\u63a5\u53e3<\/p>\n<\/td>\n<td>\n<p>mq_receive(mqd, buf, size, &amp;prio)<\/p>\n<\/td>\n<td>\n<p>\u53ef\u9009\u83b7\u53d6\u5b9e\u9645\u4f18\u5148\u7ea7\u503c<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6d88\u606f\u961f\u5217\u7ea2\u9ed1\u6811&#xff08;\u6309 priority \u964d\u5e8f\u6392\u5217&#xff09;&#xff1a;<br \/>\n          [prio&#061;10]  \u2190 \u6839\u8282\u70b9&#xff08;\u975e\u6700\u5927\u503c&#xff09;<br \/>\n         \/         \\\\<br \/>\n   [prio&#061;5]      [prio&#061;15]  \u2190 \u6700\u53f3\u8282\u70b9 &#061; \u6700\u9ad8\u4f18\u5148\u7ea7<br \/>\n      \/   \\\\           \\\\<br \/>\n[prio&#061;3] [prio&#061;7]  [prio&#061;20] \u2190 mq_receive() \u6bcf\u6b21\u53d6\u6b64\u8282\u70b9<\/p>\n<p>\u63a5\u6536\u6d41\u7a0b&#xff1a;<br \/>\n1. mq_receive() \u2192 rb_last() \u2192 \u627e\u5230 prio&#061;20 \u8282\u70b9<br \/>\n2. \u5220\u9664\u8be5\u8282\u70b9 \u2192 \u91cd\u65b0\u5e73\u8861\u7ea2\u9ed1\u6811<br \/>\n3. \u4e0b\u6b21\u63a5\u6536 \u2192 rb_last() \u2192 prio&#061;15 \u8282\u70b9 <\/p>\n<ul>\n<li>\u63d2\u5165&#xff08;mq_send&#xff09;&#xff1a;O(log n)<\/li>\n<li>\u5220\u9664&#xff08;mq_receive&#xff09;&#xff1a;O(log n)<\/li>\n<li>\u67e5\u627e\u6700\u5927\u503c&#xff1a;O(1)&#xff08;\u901a\u8fc7 rb_last\u00a0\u76f4\u63a5\u5b9a\u4f4d\u6700\u53f3\u8282\u70b9&#xff09;<\/li>\n<\/ul>\n<h4>2.3.2 \u907f\u514d\u4f18\u5148\u7ea7\u53cd\u8f6c<\/h4>\n<p>\/\/ \u274c \u53cd\u6a21\u5f0f&#xff1a;\u9ad8\u4f18\u5148\u7ea7\u6d88\u606f\u4f9d\u8d56\u4f4e\u4f18\u5148\u7ea7\u8d44\u6e90<br \/>\nmq_send(high_prio_mq, &#034;\u9700\u8981\u9501A&#034;, 32767); \/\/ \u9ad8\u4f18\u5148\u7ea7\u6d88\u606f<br \/>\n\/\/ \u4f46\u5904\u7406\u8be5\u6d88\u606f\u9700\u7b49\u5f85\u88ab\u4f4e\u4f18\u5148\u7ea7\u8fdb\u7a0b\u6301\u6709\u7684\u9501 \u2192 \u4f18\u5148\u7ea7\u53cd\u8f6c<\/p>\n<p>\/\/ \u2705 \u6b63\u786e\u6a21\u5f0f&#xff1a;\u8d44\u6e90\u9694\u79bb &#043; \u4e13\u7528\u961f\u5217<br \/>\nmqd_t emergency_mq &#061; create_queue(&#034;\/emergency&#034;, 10, 256); \/\/ \u4e13\u7528\u9ad8\u4f18\u5148\u7ea7\u961f\u5217<br \/>\nmqd_t normal_mq   &#061; create_queue(&#034;\/normal&#034;,   100, 256); \/\/ \u666e\u901a\u961f\u5217<\/p>\n<p>\/\/ \u7d27\u6025\u6d88\u606f\u8d70\u4e13\u7528\u901a\u9053&#xff0c;\u4e0d\u4e0e\u666e\u901a\u6d88\u606f\u7ade\u4e89<br \/>\nif (is_emergency(cmd))<br \/>\n    mq_send(emergency_mq, cmd, len, 32767);<br \/>\nelse<br \/>\n    mq_send(normal_mq, cmd, len, 10000); <\/p>\n<p>\u539f\u5219&#xff1a;\u9ad8\u4f18\u5148\u7ea7\u6d88\u606f\u5fc5\u987b\u6709\u72ec\u7acb\u8d44\u6e90\u901a\u9053&#xff0c;\u907f\u514d\u4e0e\u4f4e\u4f18\u5148\u7ea7\u6d88\u606f\u5171\u4eab\u961f\u5217\u5bfc\u81f4\u9690\u5f0f\u963b\u585e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u00a0\u6d88\u606f\u961f\u5217\u4f7f\u7528\u6d41\u7a0b<br \/>\n1.1 \u6982\u5ff5<br \/>\n\u6d88\u606f\u961f\u5217\u662f\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;IPC&#xff09;\u4e2d\u81ea\u5e26\u540c\u6b65\u673a\u5236\u7684\u901a\u4fe1\u65b9\u5f0f&#xff0c;\u5929\u7136\u907f\u514d\u7ade\u6001\u6761\u4ef6&#xff0c;mq_receive() \u4f1a\u963b\u585e\u76f4\u5230\u6d88\u606f\u5230\u8fbe&#xff0c;\u65e0\u9700\u989d\u5916\u540c\u6b65\u539f\u8bed\u3002 \u7279\u6027  \u5171\u4eab\u5185\u5b58  \u6d88\u606f\u961f\u5217  \u6570\u636e\u6a21\u578b  \u65e0\u7ed3\u6784\u7684\u5b57\u8282\u6d41  \u6709\u8fb9\u754c\u7684\u6d88\u606f&#xff08;\u81ea\u52a8\u5206\u5305&#xff09;  \u540c\u6b65\u673a\u5236  \u9700\u989d\u5916\u4fe1\u53f7\u91cf\/\u9501  \u5185\u7f6e\u963b\u585e\/\u5524\u9192\u673a\u5236  \u6570\u636e\u6301\u4e45\u6027  \u8fdb\u7a0b\u9000\u51fa\u540e\u6570\u636e\u4fdd\u7559  \u6d88\u606f\u5728\u63a5\u6536\u524d\u6301\u4e45\u5b58\u5728<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,174,235],"topic":[],"class_list":{"0":"post-79998","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-server","7":"tag-c","9":"tag-235"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/79998.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"1.\u00a0\u6d88\u606f\u961f\u5217\u4f7f\u7528\u6d41\u7a0b 1.1 \u6982\u5ff5 \u6d88\u606f\u961f\u5217\u662f\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;IPC&#xff09;\u4e2d\u81ea\u5e26\u540c\u6b65\u673a\u5236\u7684\u901a\u4fe1\u65b9\u5f0f&#xff0c;\u5929\u7136\u907f\u514d\u7ade\u6001\u6761\u4ef6&#xff0c;mq_receive() \u4f1a\u963b\u585e\u76f4\u5230\u6d88\u606f\u5230\u8fbe&#xff0c;\u65e0\u9700\u989d\u5916\u540c\u6b65\u539f\u8bed\u3002 \u7279\u6027 \u5171\u4eab\u5185\u5b58 \u6d88\u606f\u961f\u5217 \u6570\u636e\u6a21\u578b \u65e0\u7ed3\u6784\u7684\u5b57\u8282\u6d41 \u6709\u8fb9\u754c\u7684\u6d88\u606f&#xff08;\u81ea\u52a8\u5206\u5305&#xff09; \u540c\u6b65\u673a\u5236 \u9700\u989d\u5916\u4fe1\u53f7\u91cf\/\u9501 \u5185\u7f6e\u963b\u585e\/\u5524\u9192\u673a\u5236 \u6570\u636e\u6301\u4e45\u6027 \u8fdb\u7a0b\u9000\u51fa\u540e\u6570\u636e\u4fdd\u7559 \u6d88\u606f\u5728\u63a5\u6536\u524d\u6301\u4e45\u5b58\u5728\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/79998.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-03T15:56:54+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79998.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/79998.html\",\"name\":\"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-03T15:56:54+00:00\",\"dateModified\":\"2026-03-03T15:56:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/79998.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/79998.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79998.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/79998.html","og_locale":"zh_CN","og_type":"article","og_title":"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"1.\u00a0\u6d88\u606f\u961f\u5217\u4f7f\u7528\u6d41\u7a0b 1.1 \u6982\u5ff5 \u6d88\u606f\u961f\u5217\u662f\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff08;IPC&#xff09;\u4e2d\u81ea\u5e26\u540c\u6b65\u673a\u5236\u7684\u901a\u4fe1\u65b9\u5f0f&#xff0c;\u5929\u7136\u907f\u514d\u7ade\u6001\u6761\u4ef6&#xff0c;mq_receive() \u4f1a\u963b\u585e\u76f4\u5230\u6d88\u606f\u5230\u8fbe&#xff0c;\u65e0\u9700\u989d\u5916\u540c\u6b65\u539f\u8bed\u3002 \u7279\u6027 \u5171\u4eab\u5185\u5b58 \u6d88\u606f\u961f\u5217 \u6570\u636e\u6a21\u578b \u65e0\u7ed3\u6784\u7684\u5b57\u8282\u6d41 \u6709\u8fb9\u754c\u7684\u6d88\u606f&#xff08;\u81ea\u52a8\u5206\u5305&#xff09; \u540c\u6b65\u673a\u5236 \u9700\u989d\u5916\u4fe1\u53f7\u91cf\/\u9501 \u5185\u7f6e\u963b\u585e\/\u5524\u9192\u673a\u5236 \u6570\u636e\u6301\u4e45\u6027 \u8fdb\u7a0b\u9000\u51fa\u540e\u6570\u636e\u4fdd\u7559 \u6d88\u606f\u5728\u63a5\u6536\u524d\u6301\u4e45\u5b58\u5728","og_url":"https:\/\/www.wsisp.com\/helps\/79998.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-03T15:56:54+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/79998.html","url":"https:\/\/www.wsisp.com\/helps\/79998.html","name":"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-03T15:56:54+00:00","dateModified":"2026-03-03T15:56:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/79998.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/79998.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/79998.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u901a\u4fe1\uff1a(9) \u6d88\u606f\u961f\u5217\u4e0e\u4f18\u5148\u7ea7\u53cd\u8f6c"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=79998"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79998\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=79998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=79998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=79998"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=79998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}