{"id":1088,"date":"2024-07-28T23:52:32","date_gmt":"2024-07-28T15:52:32","guid":{"rendered":"http:\/\/blog.chxzking.cn\/?p=1088"},"modified":"2024-07-31T09:08:21","modified_gmt":"2024-07-31T01:08:21","slug":"linux%e4%b8%8b%e7%9a%84%e8%bf%9b%e7%a8%8b%e9%97%b4%e7%9a%84%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"http:\/\/blog.chxzking.cn\/index.php\/2024\/07\/28\/linux%e4%b8%8b%e7%9a%84%e8%bf%9b%e7%a8%8b%e9%97%b4%e7%9a%84%e9%80%9a%e4%bf%a1\/","title":{"rendered":"Linux\u4e0b\u7684\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><kbd>\u524d\u8a00<\/kbd><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fdb\u7a0b\u901a\u4fe1\uff08Inter-Process Communication\uff0cIPC\uff09\u662f\u6307\u5728\u4e0d\u540c\u7684\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u548c\u5171\u4eab\u4fe1\u606f\u7684\u673a\u5236\u3002\u8fdb\u7a0b\u901a\u4fe1\u662f\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u4e00\u79cd\u670d\u52a1\uff0c\u5b83\u5141\u8bb8\u8fd0\u884c\u5728\u540c\u4e00\u53f0\u8ba1\u7b97\u673a\u4e0a\u7684\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fdb\u7a0b\u901a\u4fe1\u7684\u4e3b\u8981\u4f5c\u7528\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4fe1\u606f\u5171\u4eab<\/strong>\uff1a\u591a\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7\u8fdb\u7a0b\u901a\u4fe1\u6765\u5171\u4eab\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u751f\u6210\u4e00\u4e9b\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u8fdb\u7a0b\u901a\u4fe1\u5c06\u8fd9\u4e9b\u6570\u636e\u53d1\u9001\u7ed9\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u8fdb\u884c\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u8ba1\u7b97\u52a0\u901f<\/strong>\uff1a\u901a\u8fc7\u5c06\u4e00\u4e2a\u5927\u4efb\u52a1\u5206\u89e3\u6210\u591a\u4e2a\u5c0f\u4efb\u52a1\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5c0f\u4efb\u52a1\u5206\u914d\u7ed9\u591a\u4e2a\u8fdb\u7a0b\u8fdb\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u52a0\u901f\u8ba1\u7b97\u8fc7\u7a0b\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u88ab\u79f0\u4e3a\u5e76\u884c\u8ba1\u7b97\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u5757\u5316<\/strong>\uff1a\u901a\u8fc7\u8fdb\u7a0b\u901a\u4fe1\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5927\u578b\u8f6f\u4ef6\u7cfb\u7edf\u5206\u89e3\u6210\u591a\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u6bcf\u4e2a\u6a21\u5757\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u8fdb\u7a0b\u8fd0\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u8f6f\u4ef6\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u590d\u7528\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fdb\u7a0b\u901a\u4fe1\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\u7ba1\u9053\uff08Pipe\uff09\u3001\u6d88\u606f\u961f\u5217\uff08Message Queue\uff09\u3001\u5171\u4eab\u5185\u5b58\uff08Shared Memory\uff09\u3001\u4fe1\u53f7\uff08Signal\uff09\u3001\u5957\u63a5\u5b57\uff08Socket\uff0c\u672c\u6587\u6682\u4e0d\u8ba8\u8bba\uff09\u7b49\u3002\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><kbd>\u57fa\u7840\u901a\u4fe1<\/kbd><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ba1\u9053<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ba1\u9053\u901a\u4fe1\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8fdb\u7a0b\u901a\u4fe1\u65b9\u5f0f\uff0c\u5b83\u662f\u4e00\u79cd\u534a\u53cc\u5de5\u901a\u4fe1\u65b9\u5f0f\uff08\u5373\u6570\u636e\u53ea\u80fd\u7531\u4e00\u65b9\u6d41\u5411\u53e6\u5916\u4e00\u65b9\uff09\u3002\u56fa\u5b9a\u4e00\u65b9\u4e3a\u8bfb\u7aef\u4e00\u65b9\u4e3a\u5199\u7aef\u3002\u6b64\u5916\uff0c\u7ba1\u9053\u901a\u4fe1\u662f\u57fa\u4e8e\u6587\u4ef6\u7cfb\u7edf\u7684\u7ba1\u9053\u6587\u4ef6\uff08\u533f\u540d\u548c\u547d\u540d\u7565\u6709\u4e0d\u540c\uff09\u64cd\u4f5c\uff0c\u6240\u4ee5\u4f7f\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26\u8fdb\u884c\u8bfb\u5199\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u533f\u540d\u7ba1\u9053<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u533f\u540d\u7ba1\u9053\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u65b9\u5f0f\uff1a\u7531pipe\u8fdb\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u4e0d\u9700\u8981\u751f\u6210\u4e00\u4e2a\u771f\u5b9e\u7684\u6587\u4ef6\uff0c\u800c\u662f\u5728\u5185\u6838\u7f13\u51b2\u533a\u5212\u5206\u7684\u4e00\u4e2a\u4f2a\u6587\u4ef6\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u573a\u666f\uff1a\u533f\u540d\u7ba1\u9053\u53ea\u80fd\u5728\u5177\u6709\u4eb2\u7f18\u5173\u7cfb\uff08\u7236\u5b50\u8fdb\u7a0b\u4e0e\u5144\u5f1f\u8fdb\u7a0b\uff09\u7684\u8fdb\u7a0b\u4e4b\u95f4\u4f7f\u7528\uff0c\u6ce8\u610f\uff1a\u5b59\u5b50\u8fdb\u7a0b\u3001\u7236\u8fdb\u7a0b\u7ed3\u675f\u7b49\u7684\u60c5\u51b5\u4e0b\u662f\u4e0d\u80fd\u901a\u4fe1\u7684\u3002<\/li>\n\n\n\n<li>\u751f\u547d\u5468\u671f\uff1a\u751f\u547d\u5468\u671f\u4e0e\u8fdb\u7a0b\u4e00\u81f4\uff0c\u5f53\u901a\u4fe1\u53cc\u65b9\u8fdb\u7a0b\u7ed3\u675f\u540e\uff0c\u7ba1\u9053\u968f\u4e4b\u91ca\u653e\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">pipe\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;unistd.h&gt;\n\nint pipe(int pipefd&#91;2]);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570\uff1a<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>pipefd<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u6574\u6570\u7684\u6570\u7ec4\u3002<code>pipefd[0]<\/code>\u7528\u4e8e\u8bfb\u53d6\u6570\u636e\uff0c<code>pipefd[1]<\/code>\u7528\u4e8e\u5199\u5165\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c\uff1a<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6210\u529f\u65f6\u8fd4\u56de0\uff0c\u5e76\u5c06\u4e00\u5bf9\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u586b\u5165<code>pipefd<\/code>\u6570\u7ec4\u3002<\/li>\n\n\n\n<li>\u5931\u8d25\u65f6\u8fd4\u56de-1\uff0c\u5e76\u8bbe\u7f6e<code>errno<\/code>\u4ee5\u6307\u793a\u9519\u8bef\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;unistd.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\nint main(){\n\n    int pipefile&#91;2];\n    pipe(pipefile);\n    pid_t pid = fork();\n\n    if(pid == 0){\n        printf(\"\u8fd9\u91cc\u662f\u5b50\u8fdb\u7a0b\\n\");\n        sleep(1);\n        close(pipefile&#91;0]);\n        char* text = \"hello world\";\n        while(1){\n            write(pipefile&#91;1],text,12);\n            sleep(1);\n        }\n    }else{\n        printf(\"\u8fd9\u91cc\u662f\u7236\u8fdb\u7a0b\\n\");\n        sleep(1);\n        close(pipefile&#91;1]);\n        char text&#91;12];\n        while(1){\n            read(pipefile&#91;0],text,12);\n            printf(\"\u8f93\u51fa&gt;&gt;&gt; %s\\n\",text);\n            memset(text,0,12);\n            sleep(1);\n        }\n\n    }\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8fd9\u91cc\u662f\u7236\u8fdb\u7a0b\n\u8fd9\u91cc\u662f\u5b50\u8fdb\u7a0b\n\u8f93\u51fa&gt;&gt;&gt; hello world\n\u8f93\u51fa&gt;&gt;&gt; hello world\n\u8f93\u51fa&gt;&gt;&gt; hello world\n\u8f93\u51fa&gt;&gt;&gt; hello world\n\u8f93\u51fa&gt;&gt;&gt; hello world\n\u8f93\u51fa&gt;&gt;&gt; hello world<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6ce8\u610f\uff1a<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u533f\u540d\u7ba1\u9053\u4f7f\u7528\u4e0a\u9700\u8981\u6ce8\u610f\u7684\u70b9<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u8981\u5c06\u7ba1\u9053\u7684\u5b9a\u4e49\u5199\u5728\u8fdb\u7a0b\u521b\u5efa\u4e4b\u540e\uff0c\u5426\u5219\u4f1a\u521b\u5efa\u4e24\u4e2a\u7ba1\u9053\u3002<\/li>\n\n\n\n<li>\u6ce8\u610f\u5199\u7aef\u548c\u8bfb\u7aef\u7684\u65b9\u5411\uff0c\u5173\u95ed\u4e0d\u9700\u8981\u7684\u7aef\u53e3\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u60f3\u5b9e\u73b0\u53cc\u5411\u901a\u4fe1\u5219\u9700\u8981\u4e24\u4e2a\u7ba1\u9053\uff0c\u5c3d\u7ba1\u53ef\u4ee5\u5207\u6362\u7aef\u53e3\u8fdb\u884c\u4e0d\u540c\u65f6\u7684\u53cc\u5411\u901a\u4fe1\uff0c\u4f46\u662f\u7531\u4e8e\u8fdb\u7a0b\u6267\u884c\u4e0d\u540c\u6b65\uff0c\u5728\u4e00\u4e2a\u7ba1\u9053\u4e0a\u8fdb\u884c\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u6df7\u4e71\u6216\u6b7b\u9501\uff0c\u6240\u4ee5\u901a\u5e38\u4e0d\u63a8\u8350\u5728\u4e00\u4e2a\u7ba1\u9053\u4e0a\u8fdb\u884c\u8bfb\u5199\u6bb5\u5207\u6362\u5b9e\u73b0\u53cc\u5411\u901a\u4fe1\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u547d\u540d\u7ba1\u9053\uff08FIFO\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u533f\u540d\u7ba1\u9053\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u65b9\u5f0f\uff1a\u4f7f\u7528mkfifo\u7b49\u521b\u5efa\uff0c\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u751f\u6210\u4e00\u4e2a\u771f\u6b63\u7684\u7279\u6b8a\u6587\u4ef6<\/li>\n\n\n\n<li>\u4f7f\u7528\u573a\u666f\uff1a\u53ef\u4ee5\u5728\u4efb\u610f\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1<\/li>\n\n\n\n<li>\u751f\u547d\u5468\u671f\uff1a\u5373\u4fbf\u8fdb\u7a0b\u7ed3\u675f\uff0c\u7ba1\u9053\u4f9d\u65e7\u5b58\u5728\uff0c\u76f4\u5230\u88ab\u663e\u5f0f\u5220\u9664<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u547d\u540d\u7ba1\u9053\u662f\u57fa\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u7cfb\u7edf\u800c\u751f\u6210\u7684\u7ba1\u9053\u6587\u4ef6\u8fdb\u884c\u7684\u901a\u4fe1\uff0c\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u8fdb\u7a0b\u5c06\u6d88\u606f\u5199\u5165\u4e00\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\uff0c\u4ece\u800c\u5230\u8fbe\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002\u6545\u800c\u901a\u4fe1\u53ef\u4ee5\u5728\u4efb\u610f\u8fdb\u7a0b\u4e4b\u95f4\u8fdb\u884c\uff0c\u4f46\u662f\u4ee3\u4ef7\u5c31\u662f\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u5bf9\u4e8e\u547d\u540d\u7ba1\u9053\u7684\u64cd\u4f5c\uff0c\u5c31\u662f\u5bf9\u6587\u4ef6\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">mkfifo\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/stat.h&gt;\n\nint mkfifo(const char *pathname, mode_t mode);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570\uff1a<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>pathname<\/code><\/strong>\uff1a\u6709\u540d\u7ba1\u9053\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/li>\n\n\n\n<li><strong><code>mode<\/code><\/strong>\uff1a\u6587\u4ef6\u6743\u9650\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6210\u529f\u65f6\u8fd4\u56de0\u3002<\/li>\n\n\n\n<li>\u5931\u8d25\u65f6\u8fd4\u56de-1\uff0c\u5e76\u8bbe\u7f6e<code>errno<\/code>\u4ee5\u6307\u793a\u9519\u8bef\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h>\n#include &lt;sys\/stat.h>\n#include &lt;stdio.h>\n#include &lt;unistd.h>\n#include &lt;fcntl.h>\nint main(){\n     if(mkfifo(\".\/fifofile\",0666)){\n          printf(\"\u7ba1\u9053\u6587\u4ef6\u521b\u5efa\u5931\u8d25\\n\");\n          return 0;\n     }\n     int fd = open(\"fifofile\",O_RDWR);\n     pid_t pid = fork();\n     if(pid==0){\n          char* text=\"abc\";\n          write(fd,text,4);\n          printf(\"\u5b50\u8fdb\u7a0b\u5199\u5165\u5b8c\u6bd5\\n\");\n          close(fd);\n          return 0;\n\n     }else {\n          sleep(1);\n          char text&#91;4];\n          read(fd,text,4);\n          printf(\"\u7236\u8fdb\u7a0b\u8bfb\u53d6\uff1a%s\\n\",text);\n          close(fd);\n          wait(NULL); \/\/ \u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\n          return 0;\n     }\n\n     return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5b50\u8fdb\u7a0b\u5199\u5165\u5b8c\u6bd5\n\u7236\u8fdb\u7a0b\u8bfb\u53d6\uff1aabc<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u7ba1\u9053\u8bfb\u53d6\u65f6\u6570\u636e\u4e3a\u7a7a\uff0c\u90a3\u4e48\u7a0b\u5e8f\u4f1a\u963b\u585e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4fe1\u53f7<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>kill<\/code><\/strong>\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5728\u6307\u5b9a\u7684\u8fdb\u7a0b\u6216\u8005\u8fdb\u7a0b\u7ec4\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h>\n#include &lt;signal.h>\n\nint kill(pid_t pid, int sig);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pid\uff1a<\/strong><code>pid<\/code>\u662f\u8fdb\u7a0bID\uff0c\u4e0d\u8fc7\u6709\u4e0d\u540c\u7684\u6a21\u5f0f\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c<code>pid<\/code>> 0\uff0c\u90a3\u4e48\u4fe1\u53f7\u5c06\u88ab\u53d1\u9001\u5230<code>pid<\/code>\u6307\u5b9a\u7684\u8fdb\u7a0b\u3002<\/li>\n\n\n\n<li>\u5982\u679c<code>pid<\/code>== 0\uff0c\u90a3\u4e48\u4fe1\u53f7\u5c06\u88ab\u53d1\u9001\u5230\u4e0e\u53d1\u9001\u8fdb\u7a0b\u5728\u540c\u4e00\u8fdb\u7a0b\u7ec4\u7684\u6240\u6709\u8fdb\u7a0b\u3002<\/li>\n\n\n\n<li>\u5982\u679c<code>pid<\/code>== -1\uff0c\u90a3\u4e48\u4fe1\u53f7\u5c06\u88ab\u53d1\u9001\u5230\u53d1\u9001\u8fdb\u7a0b\u6709\u6743\u9650\u53d1\u9001\u4fe1\u53f7\u7684\u6240\u6709\u8fdb\u7a0b\uff0c\u9664\u4e86init\u8fdb\u7a0b\uff08\u8fdb\u7a0b\u53f7\u4e3a1\uff09\u548c\u8c03\u7528<code>kill<\/code>\u7684\u8fdb\u7a0b\u81ea\u8eab\u3002\u4e5f\u5c31\u662f\u8c01\u53ef\u4ee5\u88ab\u6211\u53d1\u9001\u4fe1\u53f7\uff0c\u90a3\u6211\u5c31\u53d1\u9001\u7ed9\u5b83<\/li>\n\n\n\n<li>\u5982\u679c<code>pid<\/code>&lt; -1\uff0c\u90a3\u4e48\u4fe1\u53f7\u5c06\u88ab\u53d1\u9001\u5230\u8fdb\u7a0b\u7ec4ID\u7b49\u4e8e<code>pid<\/code>\u7edd\u5bf9\u503c\u7684\u6240\u6709\u8fdb\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>sig\uff1a<\/strong>\u4fe1\u53f7\uff0c\u4ee5\u4e0b\u662f\u5e38\u7528\u7684\u4fe1\u53f7\uff1a\n<ul class=\"wp-block-list\">\n<li><strong>SIGHUP\uff081\uff09<\/strong>\uff1a\u8fd9\u4e2a\u4fe1\u53f7\u901a\u5e38\u8868\u793a\u7ec8\u7aef\u5df2\u7ecf\u65ad\u5f00\u8fde\u63a5\u3002\u8bb8\u591a\u5b88\u62a4\u8fdb\u7a0b\u4f1a\u5728\u63a5\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u540e\u91cd\u65b0\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>SIGINT\uff082\uff09<\/strong>\uff1a\u5f53\u60a8\u5728\u7ec8\u7aef\u4e2d\u6309\u4e0bCtrl+C\u65f6\uff0c\u524d\u53f0\u8fdb\u7a0b\u4f1a\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u3002\u901a\u5e38\u8fd9\u4f1a\u7ed3\u675f\u8fdb\u7a0b\uff0c\u4f46\u8fdb\u7a0b\u53ef\u4ee5\u9009\u62e9\u5ffd\u7565\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><strong>SIGQUIT\uff083\uff09<\/strong>\uff1a\u8fd9\u4e2a\u4fe1\u53f7\u7c7b\u4f3c\u4e8eSIGINT\uff0c\u4f46\u5b83\u8fd8\u4f1a\u5bfc\u81f4\u8fdb\u7a0b\u5728\u7ed3\u675f\u65f6\u751f\u6210\u4e00\u4e2a\u6838\u5fc3\u8f6c\u50a8\u6587\u4ef6\uff0c\u4ee5\u4fbf\u540e\u7eed\u8fdb\u884c\u8c03\u8bd5\u3002<\/li>\n\n\n\n<li><strong>SIGILL\uff084\uff09<\/strong>\uff1a\u5f53\u8fdb\u7a0b\u5c1d\u8bd5\u6267\u884c\u4e00\u4e2a\u975e\u6cd5\u6307\u4ee4\u65f6\uff0c\u4f1a\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u3002\u8fd9\u901a\u5e38\u662f\u7531\u4e8e\u7a0b\u5e8f\u9519\u8bef\u6216\u6570\u636e\u635f\u574f\u5f15\u8d77\u7684\u3002<\/li>\n\n\n\n<li><strong>SIGFPE\uff088\uff09<\/strong>\uff1a\u5f53\u53d1\u751f\u6570\u5b66\u9519\u8bef\u6216\u6d6e\u70b9\u8fd0\u7b97\u9519\u8bef\u65f6\uff0c\u5982\u9664\u4ee5\u96f6\u6216\u6ea2\u51fa\uff0c\u8fdb\u7a0b\u4f1a\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><strong>SIGKILL\uff089\uff09<\/strong>\uff1a\u8fd9\u4e2a\u4fe1\u53f7\u4f1a\u7acb\u5373\u7ed3\u675f\u8fdb\u7a0b\uff0c\u8fdb\u7a0b\u65e0\u6cd5\u6355\u83b7\u8fd9\u4e2a\u4fe1\u53f7\u8fdb\u884c\u6e05\u7406\u5de5\u4f5c\u3002\u8fd9\u4e2a\u4fe1\u53f7\u901a\u5e38\u53ea\u5728<code>SIGTERM<\/code>\u65e0\u6548\u65f6\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>SIGALRM\uff0814\uff09<\/strong>\uff1a\u8fd9\u4e2a\u4fe1\u53f7\u901a\u5e38\u7531<code>alarm<\/code>\u51fd\u6570\u751f\u6210\uff0c\u7528\u4e8e\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u901a\u77e5\u8fdb\u7a0b\u3002<\/li>\n\n\n\n<li><strong>SIGSTOP\uff0817,19,23\uff09<\/strong>\uff1a\u8fd9\u4e2a\u4fe1\u53f7\u4f1a\u6682\u505c\u8fdb\u7a0b\u7684\u6267\u884c\u3002\u8fdb\u7a0b\u65e0\u6cd5\u5ffd\u7565\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><strong>SIGTSTP\uff0818,20,24\uff09<\/strong>\uff1a\u5f53\u60a8\u5728\u7ec8\u7aef\u4e2d\u6309\u4e0bCtrl+Z\u65f6\uff0c\u524d\u53f0\u8fdb\u7a0b\u4f1a\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u3002\u8fd9\u4f1a\u4f7f\u8fdb\u7a0b\u505c\u6b62\u6267\u884c\uff0c\u4f46\u8fdb\u7a0b\u53ef\u4ee5\u9009\u62e9\u5ffd\u7565\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><strong>SIGCHLD\uff0817,18,20\uff09<\/strong>\uff1a\u5f53\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u505c\u6b62\u6216\u7ed3\u675f\u65f6\uff0c\u5176\u7236\u8fdb\u7a0b\u4f1a\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><strong>SIGABRT\uff086\uff09<\/strong>\uff1a\u8fd9\u4e2a\u4fe1\u53f7\u901a\u5e38\u7531<code>abort<\/code>\u51fd\u6570\u751f\u6210\uff0c\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u4e25\u91cd\u7684\u7a0b\u5e8f\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u51fd\u6570\u6210\u529f\u65f6\u8fd4\u56de0<\/li>\n\n\n\n<li>\u5931\u8d25\u65f6\u8fd4\u56de-1\uff0c\u5e76\u8bbe\u7f6e<code>errno<\/code>\u4ee5\u6307\u793a\u9519\u8bef\u539f\u56e0<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>raise<\/code><\/strong>\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u7ed9\u5f53\u524d\u8fdb\u7a0b\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;signal.h>\n\nint raise(int sig);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>sig\uff1a<\/strong>\u4fe1\u53f7\uff0c\u4e0ekill\u51fd\u6570\u7684\u53c2\u6570\u5b8c\u5168\u4e00\u81f4<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>raise<\/code>\u51fd\u6570\u5728\u6210\u529f\u65f6\u8fd4\u56de0<\/li>\n\n\n\n<li>\u5728\u5931\u8d25\u65f6\u8fd4\u56de\u975e\u96f6\u503c<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>signal<\/code><\/strong>\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>signal<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u7528\u4e8e\u8bbe\u7f6e\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u5e93\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> #include &lt;signal.h>\n\nvoid (*signal(int sig, void (*func)(int)))(int);\n\u6216\u8005\ntypedef void (*sighandler_t)(int);\nsighandler_t signal(int signum, sighandler_t handler);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u8bb2\u89e3\u4e00\u4e0b\u7b2c\u4e00\u79cd\u5f62\u5f0f\uff0c\u8fd9\u662f\u4e00\u4e2a\u8fd4\u56de\u503c\u4e3a\u51fd\u6570\u6307\u9488\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u62c6\u5728\u5916\u58f3\uff1a<code>void (* )(int)<\/code> \u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u8fd4\u56de\u503c\u7684\u51fd\u6570\u6307\u9488\u7684\u771f\u6b63\u6837\u8c8c\u3002\u7136\u540e\u770b\u4e00\u770b\u5185\u90e8<code>signal(int sig, void (*func)(int)) <\/code>\u8fd9\u4e2a\u5f88\u660e\u663e\uff0c\u5b83\u662f\u4e00\u4e2a\u51fd\u6570\u660e\u548c\u5f62\u53c2\u7684\u5b9a\u4e49\uff0c\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f<code>int<\/code> ,\u53e6\u4e00\u4e2a\u662f<code>void (*)(int)<\/code>\u6307\u9488\u3002\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u662f\u7b2c\u4e00\u79cd\u5f62\u5f0f\u7684\u53e6\u4e00\u79cd\u7b80\u5355\u7684\u5199\u6cd5\uff0c\u4e3a\u4e86\u4fbf\u4e8e\u7406\u89e3\u800c\u91cd\u5b9a\u4e49\u4e86\u51fd\u6570\u6307\u9488\uff0c\u6240\u4ee5\u7b2c\u4e8c\u79cd\u5f62\u5f0f\u8981\u6e05\u6670\u5f97\u591a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>sig\/signum :<\/strong>\u8fd9\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u7279\u5b9a\u7684\u4fe1\u53f7\u7f16\u53f7\u3002\u4f8b\u5982\uff0cSIGINT\u00a0\u8868\u793a\u4e2d\u65ad\u4fe1\u53f7\uff0c\u901a\u5e38\u7531\u7528\u6237\u6309\u4e0b Ctrl+C \u89e6\u53d1\u3002<\/li>\n\n\n\n<li><strong>func\/handler:<\/strong>\u8fd9\u4e2a\u53c2\u6570\u6709\u4e24\u4e2a\u53ef\u9009\u53c2\u6570\u548c\u81ea\u5b9a\u4e49\u5904\u7406\u51fd\u6570\uff1a\n<ul class=\"wp-block-list\">\n<li><code>SIG_IGN<\/code>\uff1a\u5ffd\u7565\u8be5\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><code>SIG_DFL<\/code>\uff1a\u6062\u590d\u8be5\u4fe1\u53f7\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002<\/li>\n\n\n\n<li>\u81ea\u5b9a\u4e49\u51fd\u6570\uff1a\u5f53\u4fe1\u53f7\u53d1\u751f\u65f6\uff0c\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>signal<\/code>\u00a0\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff0c\u7c7b\u578b\u4e3a\u00a0<code>sighandler_t<\/code>\u3002\u8fd9\u4e2a\u8fd4\u56de\u503c\u662f\u4e4b\u524d\u4e3a\u8be5\u4fe1\u53f7\u8bbe\u7f6e\u7684\u5904\u7406\u51fd\u6570\u3002\u5982\u679c\u8c03\u7528\u00a0<code>signal<\/code>\u00a0\u51fd\u6570\u4e4b\u524d\u6ca1\u6709\u4e3a\u8be5\u4fe1\u53f7\u8bbe\u7f6e\u8fc7\u5904\u7406\u51fd\u6570\uff0c\u90a3\u4e48\u00a0<code>signal<\/code>\u00a0\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c06\u662f\u00a0<code>SIG_DFL<\/code>\uff0c\u8868\u793a\u8be5\u4fe1\u53f7\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><code>signal<\/code>\u00a0\u51fd\u6570\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\uff0c\u90a3\u4e48\u5b83\u4f1a\u8fd4\u56de\u00a0<code>SIG_ERR<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>sigaction<\/code><\/strong>\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u91cd\u5b9a\u4e49\u4e00\u4e2a\u4fe1\u53f7\u5728\u5f53\u524d\u8fdb\u7a0b\u4e2d\u7684\u90e8\u5206\u4fe1\u53f7\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5e76\u975e\u6240\u6709\u7684\u4fe1\u53f7\u90fd\u53ef\u4ee5\u88ab\u6355\u83b7\u548c\u5904\u7406\u3002\u6709\u4e9b\u4fe1\u53f7\uff0c\u6bd4\u5982\u00a0<code>SIGKILL<\/code>\u00a0\u548c\u00a0<code>SIGSTOP<\/code>\uff0c\u662f\u4e0d\u80fd\u88ab\u6355\u83b7\u3001\u963b\u585e\u6216\u5ffd\u7565\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;signal.h>\n\nint sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>int signum<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u7279\u5b9a\u7684\u4fe1\u53f7\u7f16\u53f7\u3002\u4f8b\u5982\uff0c<code>SIGINT<\/code>\u00a0\u8868\u793a\u4e2d\u65ad\u4fe1\u53f7\uff0c\u901a\u5e38\u7531\u7528\u6237\u6309\u4e0b Ctrl+C \u89e6\u53d1\u3002<\/li>\n\n\n\n<li><code>const struct sigaction *act<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u6307\u5411\u00a0<code>sigaction<\/code>\u00a0\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8be5\u7ed3\u6784\u5b9a\u4e49\u4e86\u65b0\u7684\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u3002\u8fd9\u4e2a\u7ed3\u6784\u5305\u542b\u4e86\u5904\u7406\u51fd\u6570\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u9009\u9879\u548c\u6807\u5fd7\u3002<\/li>\n\n\n\n<li><code>struct sigaction *oldact<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u6307\u5411\u00a0<code>sigaction<\/code>\u00a0\u7ed3\u6784\u7684\u6307\u9488\uff0c<code>sigaction<\/code>\u00a0\u51fd\u6570\u4f1a\u5c06\u65e7\u7684\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u5b58\u50a8\u5728\u8fd9\u91cc\u3002\u5982\u679c\u4f60\u4e0d\u5173\u5fc3\u65e7\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a\u00a0<code>NULL<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><code>struct sigaction<\/code>\u7ed3\u6784\u4f53\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct sigaction {\n    void     (*sa_handler)(int);\/\/sa_handler \u662f\u4e00\u4e2a\u6307\u5411\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7684\u6307\u9488\uff0c\u548c signal \u51fd\u6570\u4e2d\u7684\u5904\u7406\u51fd\u6570\u7c7b\u4f3c\n    void     (*sa_sigaction)(int, siginfo_t *, void *);\/\/sa_sigaction \u662f\u53e6\u4e00\u4e2a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u63a5\u6536\u66f4\u591a\u7684\u4fe1\u606f\u3002\n    sigset_t   sa_mask;\/\/sa_mask \u5b9a\u4e49\u4e86\u4e00\u4e2a\u4fe1\u53f7\u96c6\uff0c\u5728\u5904\u7406\u51fd\u6570\u8fd0\u884c\u671f\u95f4\uff0c\u8fd9\u4e9b\u4fe1\u53f7\u5c06\u88ab\u963b\u585e\u3002\n    int        sa_flags;\/\/sa_flags \u7528\u4e8e\u4fee\u6539\u989d\u5916\u7684\u884c\u4e3a\u3002\n    void     (*sa_restorer)(void);\/\/sigaction \u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6574\u6570\u3002\u5982\u679c\u51fd\u6570\u6210\u529f\uff0c\u8fd4\u56de\u503c\u4e3a 0\uff1b\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sigaction<\/code>\u00a0\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6574\u6570\u3002\u5982\u679c\u51fd\u6570\u6210\u529f\uff0c\u8fd4\u56de\u503c\u4e3a 0<\/li>\n\n\n\n<li>\u5982\u679c\u51fd\u6570\u5931\u8d25\uff0c\u8fd4\u56de\u503c\u4e3a -1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>pause<\/code><\/strong>\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u8c03\u7528\u00a0<code>pause<\/code>\u00a0\u51fd\u6570\u65f6\uff0c\u4f60\u7684\u8fdb\u7a0b\u4f1a\u88ab\u6302\u8d77\uff0c\u76f4\u5230\u63a5\u6536\u5230\u4e00\u4e2a\u4fe1\u53f7\u3002\u88ab\u00a0<code>pause<\/code>\u00a0\u6682\u505c\u7684\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7\u63a5\u6536\u5230\u4e00\u4e2a\u4fe1\u53f7\u6765\u5524\u9192\u3002\u8fd9\u4e2a\u4fe1\u53f7\u53ef\u4ee5\u662f\u4efb\u4f55\u4fe1\u53f7\uff0c\u53ea\u8981\u8fd9\u4e2a\u4fe1\u53f7\u80fd\u591f\u88ab\u8fdb\u7a0b\u63a5\u6536\u5230\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;unistd.h>\nint pause(void);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pause<\/code>\u00a0\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6574\u6570\u3002\u7531\u4e8e\u00a0<code>pause<\/code>\u00a0\u51fd\u6570\u53ea\u5728\u63a5\u6536\u5230\u4fe1\u53f7\u540e\u8fd4\u56de\uff0c\u6240\u4ee5\u5b83\u603b\u662f\u8fd4\u56de -1\uff0c\u5e76\u8bbe\u7f6e\u00a0<code>errno<\/code>\u00a0\u4e3a\u00a0<code>EINTR<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><code>alarm<\/code><\/strong>\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u662f\u4e00\u4e2a\u5b9a\u65f6\u51fd\u6570\uff0c\u5f53\u5b9a\u65f6\u5668\u5230\u8fbe\u8bbe\u5b9a\u7684\u65f6\u95f4\u540e\uff0c\u7cfb\u7edf\u4f1a\u5411\u8fdb\u7a0b\u53d1\u9001\u4e00\u4e2a\u00a0<code>SIGALRM<\/code>\u00a0\u4fe1\u53f7\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u5b9a\u65f6\u5668\u3002\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u5b9a\u65f6\u5668\u8fd8\u6ca1\u6709\u89e6\u53d1\u7684\u65f6\u5019\u8c03\u7528\u4e86\u00a0<code>alarm<\/code>\u00a0\u51fd\u6570\uff0c\u90a3\u4e48\u4e4b\u524d\u7684\u5b9a\u65f6\u5668\u4f1a\u88ab\u65b0\u7684\u5b9a\u65f6\u5668\u66ff\u6362\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;unistd.h>\n\nunsigned int alarm(unsigned int seconds);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>unsigned int seconds<\/code>\uff1a\u8fd9\u662f\u4f60\u60f3\u8981\u8bbe\u7f6e\u7684\u5b9a\u65f6\u5668\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u79d2\u3002\u5f53\u8fd9\u4e2a\u5b9a\u65f6\u5668\u5230\u8fbe\u8bbe\u5b9a\u7684\u65f6\u95f4\u540e\uff0c\u7cfb\u7edf\u4f1a\u5411\u8fdb\u7a0b\u53d1\u9001\u4e00\u4e2a\u00a0<code>SIGALRM<\/code>\u00a0\u4fe1\u53f7\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>alarm<\/code>\u00a0\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u65e0\u7b26\u53f7\u6574\u6570\u3002\u5982\u679c\u4e4b\u524d\u5df2\u7ecf\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u90a3\u4e48\u00a0<code>alarm<\/code>\u00a0\u51fd\u6570\u4f1a\u8fd4\u56de\u4e4b\u524d\u7684\u5b9a\u65f6\u5668\u8ddd\u79bb\u89e6\u53d1\u8fd8\u5269\u4e0b\u7684\u65f6\u95f4\u3002\u5982\u679c\u4e4b\u524d\u6ca1\u6709\u8bbe\u7f6e\u5b9a\u65f6\u5668\uff0c\u90a3\u4e48\u00a0<code>alarm<\/code>\u00a0\u51fd\u6570\u4f1a\u8fd4\u56de 0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u9996\u5148\u8c03\u7528\u00a0<code>alarm(10);<\/code>\u00a0\u8bbe\u7f6e\u4e86\u4e00\u4e2a 10 \u79d2\u7684\u5b9a\u65f6\u5668\uff0c\u7136\u540e\u5728 4 \u79d2\u540e\uff0c\u4f60\u53c8\u8c03\u7528\u00a0<code>unsigned int remaining = alarm(5);<\/code>\u00a0\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u65b0\u7684 5 \u79d2\u7684\u5b9a\u65f6\u5668\u3002\u90a3\u4e48\u7b2c\u4e8c\u6b21\u8c03\u7528\u00a0<code>alarm<\/code>\u00a0\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de 6\uff0c\u56e0\u4e3a\u5728\u4f60\u8bbe\u7f6e\u65b0\u7684\u5b9a\u65f6\u5668\u65f6\uff0c\u4e4b\u524d\u7684\u5b9a\u65f6\u5668\u8fd8\u6709 6 \u79d2\u5c31\u8981\u89e6\u53d1\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong><kbd>System V IPC<\/kbd><\/strong><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u7f6e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ftok_function\">ftok\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">ftok\u51fd\u6570\u4e3b\u8981\u4f5c\u7528\u662f\u5c06\u5728unix\/Linux\u7cfb\u7edf\u4e2d\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u952e\u503c\uff0c\u8fd9\u4e2a\u952e\u503c\u5e38\u5e38\u88ab\u7528\u4e8eIPC\uff08\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff09\u673a\u5236\uff0c\u5982\u6d88\u606f\u961f\u5217\u3001\u4fe1\u53f7\u91cf\u6216\u5171\u4eab\u5185\u5b58\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u4fd7\u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u5229\u7528\u4e86\u6587\u4ef6\u7cfb\u7edf\u7684\u8def\u5f84\u552f\u4e00\u6027\u3001\u7d22\u5f15\u53f7\u7b49\uff0c\u7ecf\u8fc7\u7b97\u6cd5\u83b7\u53d6\u4e00\u4e2a\u552f\u4e00\u503c\uff0c\u800c\u8fd9\u6837\u7684\u503c\u5728\u5bf9\u4e8e\u9700\u8981\u786e\u4fdd\u552f\u4e00\u6027\u7684\u5f88\u591a\u673a\u5236\u4e2d\uff08\u6bd4\u5982\u5171\u4eab\u5185\u5b58\u7684\u7f16\u53f7\uff09\u975e\u5e38\u5408\u9002\uff0c\u6545\u5e38\u7528\u6765\u7528\u4e8e\u8fdb\u7a0b\u901a\u4fe1\u4f5c\u4e3akey\u503c\u83b7\u53d6\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f60\u4e0d\u80fd\u7ed1\u5b9a\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u76ee\u5f55\uff0c\u8def\u5f84\u7ed1\u5b9a\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u3001\u7edd\u5bf9\u8def\u5f84\u3001\u8def\u5f84\u7cbe\u786e\u5230\u76ee\u5f55\uff0c\u8def\u5f84\u7cbe\u786e\u5230\u6587\u4ef6\u3002\u4e3a\u5e94\u5bf9\u7ed1\u5b9a\u7684\u8def\u5f84\u76f8\u540c\u7684\u60c5\u51b5\uff0c\u8bbe\u7f6e\u4e86\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u6839\u636e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u503c\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u4e0d\u540c\u7684key\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n\nkey_t ftok(const char *pathname, int proj_id);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>pathname\u662f\u6307\u5b9a\u7684\u6587\u4ef6\u540d\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5fc5\u987b\u662f\u5b58\u5728\u7684\u800c\u4e14\u53ef\u4ee5\u8bbf\u95ee\u7684\u3002<\/li>\n\n\n\n<li>proj_id\u662f\u81ea\u5b9a\u4e49\u7684\u5b50\u5e8f\u5217\u53f7\uff0c\u5b83\u662f\u4e00\u4e2a8\u4f4d\u7684\u6574\u6570\uff0c\u5373\u8303\u56f4\u662f0~255\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6210\u529f\u7684\u8bdd\u8fd4\u56de\u8fd9\u4e2a\u5f97\u5230\u7684key\u503c\uff08key_t\u5176\u5b9e\u5c31\u662fint\uff09<\/li>\n\n\n\n<li>\u5931\u8d25\u7684\u8bdd\u8fd4\u56de-1\uff0c\u5e76\u8bbe\u7f6e\u9519\u8bef\u7801errno<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;stdio.h&gt;\n\nint main(){\n     key_t key1 = ftok(\".\/\",1);\n     key_t key2 = ftok(\".\/\",2);\n\n     printf(\"\u7b2c\u4e00\u4e2akey\u503c\uff1a%d \uff0c\u7b2c\u4e8c\u4e2akey\u503c\uff1a%d\\n\",key1,key2);\n     return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c\u4e00\u4e2akey\u503c\uff1a16976010 \uff0c\u7b2c\u4e8c\u4e2akey\u503c\uff1a33753226<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4fe1\u53f7\u91cf<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fe1\u53f7\u91cf\u7528\u4e8e\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\u548c\u901a\u4fe1\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u60f3\u8981\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u5b83\u4f1a\u68c0\u67e5\u4fe1\u53f7\u91cf\u7684\u503c\u3002\u5982\u679c\u503c\u5927\u4e8e\u96f6\uff0c\u8fdb\u7a0b\u5c31\u53ef\u4ee5\u8bbf\u95ee\u8d44\u6e90\uff0c\u5e76\u5c06\u4fe1\u53f7\u91cf\u7684\u503c\u51cf\u4e00\u3002\u5982\u679c\u4fe1\u53f7\u91cf\u7684\u503c\u4e3a\u96f6\uff0c\u8fdb\u7a0b\u5c31\u4f1a\u7b49\u5f85\uff0c\u76f4\u5230\u4fe1\u53f7\u91cf\u7684\u503c\u53d8\u4e3a\u975e\u96f6\u3002\u8fd9\u79cd\u673a\u5236\u53ef\u4ee5\u9632\u6b62\u591a\u4e2a\u8fdb\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u5171\u4eab\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u6570\u636e\u7684\u4e0d\u4e00\u81f4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u4fe1\u53f7\u91cf\u672c\u8eab\u5e76\u4e0d\u4f20\u9012\u4fe1\u606f\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u907f\u514d\u53d1\u751f\u6570\u636e\u4e0d\u4e00\u81f4<\/mark>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>semget\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>semget<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u6216\u8bbf\u95ee\u4e00\u4e2a\u4fe1\u53f7\u91cf\u96c6\uff08semaphore set\uff09\u7684\u51fd\u6570\u3002\u5b83\u5728Unix\u548cLinux\u7cfb\u7edf\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff0c\u662fSystem V IPC\uff08Inter-Process Communication\uff0c\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff09\u673a\u5236\u7684\u4e00\u90e8\u5206\u3002\u6ce8\u610f\uff1a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u4e0e\u7ebf\u7a0b\u4e2d\u7684\u4fe1\u53f7\u91cf\u4e0d\u540c\uff0c\u8fd9\u4e2a\u662f\u4fe1\u53f7\u91cf\u96c6\uff0c\u800c\u4e0d\u662f\u5355\u72ec\u7684\u4fe1\u53f7\u91cf\u3002<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b80\u5355\u6765\u8bf4\uff0c\u8fdb\u7a0b\u7684\u4fe1\u53f7\u91cf\u591a\u95f8\u5f00\u5173\uff0c\u4e00\u4e2a\u5f00\u5173\u4e0a\u6709\u5f88\u591a\u6309\u952e\uff0c\u53ef\u4ee5\u5206\u522b\u64cd\u63a7\u4e0d\u540c\u7684\u6309\u952e\u3002\u800c\u7ebf\u7a0b\u7684\u4fe1\u53f7\u91cf\u662f\u5355\u95f8\u5f00\u5173\uff0c\u4e5f\u5c31\u662f\u53ea\u6709\u4e00\u4e2a\u6309\u952e\uff0c\u4e0d\u7ba1\u8ba1\u6570\u662f\u8bbe\u7f6e\u7684\u591a\u5c11\uff0c\u6bd4\u5982\u8bbe\u7f6e100\u4e2a\u4fe1\u53f7\uff0c\u4e5f\u4e0d\u8fc7\u662f\u5728\u5355\u95f8\u7684\u591a\u7aef\u5f00\u5173\u3002\u4e0d\u8fc7\uff0c<a href=\"#semctl_func\">\u8fdb\u7a0b\u4fe1\u53f7\u91cf\u4e5f\u53ef\u4ee5\u4e3a\u5176\u4e2d\u7684\u6bcf\u4e00\u4e2a\u4fe1\u53f7\u91cf\u5355\u72ec\u8bbe\u7f6e\u521d\u59cb\u503c\uff08&#8211;&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark>\uff09\u3002<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"#semctl_func\" data-type=\"internal\" data-id=\"#semctl_func\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u6ce8\u610f\uff1a\u8be5\u51fd\u6570\u4ec5\u4ec5\u662f\u521b\u5efa\u4e00\u4e2a\u4fe1\u53f7\u96c6\uff0c\u81f3\u4e8e\u4fe1\u53f7\u96c6\u4e2d\u4fe1\u53f7\u91cf\u7684\u521d\u59cb\u503c\u5904\u4e8e\u4e3a\u5b9a\u4e49\u72b6\u6001\uff0c\u5e94\u8be5\u4f7f\u7528semctl\u8bbe\u7f6e\u4fe1\u53f7\u91cf\u7684\u521d\u59cb\u503c\u3002<\/mark>(&#8211;&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark>)<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/sem.h&gt;\n\nint semget(key_t key, int nsems, int semflg);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>key<\/code>\uff1a\u8fd9\u662f\u4e00\u4e2a\u952e\u503c\uff0c\u7528\u4e8e\u6807\u8bc6\u4e00\u4e2a\u7279\u5b9a\u7684\u4fe1\u53f7\u91cf\u96c6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570<code>ftok<\/code>\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u952e\u503c\u3002<\/li>\n\n\n\n<li><code>num_sems<\/code>\uff1a\u8fd9\u662f\u4f60\u60f3\u8981\u521b\u5efa\u6216\u8bbf\u95ee\u7684\u4fe1\u53f7\u91cf\u7684\u6570\u91cf\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5e76\u4e0d\u662f\u4fe1\u53f7\u96c6\u7684\u6570\u91cf\uff0c\u53ea\u4f1a\u751f\u6210\u4e00\u4e2a\u4fe1\u53f7\u96c6\uff0c\u8fd9\u4e2a\u53c2\u6570\u662f\u751f\u6210\u8fd9\u4e2a\u4fe1\u53f7\u96c6\u4e2d\u4fe1\u53f7\u91cf\u7684\u6570\u91cf<\/mark>\u3002<\/li>\n\n\n\n<li><code>sem_flags<\/code>\uff1a\u8fd9\u662f\u4e00\u7ec4\u6807\u5fd7\uff0c\u7528\u4e8e\u63a7\u5236\u4fe1\u53f7\u91cf\u7684\u8bbf\u95ee\u6743\u9650\u548c\u884c\u4e3a\uff0c\u5206\u4e3a\u6807\u5fd7\u4f4d\u4e0e\u6743\u9650\u4f4d\uff0c\u4e24\u8005\u4e4b\u95f4\u53ef\u4ee5\u4f7f\u7528<code> | <\/code>\u94fe\u63a5\u3002\u6ce8\u610f\uff0c\u6743\u9650\u4f4d\u662f\u5fc5\u987b\u4f7f\u7528\u7684\uff0c\u4f46\u662f\u6807\u5fd7\u4f4d\u53ef\u4ee5\u586b\u5199\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u586b\u5199\u3002\n<ul class=\"wp-block-list\">\n<li>\u6807\u5fd7\u4f4d\uff1a\u6807\u5fd7\u4f4d\u7531\u4ee5\u4e0b\u4e24\u4e2a\u53ef\u9009\u9879\u3002\u4e24\u8005\u4e4b\u95f4\u53ef\u4ee5\u4f7f\u7528<code> | <\/code>\u94fe\u63a5\u3002\n<ul class=\"wp-block-list\">\n<li><code>IPC_CREAT<\/code>\uff1a\u5982\u679c\u4fe1\u53f7\u91cf\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4fe1\u53f7\u91cf\u96c6\u3002<\/li>\n\n\n\n<li><code>IPC_EXCL<\/code>\uff1a\u8fd9\u4e2a\u6807\u5fd7\u5fc5\u987b\u548c<code>IPC_CREAT<\/code>\u4e00\u8d77\u4f7f\u7528\uff0c\u5355\u72ec\u4f7f\u7528\u6ca1\u6709\u610f\u4e49\u3002\u5f53\u8fd9\u4e24\u4e2a\u6807\u5fd7\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u5982\u679c\u4fe1\u53f7\u91cf\u96c6\u5df2\u7ecf\u5b58\u5728\uff0c<code>semget<\/code>\u51fd\u6570\u5c31\u4f1a\u5931\u8d25\u5e76\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n\n\n\n<li>\u4e0d\u586b\u5199\uff1a\u5c1d\u8bd5\u8bbf\u95ee\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u4fe1\u53f7\u91cf\uff0c\u5982\u679c\u8bbf\u95ee\u5931\u8d25\uff0c\u5219\u4f1a\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u6743\u9650\u4f4d\uff1a\u5bf9\u8bfb\u5199\u6267\u884c\u6743\u9650\u8fdb\u884c\u8bbe\u7f6e\uff0c\u4e0e\u6587\u4ef6\u76f8\u4f3c\uff0c\u5206\u4e3a\u7528\u6237\u3001\u7ec4\u3001\u5176\u4ed6\u7528\u6237\uff0c\u6bd4\u5982<code>0666<\/code>(\u6240\u6709\u7528\u6237\u5747\u53ef\u4ee5\u8bfb\u5199)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u79f0\u4e3a\u4fe1\u53f7\u91cf<code>ID\uff08semid\uff09<\/code>\uff0c\u7528\u4e8e\u540e\u7eed\u7684\u4fe1\u53f7\u91cf\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u51fd\u6570\u8c03\u7528\u5931\u8d25\uff0c\u5b83\u5c06\u8fd4\u56de-1\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>semop\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">semop\u662f\u4fe1\u53f7\u91cf\u64cd\u4f5c\u51fd\u6570\uff0c\u786e\u5207\u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u76ee\u7684\u662f\u63a7\u5236\u4e34\u754c\u533a\u7684\u8d44\u6e90\uff0c\u901a\u8fc7\u8fd9\u4e2a\u51fd\u6570\u8bbe\u7f6e\u4fe1\u53f7\u91cf\u5185\u90e8\u7684\u8ba1\u6570\u5668\uff0csemop\u53c8\u6839\u636e\u8fd9\u4e2a\u8ba1\u6570\u5668\u662f\u5426\u5f52\u96f6\u6765\u51b3\u5b9a\u662f\u5426\u5141\u8bb8\u8bbf\u95ee\u4e34\u754c\u533a\u8d44\u6e90\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u8fd9\u4e2a\u51fd\u6570\u8d77\u5230\u4e86\u63a7\u5236\u7684\u4f5c\u7528\uff0c\u5c31\u50cf\u662f\u4e00\u4e2a\u505c\u8f66\u573a\u7684\u95e8\u536b\uff0c\u5f53\u8f66\u6765\u7684\u65f6\u5019\uff0c\u4f1a\u68c0\u67e5\u505c\u8f66\u573a\u662f\u5426\u6709\u7a7a\u4f4d\uff08\u8ba1\u6570\u5668\u662f\u5426\u4e3a0\uff09\uff0c\u5982\u679c\u6709\u7a7a\u4f4d\u5219\u5141\u8bb8\u8fdb\u5165\uff0c\u5982\u679c\u6ca1\u6709\u7a7a\u4f4d\u5219\u4e0d\u5141\u8bb8\u8fdb\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/sem.h&gt;\n\nint semop(int semid, struct sembuf *sops, size_t nsops);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>semid<\/code>\uff1a\u4fe1\u53f7\u91cf\u96c6\u7684\u6807\u8bc6\u7b26\uff0c\u901a\u8fc7semget\u83b7\u53d6\u3002<\/li>\n\n\n\n<li><code>sops<\/code>\uff1a\u6307\u5411\u8fdb\u884c\u64cd\u4f5c\u7684<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u7ed3\u6784\u4f53\u6570\u7ec4<\/mark>\u7684\u5730\u5740\uff0c\u5982\u679c\u4f20\u5165\u7684\u6570\u7ec4\u5927\u4e8e1\u65f6\uff0c\u51fd\u6570\u4f1a\u6309\u7167\u6570\u7ec4\u7684\u987a\u5e8f\u8fdb\u884c\u904d\u5386\uff0c\u6ce8\u610f\uff0c\u6570\u7ec4\u7684\u987a\u5e8f\u4e0d\u9700\u8981\u548c\u5b9e\u9645\u4fe1\u53f7\u91cf\u7684\u4e00\u4e00\u5bf9\u5e94\uff0c\u6bd4\u5982\u6211\u7684\u7b2c\u4e00\u4e2a\u6570\u7ec4\u5b58\u50a8\u7684\u7ed3\u6784\u4f53\u53ef\u4ee5\u662f\u64cd\u63a7\u7b2c\u4e8c\u4e2a\u4fe1\u53f7\u7684\u4fe1\u606f\uff0c\u4e0d\u8fc7\u8981\u6ce8\u610f\uff0c\u7531\u4e8e\u51fd\u6570\u662f\u6839\u636e\u6570\u7ec4\u7684\u987a\u5e8f\u6267\u884c\uff0c\u6240\u4ee5\u8981\u786e\u4fdd\u81ea\u5df1\u586b\u5199\u7684\u7ed3\u6784\u4f53\u4f4d\u7f6e\u4f1a\u88ab\u6b63\u786e\u6267\u884c\u3002\uff08\u6848\u4f8b&#8211;&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><a href=\"#\u591a\u4e2a\u4fe1\u53f7\u60c5\u51b5\u4e0b\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u6848\u4f8b\" data-type=\"internal\" data-id=\"#\u591a\u4e2a\u4fe1\u53f7\u60c5\u51b5\u4e0b\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u6848\u4f8b\">\u8df3\u8f6c<\/a><\/mark>\uff09<\/li>\n\n\n\n<li><code>nsops<\/code>\uff1a\u5c06\u8981\u8fdb\u884c\u64cd\u4f5c\u7684\u4fe1\u53f7\u7684\u4e2a\u6570\uff0c\u5b83\u8bbe\u5b9a\u4e86\u7ed3\u6784\u4f53\u6570\u7ec4\u6267\u884c\u7684\u6700\u5927\u4e0b\u6807\uff0c\u6240\u4ee5\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u5982\u679c\u4f60\u8bbe\u7f6e\u7684\u64cd\u4f5c\u4e2a\u6570\u5927\u4e8e\u4e86\u5b9e\u9645\u7ed3\u6784\u4f53\u6570\u7ec4\u4e2a\u6570\uff0c\u4f1a\u5f15\u53d1\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u751a\u81f3\u53ef\u80fd\u5bfc\u81f4\u8fdb\u7a0b\u5d29\u6e83\u3002\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4e2d\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>sops<\/code>\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct sembuf {\n    unsigned short sem_num; \/\/ \u4fe1\u53f7\u5728\u4fe1\u53f7\u96c6\u4e2d\u7684\u7d22\u5f15\uff0c0\u4ee3\u8868\u7b2c\u4e00\u4e2a\u4fe1\u53f7\uff0c1\u4ee3\u8868\u7b2c\u4e8c\u4e2a\u4fe1\u53f7\n    short sem_op; \/\/ \u64cd\u4f5c\u7c7b\u578b\n    short sem_flg; \/\/ \u64cd\u4f5c\u6807\u5fd7\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u6784\u4f53\u53c2\u6570\u89e3\u6790\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sem_num<\/code>\uff1a\u6b32\u64cd\u4f5c\u4fe1\u53f7\u5728\u6574\u4e2a\u4fe1\u53f7\u96c6\u7684\u4f4d\u7f6e\uff0c\u5c31\u50cf\u662f\u6570\u7ec4\u7684\u4e0b\u6807\u6027\u8d28\u5dee\u4e0d\u591a\u3002\u4f46\u662f\u8981\u6ce8\u610f\u7684\u4e00\u70b9\uff1a\u4e0d\u8981\u8d8a\u754c\u8bbf\u95ee\uff0c\u6bd4\u5982\u5b9a\u4e493\u4e2a\u4fe1\u53f7\u7684\u4fe1\u53f7\u96c6\uff0c\u7d22\u5f15\u5199\u201c3\u201d\uff0c\u5b9e\u9645\u4e0a\u8bbf\u95ee\u7b2c\u56db\u4e2a\u5143\u7d20\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u51fd\u6570\u8fd4\u56de\u9519\u8bef\u503c\u3002<\/li>\n\n\n\n<li><code>sem_op<\/code>\uff1a\u8fd9\u4e2a\u53c2\u6570\u662f\u5bf9\u8d44\u6e90\u91ca\u653e\u4e0e\u5360\u7528\u7684\u63a7\u5236\uff0c\u4fe1\u53f7\u91cf\u4f1a\u6839\u636e\u8fd9\u4e2a\u503c\u7684\u6b63\u8d1f\u6027\u5bf9\u8d44\u6e90\u603b\u6570\u8fdb\u884c\u52a0\u6216\u8005\u51cf\uff0c\u8bf7\u6ce8\u610f\uff0c\u4fe1\u53f7\u91cf\u7684\u5e76\u6ca1\u6709\u786c\u6027\u7684\u4e0a\u9650\u3002\u6362\u53e5\u8bdd\u8bf4\uff1a\u4fe1\u53f7\u91cf\u5185\u90e8\u6709\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u8fd9\u4e2a\u8ba1\u6570\u5668\u7684\u521d\u59cb\u503c\u662f\u4f60\u8bbe\u7f6e\u7684\uff0c\u4f60\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u5bf9\u8fd9\u4e2a\u8ba1\u6570\u5668\u7684\u503c\u8fdb\u884c\u63a7\u5236\uff08\u6bd4\u5982\u4e00\u6b21\u6027\u52a0\u591a\u5c11\uff0c\u4e00\u6b21\u6027\u51cf\u591a\u5c11\uff09\uff0c\u5f53\u8ba1\u6570\u5668\u4e3a0\u7684\u65f6\u5019\uff08\u8ba1\u6570\u5668\u4e0d\u4e3a\u8d1f\u6570\uff09\uff0c\u8fdb\u7a0b\u963b\u585e\uff0c\u4f46\u662f\u6211\u53ef\u4ee5\u4e00\u76f4\u91ca\u653e\u8d44\u6e90\uff0c\u751a\u81f3\u8d85\u8fc7\u8bbe\u5b9a\u7684\u521d\u59cb\u503c\u3002\u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4e00\u6b21\u83b7\u53d6\u7684\u8d44\u6e90\u8d85\u8fc7\u4e86\u73b0\u6709\u8d44\u6e90\uff0c\u8fd9\u4e2a\u8fdb\u7a0b\u5c31\u4f1a\u591a\u9000\u5c11\u8865\uff0c\u5269\u4e0b\u8d44\u6e90\u7528\u5b8c\u4e86\u4e5f\u4e0d\u591f\uff0c\u5c31\u4f1a\u963b\u585e\u3002\n<ul class=\"wp-block-list\">\n<li><code>sem_op &gt; 0<\/code>\uff1a\u4fe1\u53f7\u52a0\u4e0a<code>sem_op<\/code>\u7684\u503c\uff0c\u8868\u793a\u8fdb\u7a0b\u91ca\u653e\u63a7\u5236\u7684\u8d44\u6e90\u3002\u4eba\u8bdd\uff1a\u5199\u591a\u5c11\uff0c\u5c31\u4f1a\u91ca\u653e\u591a\u5c11\u8d44\u6e90<\/li>\n\n\n\n<li><code>sem_op = 0<\/code>\uff1a\u7406\u8bba\u4e0a\u7684\u64cd\u4f5c\u662f\uff1a\u201c\u5f53\u524d\u8d44\u6e90\u4e0d\u4e3a0\uff0c\u5219\u8fdb\u7a0b\u7acb\u523b\u9677\u5165\u963b\u585e\uff0c\u5982\u679c\u8d44\u6e90\u4e3a0\uff0c\u5219\u8fdb\u7a0b\u5524\u9192\u201d\u3002\u4e0d\u8fc7\u548c\u7ed3\u6784\u4f53\u7b2c\u4e09\u4e2a\u53c2\u6570\u6709\u5173\u7cfb\uff0c\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e<code>IPC_NOWAIT<\/code>\uff0c\u5219\u8c03\u7528\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u72b6\u6001\uff0c\u76f4\u5230\u4fe1\u53f7\u91cf\u7684\u503c\u4e3a0\uff1b\u5426\u5219\u8fdb\u7a0b\u4e0d\u56de\u7761\u7720\uff0c\u76f4\u63a5\u8fd4\u56de<code>EAGAIN<\/code>\u3002<\/li>\n\n\n\n<li><code>sem_op &lt; 0<\/code>\uff1a\u4fe1\u53f7\u52a0\u4e0a<code>sem_op<\/code>\u7684\u503c\u3002\u82e5\u6ca1\u6709\u8bbe\u7f6e<code>IPC_NOWAIT<\/code>\uff0c\u5219\u8c03\u7528\u8fdb\u7a0b\u963b\u585e\uff0c\u76f4\u5230\u8d44\u6e90\u53ef\u7528\uff1b\u5426\u5219\u8fdb\u7a0b\u76f4\u63a5\u8fd4\u56de<code>EAGAIN<\/code>\u3002\u4eba\u8bdd\uff1a\u5199\u591a\u5c11\uff0c\u5c31\u5360\u591a\u5c11\uff0c\u8d44\u6e90\u591f\u5c31\u7528\uff0c\u4e0d\u591f\u5c31\u963b\u585e\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>sem_flg<\/code>\uff1a\u7528\u6765\u8bbe\u7f6e\u4fe1\u53f7\u91cf\u884c\u4e3a\uff0c\u4e09\u79cd\u5f62\u5f0f\uff0c<code>0<\/code>\u548c<code>IPC_NOWAIT<\/code>\u53ef\u4ee5\u4e0e<code>SEM_UNDO<\/code>\u8fdb\u884c|\u64cd\u4f5c\uff0c\u4f46\u662f<code>0<\/code>\u548c<code>IPC_NOWAIT<\/code>\u4e4b\u95f4\u4e0d\u53ef\u4ee5\u3002\n<ul class=\"wp-block-list\">\n<li><code>0<\/code>\uff1a\u8fd9\u662f\u9ed8\u8ba4\u503c\uff0c\u8868\u793a\u963b\u585e\u8c03\u7528\u3002\u5982\u679c\u4fe1\u53f7\u91cf\u64cd\u4f5c\u4e0d\u80fd\u7acb\u5373\u5b8c\u6210\uff08\u4f8b\u5982\uff0c\u5c1d\u8bd5\u83b7\u53d6\u7684\u8d44\u6e90\u4e0d\u8db3\uff09\uff0c\u90a3\u4e48<code>semop<\/code>\u51fd\u6570\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u64cd\u4f5c\u53ef\u4ee5\u5b8c\u6210\u3002<\/li>\n\n\n\n<li><code>IPC_NOWAIT<\/code>\uff1a\u8fd9\u4e2a\u6807\u5fd7\u8868\u793a\u975e\u963b\u585e\u8c03\u7528\u3002\u5982\u679c\u4fe1\u53f7\u91cf\u64cd\u4f5c\u4e0d\u80fd\u7acb\u5373\u5b8c\u6210\uff0c\u90a3\u4e48<code>semop<\/code>\u51fd\u6570\u4e0d\u4f1a\u963b\u585e\uff0c\u800c\u662f\u7acb\u5373\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>SEM_UNDO<\/code>\uff1a\u8fd9\u4e2a\u6807\u5fd7\u4f7f\u64cd\u4f5c\u7cfb\u7edf\u8ddf\u8e2a\u5f53\u524d\u8fdb\u7a0b\u5bf9\u8fd9\u4e2a\u4fe1\u53f7\u91cf\u7684\u4fee\u6539\u60c5\u51b5\u3002\u5982\u679c\u8fd9\u4e2a\u8fdb\u7a0b\u5728\u6ca1\u6709\u91ca\u653e\u8be5\u4fe1\u53f7\u91cf\u7684\u60c5\u51b5\u4e0b\u7ec8\u6b62\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c06\u81ea\u52a8\u91ca\u653e\u8be5\u8fdb\u7a0b\u6301\u6709\u7684\u4fe1\u53f7\u91cf\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u56e0\u8fdb\u7a0b\u5f02\u5e38\u7ec8\u6b62\u800c\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u9732\u3002\u8bb2\u4eba\u8bdd\u5c31\u662f\uff1a\u53ea\u8981\u4f60\u7684semop\u51fd\u6570\u7684\u4f7f\u7528\u4e86\u5e26\u6709\u8fd9\u4e2a\u53c2\u6570\u7684\u7ed3\u6784\u4f53\uff0c\u90a3\u4e48\u4e0d\u7ba1\u4f60\u662f\u589e\u52a0\u8fc7\u4fe1\u53f7\u91cf\uff0c\u8fd8\u662f\u5360\u7528\u8fc7\u4fe1\u53f7\u91cf\uff0c\u5728\u8fdb\u7a0b\u88ab\u7ec8\u6b62\uff08\u53ef\u4ee5\u4e3b\u52a8\u7ed3\u675f\u4e5f\u4e5f\u53ef\u4ee5\u88ab\u52a8\u7ed3\u675f\uff09\u540e\uff0c\u5b83\u90fd\u4f1a\u6062\u590d\u5230\u8fd9\u4e2a\u88absemop\u4fee\u6539\u4e4b\u524d\u7684\u6837\u5b50\uff0c\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u53ea\u4f1a\u6062\u590d\u5f53\u524dsemop\u81ea\u5df1\u7ba1\u8f96\u7684\u7ed3\u6784\u4f53\u8fdb\u884c\u8fc7\u7684\u64cd\u4f5c\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u6709\u591a\u4e2asemop,<a href=\"#sem_undo\" data-type=\"internal\" data-id=\"#sem_undo\">\u4f46\u662f\u53ea\u6709\u4e00\u4e2asemop\u4f7f\u7528\u4e86SEM_UNDO,\u53ea\u4f1a\u6062\u590d\u8fd9\u4e2a\u4f7f\u7528\u4e86SEM_UNDO\u7684\u4fe1\u53f7\u91cf\u4fee\u6539\uff08\u6848\u4f8b&#8211;&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark>\uff09\u3002<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8c03\u7528\u6210\u529f\u8fd4\u56de0\u3002<\/li>\n\n\n\n<li>\u5931\u8d25\u8fd4\u56de-1\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"\u591a\u4e2a\u4fe1\u53f7\u60c5\u51b5\u4e0b\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u6848\u4f8b\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u5728\u5b58\u5728\u591a\u4e2a\u4fe1\u53f7\u91cf\u7684\u60c5\u51b5\u4e0b\u7ed3\u6784\u4f53\u6570\u7ec4\u6848\u4f8b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    struct sembuf sops&#91;2];    \n    \/\/ \u8bbe\u7f6e\u7b2c\u4e00\u4e2a\u64cd\u4f5c\u3002\u8fd9\u5c06\u4f7f\u4fe1\u53f7\u91cf\u96c6\u4e2d\u7684\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\u7684\u503c\u589e\u52a01\n    sops&#91;0].sem_num = 0; \/\/ \u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\u7684\u7d22\u5f15\n    sops&#91;0].sem_op = 1;  \/\/ \u589e\u52a0\u64cd\u4f5c\n    sops&#91;0].sem_flg = 0; \/\/ \u64cd\u4f5c\u6807\u5fd7\n\n    \/\/ \u8bbe\u7f6e\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u3002\u8fd9\u5c06\u4f7f\u4fe1\u53f7\u91cf\u96c6\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u4fe1\u53f7\u91cf\u7684\u503c\u51cf\u5c111\n    sops&#91;1].sem_num = 1; \/\/ \u7b2c\u4e8c\u4e2a\u4fe1\u53f7\u91cf\u7684\u7d22\u5f15\n    sops&#91;1].sem_op = -1; \/\/ \u51cf\u5c11\u64cd\u4f5c\n    sops&#91;1].sem_flg = 0; \/\/ \u64cd\u4f5c\u6807\u5fd7\n\n    \/\/ \u4f7f\u7528semop\u51fd\u6570\u6267\u884c\u8fd9\u4e24\u4e2a\u64cd\u4f5c\n    if (semop(semid, sops, 2) == -1) {\n        \/\/ \u9519\u8bef\u5904\u7406\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"sem_undo\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">SEM_UNDO\u7684\u6848\u4f8b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\u64cd\u4f5c\na.sem_num = 0,               \/\/\u6307\u5b9a\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\na.sem_op = +5,               \/\/\u64cd\u4f5c\u4e3a\u589e\u52a0\u4e00\u4e2a\u4fe1\u53f7\u91cf\u7684\u503c\na.sem_flg = 0 ;\nsemop(semid,&amp;a,1);              \/\/\u6267\u884c\u4fe1\u53f7\u91cf\u64cd\u4f5c\n\/\/\u7b2c\u4e8c\u4e2a\u4fe1\u53f7\u91cf\u64cd\u4f5c\na.sem_num = 0;               \/\/\u6307\u5b9a\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\na.sem_op = +2;                \/\/\u64cd\u4f5c\u4e3a\u589e\u52a0\u4e00\u4e2a\u4fe1\u53f7\u91cf\u7684\u503c\na.sem_flg = 0 | SEM_UNDO;\n\/\/\u7b2c\u4e09\u4e2a\u4fe1\u53f7\u91cf\u64cd\u4f5c\nsemop(semid,&amp;a,1);              \/\/\u6267\u884c\u4fe1\u53f7\u91cf\u64cd\u4f5c\na.sem_num = 0,               \/\/\u6307\u5b9a\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\na.sem_op = -3,               \/\/\u64cd\u4f5c\u4e3a\u589e\u52a0\u4e00\u4e2a\u4fe1\u53f7\u91cf\u7684\u503c\na.sem_flg = 0 ;\nsemop(semid,&amp;a,1);              \/\/\u6267\u884c\u4fe1\u53f7\u91cf\u64cd\u4f5c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u521d\u59cb\u4fe1\u53f7\u91cf\u662f<code>5<\/code>\uff0c\u90a3\u4e48\u73b0\u5728\u8fd9\u4e2a\u8fdb\u7a0b\u8c03\u7528\u5bf9\u4fe1\u53f7\u91cf\u8fdb\u884c\u4e86\u5982\u4e0a\u7684\u64cd\u4f5c\u3002<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u5f53\u8fdb\u7a0b\u7ed3\u675f\u4e4b\u540e<\/mark>\uff0c\u4fe1\u53f7\u91cf\u4f1a\u53d8\u6210<code>7<\/code>\u3002\u56e0\u4e3a\u7b2c\u4e8c\u4e2a\u64cd\u4f5c\u4f7f\u7528\u4e86<code>SEM_UNDO<\/code>,\u8fdb\u7a0b\u7ed3\u675f\u540e\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u4f1a\u88ab\u6062\u590d\uff0c\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8e\u6ca1\u6709\u8fdb\u884c\u8fc7\u7b2c\u4e8c\u4e2a\u4fe1\u53f7\u91cf\u64cd\u4f5c\u3002\u5f53\u7136\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5982\u679c\u8fdb\u7a0b\u6ca1\u7ed3\u675f\uff0c\u90a3\u4e48\u5c31\u4e0d\u4f1a\u6062\u590d<\/mark>\u3002\u6ca1\u7ed3\u675f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u7ed3\u679c\u5c31\u662f<code>9<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"semctl_func\"><strong>semctl\u51fd\u6570<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u4e5f\u662f\u4fe1\u53f7\u91cf\u7684\u64cd\u4f5c\u51fd\u6570\uff0c\u4e0d\u8fc7\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u6bd4<code>semop<\/code>\u7684\u4f7f\u7528\u5c42\u6b21\u66f4\u6df1\uff0c\u786e\u5207\u6765\u8bf4\u8fd9\u4e2a\u53eb\u505a\u4fe1\u53f7\u91cf\u63a7\u5236\uff0c\u7528\u4e8e\u64cd\u4f5c\u4fe1\u53f7\u91cf\u672c\u8eab\uff0c\u800c\u4e0d\u662f\u7ba1\u7406\u57fa\u4e8e\u4fe1\u53f7\u91cf\u7684\u503c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e24\u8005\u7684\u4f5c\u7528\u5bf9\u8c61\u4e0d\u4e00\u6837\uff0c<code>semop<\/code>\u662f\u4e3a\u4e86\u4fdd\u62a4\u4e00\u4e2a\u8d44\u6e90\uff0c\u7136\u540e\u5bf9\u4fe1\u53f7\u91cf\u8fdb\u884c\u6d88\u606f\u4f20\u9012\uff0c\u963b\u585e\u6216\u8005\u5141\u8bb8\u5176\u4ed6\u8fdb\u7a0b\u8bbf\u95ee\u8d44\u6e90\uff0c\u800c<code>semctl<\/code>\u7684\u4f5c\u7528\u5bf9\u8c61\u662f\u4fe1\u53f7\u91cf\u81ea\u5df1\uff0c\u5b83\u53ef\u4ee5\u64cd\u4f5c\u4fe1\u53f7\u91cf\u81ea\u5df1\u7684\u7279\u6027\uff0c\u8bbe\u7f6e\u4fe1\u53f7\u91cf\u7684\u7279\u6027\u4ee5\u4fbf\u66f4\u597d\u7684\u5bf9\u8d44\u6e90\u8fdb\u884c\u63a7\u5236\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/sem.h&gt;\n\nint semctl(int semid, int semnum, int cmd, ...);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>semid\uff1a<\/code>\u4fe1\u53f7\u96c6\u7684\u6807\u8bc6\u7b26\uff0c\u5373\u662f\u4fe1\u53f7\u8868\u7684\u7d22\u5f15\uff0csemget\u83b7\u5f97\u3002<\/li>\n\n\n\n<li><code>semnum<\/code>\uff1a\u4fe1\u53f7\u96c6\u7684\u7d22\u5f15\uff0c\u7528\u6765\u5b58\u53d6\u4fe1\u53f7\u96c6\u5185\u7684\u67d0\u4e2a\u4fe1\u53f7\u3002<\/li>\n\n\n\n<li><code>cmd<\/code>\uff1a\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4\u3002\u5e38\u7528\u7684\u547d\u4ee4\u5982\u4e0b\uff1a\n<ul class=\"wp-block-list\">\n<li><code>IPC_STAT<\/code>\uff1a\u83b7\u53d6\u4fe1\u53f7\u91cf\u96c6\u7684\u72b6\u6001\u4fe1\u606f\u3002\uff08<mark style=\"background-color:#00d084\" class=\"has-inline-color\">\u9700\u8981<\/mark>\u5728\u53ef\u53d8\u53c2\u6570\u4f4d\u586b\u5199\u53c2\u6570\u3002\u8fd9\u4e2a\u547d\u4ee4\u9700\u8981\u4e00\u4e2a<code>semid_ds<\/code>\u7ed3\u6784\u7684\u6307\u9488\uff0c\u7528\u4e8e\u5b58\u50a8\u4fe1\u53f7\u91cf\u96c6\u7684\u72b6\u6001\u4fe1\u606f\u3002\uff09<\/li>\n\n\n\n<li><code>IPC_SET<\/code>\uff1a\u8bbe\u7f6e\u4fe1\u53f7\u91cf\u96c6\u7684\u72b6\u6001\u4fe1\u606f\u3002\uff08<mark style=\"background-color:#00d084\" class=\"has-inline-color\">\u9700\u8981<\/mark>\u5728\u53ef\u53d8\u53c2\u6570\u4f4d\u586b\u5199\u53c2\u6570\u3002\u8fd9\u4e2a\u547d\u4ee4\u9700\u8981\u4e00\u4e2a<code>semid_ds<\/code>\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8be5\u7ed3\u6784\u5305\u542b\u4e86\u4f60\u60f3\u8981\u8bbe\u7f6e\u7684\u4fe1\u53f7\u91cf\u96c6\u7684\u65b0\u72b6\u6001\u4fe1\u606f\u3002\uff09<\/li>\n\n\n\n<li><code>IPC_RMID<\/code>\uff1a\u5220\u9664\u4fe1\u53f7\u91cf\u96c6\uff0c\u5f53\u88ab\u8bbe\u7f6e\u4e3a\u8fd9\u4e2a\u547d\u4ee4\u7684\u65f6\u5019\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5b9e\u9645\u4e0a\u4f1a\u88ab\u5ffd\u7565\uff0c\u4e0d\u7ba1\u6307\u5b9a\u4ec0\u4e48\u6807\u53f7\u7684\u4fe1\u53f7\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u51fd\u6570\u90fd\u4f1a\u5220\u9664\u6574\u4e2a\u4fe1\u53f7\u96c6\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5355\u72ec\u7684\u4fe1\u53f7\u91cf<\/mark>\u3002\uff08\u8fd9\u4e2a\u547d\u4ee4<mark style=\"background-color:#00d084\" class=\"has-inline-color\">\u4e0d\u9700\u8981<\/mark>\u4efb\u4f55\u989d\u5916\u7684\u53c2\u6570\uff0c\u5b83\u4f1a\u7acb\u5373\u5220\u9664\u4fe1\u53f7\u91cf\u96c6\u3002\uff09<\/li>\n\n\n\n<li><code>GETVAL<\/code>\uff1a\u83b7\u53d6\u4fe1\u53f7\u91cf\u7684\u503c\u3002\uff08\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u8fd4\u56de\u4fe1\u53f7\u91cf\u7684\u503c\uff0c<mark style=\"background-color:#00d084\" class=\"has-inline-color\">\u4e0d\u9700\u8981<\/mark>\u4efb\u4f55\u989d\u5916\u7684\u53c2\u6570\u3002\uff09<\/li>\n\n\n\n<li><code>SETVAL<\/code>\uff1a\u8bbe\u7f6e\u4fe1\u53f7\u91cf\u7684\u503c\u3002\uff08<mark style=\"background-color:#00d084\" class=\"has-inline-color\">\u9700\u8981<\/mark>\u5728\u53ef\u53d8\u53c2\u6570\u4f4d\u586b\u5199\u53c2\u6570\uff0c\u8fd9\u4e2a\u547d\u4ee4\u9700\u8981\u4e00\u4e2a\u6574\u6570\u503c\uff0c\u4f5c\u4e3a\u65b0\u7684\u4fe1\u53f7\u91cf\u503c\u3002\uff09<\/li>\n\n\n\n<li><code>GETALL<\/code>\uff1a\u83b7\u53d6\u6240\u6709\u4fe1\u53f7\u91cf\u7684\u503c\u3002\uff08<mark style=\"background-color:#00d084\" class=\"has-inline-color\">\u9700\u8981<\/mark>\u5728\u53ef\u53d8\u53c2\u6570\u4f4d\u586b\u5199\u53c2\u6570\uff0c\u8fd9\u4e2a\u547d\u4ee4\u9700\u8981\u4e00\u4e2a\u65e0\u7b26\u53f7\u77ed\u6574\u578b\u6570\u7ec4\u7684\u6307\u9488\uff0c\u7528\u4e8e\u5b58\u50a8\u6240\u6709\u4fe1\u53f7\u91cf\u7684\u503c\u3002\uff09<\/li>\n\n\n\n<li><code>SETALL<\/code>\uff1a\u8bbe\u7f6e\u6240\u6709\u4fe1\u53f7\u91cf\u7684\u503c\u3002\uff08<mark style=\"background-color:#00d084\" class=\"has-inline-color\">\u9700\u8981<\/mark>\u5728\u53ef\u53d8\u53c2\u6570\u4f4d\u586b\u5199\u53c2\u6570\uff0c\u8fd9\u4e2a\u547d\u4ee4\u9700\u8981\u4e00\u4e2a\u65e0\u7b26\u53f7\u77ed\u6574\u578b\u6570\u7ec4\u7684\u6307\u9488\uff0c\u8be5\u6570\u7ec4\u5305\u542b\u4e86\u4f60\u60f3\u8981\u8bbe\u7f6e\u7684\u6240\u6709\u65b0\u7684\u4fe1\u53f7\u91cf\u503c\u3002\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7b2c\u56db\u4f4d\u662f\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\uff0c\u5177\u4f53\u586b\u5199\u4ec0\u4e48\uff0c\u53d6\u51b3\u4e8e<code>cmd<\/code>\u53c2\u6570\u7684\u503c\u3002\u8fd9\u4e2a\u53c2\u6570\u901a\u5e38\u662f\u4e00\u4e2a<code>semun<\/code>\u8054\u5408\u4f53\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>union semun {\n    int val;                  \/* \u7528\u4e8eSETVAL *\/\n    struct semid_ds *buf;     \/* \u7528\u4e8eIPC_STAT\u548cIPC_SET *\/\n    unsigned short *array;    \/* \u7528\u4e8eGETALL\u548cSETALL *\/\n    struct seminfo *__buf;    \/* \u7528\u4e8eIPC_INFO *\/\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6210\u529f\u6267\u884c\u65f6\uff0c\u6839\u636e\u4e0d\u540c\u7684\u547d\u4ee4\u8fd4\u56de\u4e0d\u540c\u7684\u975e\u8d1f\u503c\u3002<\/li>\n\n\n\n<li>\u5931\u8d25\u8fd4\u56de-1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7efc\u5408\u793a\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u7a0b\u5e8f1\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/sem.h&gt;\n#include &lt;unistd.h&gt;\nint main(){\n    \/\/\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u952e\u503c\n    key_t key = ftok(\"\/\",1);\n    \/\/\u521b\u5efa\u4e00\u4e2a\u4fe1\u53f7\u91cf\n    int semid = semget(key,1,0666 | IPC_CREAT | IPC_EXCL ); \n    if(semid == -1){\n        perror(\"\u51fa\u9519\uff1a\");\n        return 0;\n    }\n\n    \/\/\u5b9a\u4e49\u89c4\u8303\u7684\u5171\u7528\u4f53\n    union sumun{\n        int val;\n        struct semid_ds *buf;\n        unsigned short *array;\n        struct seminfo *__buf;\n    };\n    \/\/\u8bbe\u7f6e\u4fe1\u53f7\u91cf\u7684\u521d\u59cb\u503c\n    union sumun val = {.val = 1};\n    if(semctl(semid,0,SETVAL,val) == -1){\/\/\u6ce8\u610f\uff0c\u6709\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u770b\u5230\u7c7b\u4f3csemctl(semid,0,SETVAL,1)\u7684\u60c5\u51b5\uff0c\u5e76\u4e0d\u5efa\u8bae\u8fd9\u79cd\u5199\u6cd5\uff0c\u56e0\u4e3a\u8fd9\u5e76\u4e0d\u662f\u6807\u51c6\u7684\u5199\u6cd5\uff0c\u53ef\u80fd\u5b58\u5728\u672a\u77e5\u7684\u9519\u8bef\u3002\n        \/\/\u5982\u679c\u8bbe\u7f6e\u51fa\u9519\n        semctl(semid,0,IPC_RMID);\n        perror(\"\u51fa\u9519\uff1a\");\n        return 0;\n    }\n\n    for(int i = 0;i&lt;3;i++){\n        struct sembuf a = {\n            .sem_num = 0,               \/\/\u6307\u5b9a\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\n            .sem_op = -1,               \/\/\u64cd\u4f5c\u4e3a\u51cf\u5c11\u4e00\u4e2a\u4fe1\u53f7\u91cf\u7684\u503c\n            .sem_flg = 0 | SEM_UNDO     \/\/\u8be5\u4fe1\u53f7\u91cf\u64cd\u4f5c\u7c7b\u578b\u2014\u2014\u963b\u585e\u578b\uff0c\u8fdb\u7a0b\u7ed3\u675f\u81ea\u52a8\u6d88\u9664\u8be5\u8fdb\u7a0b\u6240\u505a\u7684\u6240\u6709\u66f4\u6539\n        };\n        semop(semid,&amp;a,1);              \/\/\u6267\u884c\u4fe1\u53f7\u91cf\u64cd\u4f5c\n        printf(\"\u7a0b\u5e8f1\u7684\u54cd\u5e94\\n\");\n    }\n    \/\/\u7b49\u5f85\u7a0b\u5e8f2\u9000\u51fa\n    sleep(5);   \n    printf(\"\u7a0b\u5e8f1\u9000\u51fa\\n\");\n    \/\/\u5220\u9664\u4fe1\u53f7\u96c6\n    semctl(semid,0,IPC_RMID);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u7a0b\u5e8f2\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/sem.h&gt;\n #include &lt;unistd.h&gt;\n\n\nint main(){\n    \/\/\u751f\u6210\u4e00\u4e2a\u952e\u503c\n    key_t key = ftok(\"\/\",1);\n    \/\/\u62d6\u5ef6\u65f6\u95f4\uff0c\u7b49\u5f85\u7a0b\u5e8f1\u521b\u5efa\u4fe1\u53f7\u96c6\n    sleep(1);\n    \/\/\u83b7\u53d6\u4e00\u4e2a\u4fe1\u53f7\u91cfid\n    int semid = semget(key,1,0666 | IPC_CREAT); \n    if(semid == -1){\n        perror(\"\u51fa\u9519\uff1a\");\n        return 0;\n    }\n\n    for(int i = 0;i&lt;3;i++){\n        printf(\"\u4efb\u610f\u952e\u76d8\u5524\u9192\u7a0b\u5e8f1&gt;&gt;&gt;\");\n        getchar();\n\n        struct sembuf a = {\n            .sem_num = 0,               \/\/\u6307\u5b9a\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\n            .sem_op = +1,               \/\/\u64cd\u4f5c\u4e3a\u589e\u52a0\u4e00\u4e2a\u4fe1\u53f7\u91cf\u7684\u503c\n            .sem_flg = 0 | SEM_UNDO     \/\/\u8be5\u4fe1\u53f7\u91cf\u64cd\u4f5c\u7c7b\u578b\u2014\u2014\u963b\u585e\u578b\uff0c\u8fdb\u7a0b\u7ed3\u675f\u81ea\u52a8\u6d88\u9664\u8be5\u8fdb\u7a0b\u6240\u505a\u7684\u6240\u6709\u66f4\u6539\n        };\n        semop(semid,&amp;a,1);              \/\/\u6267\u884c\u4fe1\u53f7\u91cf\u64cd\u4f5c\n        printf(\"\u7a0b\u5e8f2\u7684\u54cd\u5e94\\n\");\n    }\n\n    printf(\"\u7a0b\u5e8f1\u9000\u51fa\\n\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u7a0b\u5e8f1                   \/\/\u7a0b\u5e8f2\n\u7a0b\u5e8f1\u7684\u54cd\u5e94               \u4efb\u610f\u952e\u76d8\u5524\u9192\u7a0b\u5e8f1&gt;&gt;&gt;\n\u7a0b\u5e8f1\u7684\u54cd\u5e94               \u7a0b\u5e8f2\u7684\u54cd\u5e94\n\u7a0b\u5e8f1\u7684\u54cd\u5e94               \u4efb\u610f\u952e\u76d8\u5524\u9192\u7a0b\u5e8f1&gt;&gt;&gt;\n\u7a0b\u5e8f1\u9000\u51fa                 \u7a0b\u5e8f2\u7684\u54cd\u5e94  \n                          \u4efb\u610f\u952e\u76d8\u5524\u9192\u7a0b\u5e8f1&gt;&gt;&gt;\n                          \u7a0b\u5e8f2\u7684\u54cd\u5e94\n                          \u7a0b\u5e8f1\u9000\u51fa<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5171\u4eab\u5185\u5b58<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u865a\u62df\u5185\u5b58\u6280\u672f\uff0c\u5176\u4e2d\u903b\u8f91\u5185\u5b58\u901a\u8fc7\u5185\u5b58\u7ba1\u7406\u5355\u5143\uff08MMU\uff09\u6620\u5c04\u5230\u7269\u7406\u5730\u5740\u3002\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u53ef\u4ee5\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u6765\u5b9e\u73b0\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u8fd9\u662f\u4e00\u79cd\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u8bbf\u95ee\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58\u7684\u65b9\u6cd5<\/mark>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u4f1a\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6bb5\uff0c\u8be5\u6bb5\u7684\u7269\u7406\u5185\u5b58\u5730\u5740\u4f1a\u88ab\u6620\u5c04\u5230\u6240\u6709\u5171\u4eab\u8be5\u5185\u5b58\u7684\u8fdb\u7a0b\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u3002\u4e0d\u8fc7\u8981\u6ce8\u610f\uff0c\u4e0d\u540c\u8fdb\u7a0b\u865a\u62df\u5185\u5b58\u5e76\u4e0d\u4e00\u5b9a\u662f\u76f8\u540c\u7684\uff0c\u4e8b\u5b9e\u4e0a\uff0c\u5927\u6982\u7387\u662f\u4e0d\u76f8\u540c\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u6709\u8981\u6ce8\u610f\u4e00\u70b9\u662f\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u63a8\u8350\u5171\u4eab\u5185\u5b58\u8bbe\u7f6e\u4e3a<strong>4KB<\/strong>\u7684\u500d\u6570\uff0c\u5185\u5b58\u662f\u4ee5<strong>\u9875<\/strong>\u7684\u5f62\u5f0f\u8fdb\u884c\u7ba1\u7406\u7684\uff0c\u6bcf\u9875\u901a\u5e38\u4e3a<strong>4KB<\/strong>\u3002\u5f53\u6211\u4eec\u7533\u8bf7\u5185\u5b58\u65f6\uff0c\u5982\u679c\u7533\u8bf7\u7684\u5185\u5b58\u5927\u5c0f\u4e0d\u662f<strong>4KB<\/strong>\u7684\u500d\u6570\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5206\u914d\u6700\u63a5\u8fd1\u4e14\u5927\u4e8e\u7533\u8bf7\u5185\u5b58\u76844KB\u500d\u6570\u7684\u5185\u5b58\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u7533\u8bf7<strong>1KB<\/strong>\u7684\u5185\u5b58\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5b9e\u9645\u4e0a\u4f1a\u5206\u914d<strong>4KB<\/strong>\u7684\u5185\u5b58\u3002\u4f46\u662f\u4f60\u53ea\u80fd\u4f7f\u7528<strong>1KB<\/strong>\uff0c\u5269\u4f59\u7684\u7a7a\u95f4\u5c31\u51fa\u73b0\u4e86\u6d6a\u8d39\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u7531\u4e8e\u6570\u636e\u4e0d\u9700\u8981\u5728\u8fdb\u7a0b\u4e4b\u95f4\u590d\u5236\uff0c\u56e0\u6b64\u6548\u7387\u5f88\u9ad8\u3002\u7136\u800c\uff0c\u8fd9\u4e5f\u5e26\u6765\u4e86\u5e76\u53d1\u8bbf\u95ee\u7684\u95ee\u9898\uff0c\u4e3a\u4e86\u907f\u514d\u6570\u636e\u7684\u4e0d\u4e00\u81f4\uff0c\u8fdb\u7a0b\u9700\u8981\u4f7f\u7528\u67d0\u79cd\u540c\u6b65\u673a\u5236\uff08\u5982\u4fe1\u53f7\u91cf\uff09\u6765\u534f\u8c03\u5bf9\u5171\u4eab\u5185\u5b58\u7684\u8bbf\u95ee\u3002<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">shmget\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">shmget\u51fd\u6570\u662f\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\uff0c\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4e0e\u5171\u4eab\u5185\u5b58\u7ed1\u5b9a\u7684\u6807\u8bc6\u7801\uff0c\u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u7531\u4e8e\u6b64\u51fd\u6570\u901a\u5e38\u4e0eftok\u51fd\u6570\u5728\u4e00\u8d77\u4f7f\u7528\uff0c\u8fd9\u91cc\u5b58\u5728\u4e00\u4e2a\u8bef\u533a\uff1a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">ftok\u51fd\u6570\u5e76\u4e0d\u662f\u8fd4\u56de\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6807\u5fd7\u7801\uff0c\u5b83\u662f\u8fd4\u56de\u4e00\u4e2a\u5177\u6709\u552f\u4e00\u6027\u7684\u6570\u5b57\uff0c\u8fd9\u4e2a\u6570\u5b57\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u4e0e\u5171\u4eab\u5185\u5b58\u6807\u5fd7\u7801\u751f\u6210\u7684\u79cd\u5b50\uff0c<\/mark>shmget\u6839\u636e\u8fd9\u4e2a\u79cd\u5b50\u751f\u6210\u4e00\u4e2a\u4e0d\u4f1a\u53d1\u751f\u952e\u503c\u51b2\u7a81\u7684\u5171\u4eab\u5185\u5b58\u6807\u5fd7\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/shm.h&gt;\n\nint shmget(key_t key, size_t size, int shmflg);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>key\uff1a<\/strong>\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u5171\u4eab\u5185\u5b58\u6bb5\u7684ID\u3002\u5728\u901a\u5e38\u60c5\u51b5\uff0c\u4e3a\u4e86\u907f\u514d\u5171\u4eab\u5185\u5b58\u7684\u952e\u503c\u51b2\u7a81\uff0c<a href=\"#ftok_function\" data-type=\"internal\" data-id=\"#ftok_function\">\u4f1a\u4f7f\u7528ftok\u51fd\u6570\uff08&#8211;&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark>\uff09\u83b7\u53d6\u4e00\u4e2a\u552f\u4e00\u7684key<\/a>\uff0c\u5e76\u4e0d\u5efa\u8bae\u81ea\u5df1\u8bbe\u7f6e\u4e00\u4e2a\u503c\uff0c\u867d\u7136\u53ef\u884c\uff0c\u4f46\u662f\u5728\u9879\u76ee\u4ee3\u7801\u91cf\u5927\u4e86\u4e4b\u540e\uff0c\u96be\u514d\u51fa\u73b0\u952e\u503c\u51b2\u7a81\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>size\uff1a<\/strong>\u53c2\u6570\u7528\u6765\u6307\u5b9a\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5927\u5c0f\u3002<\/li>\n\n\n\n<li><strong>shmflg\uff1a<\/strong>\u53c2\u6570\u7528\u6765\u6307\u5b9a\u6743\u9650\u3001\u521b\u5efa\u548c\u6253\u5f00\u5185\u5b58\u6bb5\u7684\u65b9\u5f0f\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd9\u4e2a\u53c2\u6570\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u5206\u522b\u662f\u6743\u9650\u4f4d\u548c\u6807\u5fd7\u4f4d\u3002\u5b83\u4eec\u4e4b\u95f4\u4f7f\u7528\u201c<code> | <\/code>\u201d\u94fe\u63a5\u3002\n<ul class=\"wp-block-list\">\n<li><strong>\u6743\u9650\u4f4d<\/strong>\uff1a\u8fd9\u90e8\u5206\u5b9a\u4e49\u4e86\u4e0d\u540c\u7528\u6237\u5bf9\u5171\u4eab\u5185\u5b58\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5b83\u901a\u5e38\u7531\u4e09\u4e2a\u516b\u8fdb\u5236\u6570\u5b57\u7ec4\u6210\uff0c\u6bcf\u4e2a\u6570\u5b57\u5bf9\u5e94\u4e00\u79cd\u7528\u6237\u7c7b\u578b\u7684\u6743\u9650\uff08\u6240\u6709\u8005\u3001\u7ec4\u548c\u5176\u4ed6\uff09\u3002\u6bcf\u4e2a\u6570\u5b57\u7531\u4e09\u4f4d\u4e8c\u8fdb\u5236\u6570\u8868\u793a\uff0c\u5206\u522b\u4ee3\u8868\u8bfb\u3001\u5199\u548c\u6267\u884c\u6743\u9650\u3002\u4f8b\u5982\uff0c\u6743\u9650\u4f4d<code>0600<\/code>\u8868\u793a\u6240\u6709\u8005\u6709\u8bfb\u5199\u6743\u9650\uff0c\u800c\u7ec4\u548c\u5176\u4ed6\u7528\u6237\u6ca1\u6709\u4efb\u4f55\u6743\u9650\u3002<\/li>\n\n\n\n<li><strong>\u6807\u5fd7\u4f4d<\/strong>\uff1a\u6807\u5fd7\u4f4d\u9884\u8bbe\u4e86\u51e0\u79cd<code>shmget<\/code>\u51fd\u6570\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002\n<ul class=\"wp-block-list\">\n<li><code>IPC_CREAT<\/code>\uff1a\u5982\u679c\u5171\u4eab\u5185\u5b58\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u65b0\u7684\u5171\u4eab\u5185\u5b58\u3002\u5426\u5219\uff0c\u6253\u5f00\u5df2\u5b58\u5728\u7684\u5171\u4eab\u5185\u5b58\u3002\u8fd9\u4e2a\u4f4d\u5355\u72ec\u4f7f\u7528\u5f80\u5f80\u51fa\u73b0\u5728\u5df2\u7ecf\u5b58\u5728\u4e86\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u7684\u60c5\u51b5\u4e0b\uff0c\u5176\u4ed6\u8fdb\u7a0b\u60f3\u4e0e\u4e4b\u7ed1\u5b9a\u7684\u65f6\u5019\u3002<\/li>\n\n\n\n<li><code>IPC_EXCL<\/code>\uff1a\u4e0e<code>IPC_CREAT<\/code>\u4e00\u8d77\u4f7f\u7528\uff08<code>IPC_CREAT | IPC_EXCL<\/code>\u5373<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-cyan-blue-color\">\u5982\u679c\u5171\u4eab\u5185\u5b58\u5df2\u7ecf\u5b58\u5728\uff0c\u90a3\u4e48<code>shmget<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u9519\u8bef\u3002\u5982\u679c\u5171\u4eab\u5185\u5b58\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u5b83\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5171\u4eab\u5185\u5b58<\/mark>\uff09\uff0c\u5982\u679c\u5171\u4eab\u5185\u5b58\u5df2\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>SHM_HUGETLB<\/code>\uff1a\u521b\u5efa\u5927\u9875\u5171\u4eab\u5185\u5b58\u3002<\/li>\n\n\n\n<li><code>SHM_NORESERVE<\/code>\uff1a\u4e0d\u9884\u7559\u4ea4\u6362\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5171\u4eab\u5185\u5b58\u521b\u5efa\u6210\u529f\uff0cshmget\u5c06\u8fd4\u56de\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u5373\u8be5\u6bb5\u5171\u4eab\u5185\u5b58\u7684\u6807\u8bc6\u7801\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8c03\u7528\u5931\u8d25\uff0c\u5219\u8fd4\u56de\u201c\uff0d1\u201d\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">shmat\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">shmat\u662f\u4e00\u4e2a\u5728Unix\u548cLinux\u7cfb\u7edf\u4e2d\u7528\u4e8e\u5c06\u5171\u4eab\u5185\u5b58\u8fde\u63a5\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u542f\u52a8\u5bf9\u5df2\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u7684\u8bbf\u95ee\u3002<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u8fdb\u7a0b\u7ed3\u675f\uff0c\u90a3\u4e48\u88ab\u94fe\u63a5\u7684\u5171\u4eab\u7a7a\u95f4\u4f1a\u81ea\u52a8\u8131\u79bb\u3002<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/shm.h&gt;\n\nvoid *shmat(int shmid, const void *shmaddr, int shmflg);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>shmid\u53c2\u6570\u662f\u7531shmget\u51fd\u6570\u8fd4\u56de\u7684\u5171\u4eab\u5185\u5b58\u6807\u8bc6\u7b26\u3002<\/li>\n\n\n\n<li>shmaddr\u53c2\u6570\u662f\u6307\u5b9a\u5171\u4eab\u5185\u5b58\u8fde\u63a5\u5230\u5f53\u524d\u8fdb\u7a0b\u4e2d\u7684\u5730\u5740\u4f4d\u7f6e\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u5e0c\u671b\u81ea\u5df1\u63a7\u5236\u8fd9\u4e2a\u5171\u4eab\u5185\u5b58\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u4f4d\u7f6e\u7684\u5730\u5740\u4f20\u5165\u8fd9\u4e2a\u53c2\u6570\uff0c\u4ee5\u5b9e\u73b0\u5bf9\u5185\u5b58\u66f4\u52a0\u7cbe\u7ec6\u7684\u63a7\u5236\u3002\u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662f\uff1a\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\u6027\u8d28\u662f\u5efa\u8bae\uff1a\u4e5f\u5c31\u662f\u8bf4\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u4e00\u5b9a\u4f1a\u542c\u4ece\u4f60\u7684\u5efa\u8bae\uff0c\u56e0\u4e3a\u5f80\u5f80\u8fd8\u6709\u5176\u5b83\u7684\u65b9\u9762\u4f1a\u5f71\u54cd<\/mark>\uff0c\u6bd4\u5982\u5185\u5b58\u5bf9\u9f50\u3002\u5185\u5b58\u5bf9\u9f50\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u90a3\u4e48\u64cd\u4f5c\u7cfb\u7edf\u6700\u7ec8\u7ed9\u51fa\u7684\u5730\u5740\u4f1a\u4e0e\u4f60\u7684\u671f\u671b\u5730\u5740\u504f\u79fb\u51fa\u73b0\u504f\u79fb\uff08\u6b64\u60c5\u51b5\u662f\u7b2c\u4e09\u4e2a\u53c2\u6570\u5141\u8bb8\u504f\u79fb\uff0c\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e<code>SHM_RND<\/code>\u5219\u4f1a\u76f4\u63a5\u8fd4\u56de\u9519\u8bef\u503c\uff09\u3002\u5982\u679c\u4f60\u7684\u5730\u5740\u63a5\u4e0b\u6765\u7684\u7a7a\u95f4\u4e0d\u591f\uff0c\u90a3\u4e48\u4f1a\u8fd4\u56de\u9519\u8bef\u503c\u3002<\/li>\n\n\n\n<li>\u4e00\u5b9a\u8981\u6709\u53c2\u6570\u63a5\u6536\u51fd\u6570\u8fd4\u56de\u503c\uff1a\u56e0\u4e3a\u4f60\u7684\u5efa\u8bae\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u4e00\u5b9a\u542c\u4ece\uff0c\u6240\u4ee5\u5c31\u9700\u8981\u4e00\u4e2a\u53c2\u6570\u6839\u636e\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\u67e5\u770b\u771f\u6b63\u7684\u503c\u3002<\/li>\n\n\n\n<li>\u7f6eNULL\u9009\u9879\uff1a\u5982\u679c\u4f60\u4e0d\u5728\u610f\u5206\u914d\u7684\u5730\u5740\uff0c\u90a3\u4e48\u6b64\u53c2\u6570\u7f6eNULL\u4f1a\u5b8c\u5168\u542c\u4ece\u64cd\u4f5c\u7cfb\u7edf\u7684\u5206\u914d\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>shmflg\u53c2\u6570\u662f\u4e00\u7ec4\u6807\u5fd7\u4f4d\u3002\n<ul class=\"wp-block-list\">\n<li><code>SHM_RDONLY<\/code>\uff1a\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u9644\u52a0\u5171\u4eab\u5185\u5b58\u3002\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u8fd9\u4e2a\u6807\u5fd7\uff0c\u90a3\u4e48\u5171\u4eab\u5185\u5b58\u5c06\u4ee5\u8bfb\u5199\u65b9\u5f0f\u9644\u52a0\u3002<\/li>\n\n\n\n<li><code>SHM_RND<\/code>\uff1a\u5c06<code>shmaddr<\/code>\u53c2\u6570\u820d\u5165\u5230\u4e00\u4e2a\u5408\u9002\u7684\u8fb9\u754c\u3002\u8fd9\u4e2a\u6807\u5fd7\u901a\u5e38\u4e0e<code>shmaddr<\/code>\u53c2\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><code>SHM_REMAP<\/code>\uff1a\u5141\u8bb8\u5171\u4eab\u5185\u5b58\u9644\u52a0\u5230\u5df2\u5b58\u5728\u7684\u5185\u5b58\u6bb5\u4e0a\u3002\u8fd9\u4e2a\u6807\u5fd7\u5728Linux 2.4\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u53ef\u7528\u3002<\/li>\n\n\n\n<li>0\uff1a\u5982\u679c\u4e0d\u9700\u8981\u7279\u6b8a\u7684\u9644\u52a0\u65b9\u5f0f\uff0c\u90a3\u4e48<code>shmflg<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u4e3a0\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u8c03\u7528\u6210\u529f\uff0cshmat\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5171\u4eab\u5185\u5b58\u7b2c\u4e00\u4e2a\u5b57\u8282\u7684\u6307\u9488\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8c03\u7528\u5931\u8d25\uff0c\u5219\u8fd4\u56de <code>(void*)\uff0d1 <\/code>\u3002<mark style=\"background-color:#ff6900\" class=\"has-inline-color\">\u6ce8\u610f\uff0c\u8fd9\u4e2a\u62a5\u9519\u5e76\u4e0d\u662f\u8fd4\u56deNULL<\/mark>\uff0c\u8fd9\u91cc\u6700\u521d\u53ef\u4ee5\u8ffd\u968f\u5230unix\u65f6\u671f\uff0c\u662f\u4e00\u4e2a\u5386\u53f2\u9057\u7559\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">shmdt\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">shmdt\u662f\u4e00\u4e2a\u5728Unix\u548cLinux\u7cfb\u7edf\u4e2d\u7528\u4e8e\u5c06\u5171\u4eab\u5185\u5b58\u4ece\u5f53\u524d\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u4e2d\u5206\u79bb\u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u505c\u6b62\u5bf9\u5df2\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u7684\u8bbf\u95ee\u3002\u4e0d\u8fc7\u8981\u6ce8\u610f\u4e00\u70b9\u662f\uff1a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u8fd9\u4e2a\u51fd\u6570\u4ec5\u4ec5\u662f\u5c06\u81ea\u5df1\u4e0e\u5171\u4eab\u5185\u5b58\u8131\u79bb\uff0c\u5171\u4eab\u5185\u5b58\u7684\u8d44\u6e90\u4f9d\u65e7\u5b58\u5728\uff0c\u5b83\u65e0\u6cd5\u88ab\u81ea\u52a8\u5220\u9664\uff0c\u9700\u8981\u663e\u793a\u5220\u9664<\/mark>\uff0c\u4e5f\u5c31\u8bf4\u8fd9\u4e2a\u51fd\u6570\u53ea\u662f\u7ba1\u7406\u81ea\u5df1\u7684\u4e00\u4ea9\u4e09\u5206\u5730\uff0c\u4e0d\u4f1a\u7ba1\u7406\u5171\u4eab\u5185\u5b58\u7684\u672c\u4f53<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/shm.h&gt;\n\nint shmdt(const void *shmaddr);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>shmaddr\u662f\u6307\u5411\u5171\u4eab\u5185\u5b58\u7684\u5730\u5740<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8c03\u7528\u6210\u529f\u8fd4\u56de0<\/li>\n\n\n\n<li>\u8c03\u7528\u5931\u8d25\u8fd4\u56de-1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">shmctl\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">shmctl\u662f\u4e00\u4e2a\u5728Unix\u548cLinux\u7cfb\u7edf\u4e2d\u7528\u4e8e\u63a7\u5236\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u7684\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u72b6\u6001\u4fe1\u606f\u3001\u8bbe\u7f6e\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5c5e\u6027\uff0c\u4ee5\u53ca\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u5bf9\u5171\u4eab\u5185\u5b58\u672c\u4f53\u8fdb\u884c\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c31\u662f\u4e00\u4e2a\u64cd\u4f5c\u96c6\u5408\u3002\u5f53\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c06\u4f1a\u56de\u6536\u88ab\u7533\u8bf7\u7684\u5171\u4eab\u5185\u5b58\u7a7a\u95f4\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u6ce8\u610f\uff0c\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u5fc5\u987b\u786e\u4fdd\u6240\u6709\u7684\u5bf9\u8fd9\u4e2a\u5185\u5b58\u8bbf\u95ee\u7684\u8fdb\u7a0b\u5747\u5df2\u8131\u79bb<\/mark>\uff0c\u56e0\u4e3a\u8bbf\u95ee\u88ab\u56de\u6536\u7684\u8d44\u6e90\u53ef\u80fd\u5bfc\u81f4\u51fa\u73b0\u6570\u636e\u8986\u76d6\u3001\u6bb5\u9519\u8bef\u7b49\u3002\u5982\u679c\u4e0d\u662f\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\uff0c\u4e5f\u5fc5\u987b\u907f\u514d\u5728\u5bf9\u5171\u4eab\u5185\u5b58\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u8fdb\u7a0b\u4e0d\u8981\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/shm.h&gt;\n\nint shmctl(int shmid, int cmd, struct shmid_ds *buf);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>shmid\u53c2\u6570\u662f\u7531shmget\u51fd\u6570\u8fd4\u56de\u7684\u5171\u4eab\u5185\u5b58\u6807\u8bc6\u7b26\u3002<\/li>\n\n\n\n<li>cmd\u53c2\u6570\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u8868\u793a\u8981\u6267\u884c\u7684\u64cd\u4f5c\u3002\u5e38\u7528\u7684\u547d\u4ee4\u5305\u62ec\uff1a\n<ul class=\"wp-block-list\">\n<li>IPC_STAT\uff08\u83b7\u53d6\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u72b6\u6001\u4fe1\u606f\uff09\u3002<\/li>\n\n\n\n<li>IPC_SET\uff08\u4fee\u6539\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u5c5e\u6027\uff09\u3002\n<ul class=\"wp-block-list\">\n<li><strong>\u6743\u9650\uff08shm_perm.mode\uff09<\/strong>\uff1a\u4f60\u53ef\u4ee5\u6539\u53d8\u5171\u4eab\u5185\u5b58\u7684\u6743\u9650\uff0c\u4f8b\u5982\u8bfb\u3001\u5199\u548c\u6267\u884c\u6743\u9650\u3002\u8fd9\u53ef\u4ee5\u5f71\u54cd\u5176\u4ed6\u8fdb\u7a0b\u662f\u5426\u80fd\u591f\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u3002<\/li>\n\n\n\n<li><strong>\u6240\u6709\u8005\uff08shm_perm.uid \u548c shm_perm.gid\uff09<\/strong>\uff1a\u4f60\u53ef\u4ee5\u6539\u53d8\u5171\u4eab\u5185\u5b58\u7684\u6240\u6709\u8005\u548c\u6240\u5c5e\u7684\u7ec4\u3002\u8fd9\u4e5f\u53ef\u4ee5\u5f71\u54cd\u5176\u4ed6\u8fdb\u7a0b\u662f\u5426\u80fd\u591f\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u3002<\/li>\n\n\n\n<li><strong>\u5927\u5c0f\uff08shm_segsz\uff09<\/strong>\uff1a\u7406\u8bba\u4e0a\uff0c\u4f60\u53ef\u4ee5\u6539\u53d8\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u3002\u4f46\u662f\uff0c\u5927\u591a\u6570\u7cfb\u7edf\u4e0d\u5141\u8bb8\u5728\u521b\u5efa\u5171\u4eab\u5185\u5b58\u540e\u6539\u53d8\u5176\u5927\u5c0f\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u5927\u6216\u66f4\u5c0f\u7684\u5171\u4eab\u5185\u5b58\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5220\u9664\u65e7\u7684\u5171\u4eab\u5185\u5b58\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>IPC_RMID\uff08\u5220\u9664\u5171\u4eab\u5185\u5b58\u6bb5\uff09\u3002\u8fd9\u4e2a\u60c5\u51b5\u4e0b\u51fd\u6570\u4f1a\u5ffd\u7565\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u6240\u4ee5\u586b\u5199NULL\u5373\u53ef\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>buf\u53c2\u6570\u662f\u4e00\u4e2a\u6307\u5411struct shmid_ds\u7c7b\u578b\u7ed3\u6784\u7684\u6307\u9488\u3002\u8be5\u7ed3\u6784\u5305\u542b\u5171\u4eab\u5185\u5b58\u6bb5\u7684\u72b6\u6001\u4fe1\u606f\u3002\u6839\u636ecmd\u53c2\u6570\u7684\u4e0d\u540c\uff0cbuf\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u8f93\u5165\u6216\u8f93\u51fa<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u51fd\u6570\u6267\u884c\u6210\u529f\uff0c\u8fd4\u56de\u503c\u4e3a0\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0c\u8fd4\u56de\u503c\u4e3a-1\uff0c\u5e76\u8bbe\u7f6eerrno\u6765\u6307\u793a\u5177\u4f53\u7684\u9519\u8bef\u539f\u56e0<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7efc\u5408\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/shm.h&gt;\n#include &lt;string.h&gt;\nint main(){\n     \n     \/\/\u5f00\u8f9f\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\n     key_t key = ftok(\".\/\",1);\n     if(key == -1){\n          printf(\"\u79cd\u5b50\u83b7\u53d6\u5931\u8d25\\n\");\n          return 0;\n     }\n     int id = shmget(key,4096,0666 | IPC_CREAT | IPC_EXCL);\n     if(id == -1){\n          printf(\"\u5171\u4eab\u5185\u5b58\u7ed1\u5b9a\u5931\u8d25\\n\");\n          return 0;\n     }\n\n     \/\/\u5f00\u8f9f\u8fdb\u7a0b\n     pid_t pid = fork();\n\n\n     if(pid == 0){\n          \/\/\u5b50\u8fdb\u7a0b\n          \/\/\u7ed1\u5b9a\u5171\u4eab\u5185\u5b58\n          void* pmem = shmat(id,NULL,0);\n          if(pmem == (void*)-1){\n               printf(\"\u5b50\u8fdb\u7a0b\u94fe\u63a5\u5171\u4eab\u5185\u5b58\u5931\u8d25\\n\");\n               return 0;\n          }\n          \/\/\u53d1\u9001\u6d88\u606f\n          char* text = \"\u7231\u6765\u81ea\u5b50\u8fdb\u7a0b\";\n          strcpy(pmem,text);\n          printf(\"\u5b50\u8fdb\u7a0b\u53d1\u9001\u6570\u636e\u5b8c\u6bd5\\n\");\n          sleep(5);\n\n          \/\/\u8131\u79bb\u5171\u4eab\u5185\u5b58\n          shmdt(pmem);\n          return 0;\n\n     }else if(pid &gt; 0){\n          \/\/\u7236\u8fdb\u7a0b\n          \/\/\u7ed1\u5b9a\u5171\u4eab\u5185\u5b58\n          void* pmem = shmat(id,NULL,0);\n          if(pmem == (void*)-1){\n               printf(\"\u5b50\u8fdb\u7a0b\u94fe\u63a5\u5171\u4eab\u5185\u5b58\u5931\u8d25\\n\");\n               return 0;\n          }\n\n          \/\/\u63a5\u6536\u6d88\u606f\n          sleep(5);\n          char text&#91;50];\n          strcpy(text,pmem);\n          printf(\"\u6536\u5230\u6d88\u606f\uff0c\u6d88\u606f\u4e3a\uff1a%s\\n\",text);\n          sleep(5);\n          \/\/\u8131\u79bb\u5171\u4eab\u5185\u5b58\n          shmdt(pmem);\n          \/\/\u9500\u6bc1\u5171\u4eab\u5185\u5b58\n          shmctl(id,IPC_RMID,NULL);\n          return 0;\n     }\n     return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5b50\u8fdb\u7a0b\u53d1\u9001\u6570\u636e\u5b8c\u6bd5\n\u6536\u5230\u6d88\u606f\uff0c\u6d88\u606f\u4e3a\uff1a\u7231\u6765\u81ea\u5b50\u8fdb\u7a0b<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6d88\u606f\u961f\u5217<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Linux\u7684\u6d88\u606f\u961f\u5217\u662f\u4e00\u79cd\u5728\u4e0d\u540c\u8fdb\u7a0b\u6216\u8005\u4e0d\u540c\u7cfb\u7edf\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u673a\u5236\uff0c\u5b83\u662f\u4e00\u79cd\u5b58\u653e\u6d88\u606f\u7684\u5bb9\u5668\uff0c\u53d1\u9001\u65b9\u5411\u961f\u5217\u4e2d\u53d1\u9001\u6d88\u606f\uff0c\u63a5\u6536\u65b9\u4ece\u961f\u5217\u4e2d\u63a5\u6536\u6d88\u606f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6d88\u606f\u961f\u5217\u7684\u5b9e\u73b0\u5206\u4e3a\u4e24\u79cd\uff0c\u4e00\u79cd\u4e3a<code>System V<\/code>\u7684\u6d88\u606f\u961f\u5217\uff0c\u4e00\u79cd\u662f<code>Posix<\/code>\u6d88\u606f\u961f\u5217\u3002\u6d88\u606f\u961f\u5217\u53ef\u4ee5\u8ba4\u4e3a\u662f\u4e00\u4e2a\u6d88\u606f\u94fe\u8868\uff0c\u67d0\u4e2a\u8fdb\u7a0b\u5f80\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u4e2d\u5199\u5165\u6d88\u606f\u4e4b\u524d\uff0c\u4e0d\u9700\u8981\u53e6\u5916\u67d0\u4e2a\u8fdb\u7a0b\u5728\u8be5\u961f\u5217\u4e0a\u7b49\u5f85\u6d88\u606f\u7684\u8fbe\u5230\uff0c\u8fd9\u4e00\u70b9\u4e0e\u7ba1\u9053\u548c<code>FIFO<\/code>\u76f8\u53cd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>System V<\/code>\u6d88\u606f\u961f\u5217\u548c<code>POSIX<\/code>\u6d88\u606f\u961f\u5217\u90fd\u662f\u5728<code>Linux<\/code>\u4e2d\u7528\u4e8e\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u673a\u5236\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u663e\u8457\u7684\u5dee\u5f02\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bfb\u53d6\u6d88\u606f\u7684\u65b9\u5f0f<\/strong>\uff1a\u5bf9\u4e8e<code>POSIX<\/code>\u6d88\u606f\u961f\u5217\uff0c\u8bfb\u64cd\u4f5c\u603b\u662f\u8fd4\u56de\u961f\u5217\u4e2d\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u6700\u65e9\u6d88\u606f\u3002\u800c\u5bf9\u4e8e<code>System V<\/code>\u6d88\u606f\u961f\u5217\uff0c\u8bfb\u64cd\u4f5c\u53ef\u4ee5\u8fd4\u56de\u4efb\u610f\u6307\u5b9a\u4f18\u5148\u7ea7\u7684\u6d88\u606f\u3002<\/li>\n\n\n\n<li><strong>\u6d88\u606f\u5230\u8fbe\u7684\u901a\u77e5\u673a\u5236\uff1a<\/strong>\u5f53\u5f80\u4e00\u4e2a\u7a7a\u961f\u5217\u653e\u7f6e\u4e00\u4e2a\u6d88\u606f\u65f6\uff0c<code>POSIX<\/code>\u6d88\u606f\u961f\u5217\u5141\u8bb8\u4ea7\u751f\u4e00\u4e2a\u4fe1\u53f7\u6216\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u3002\u800c<code>System V<\/code>\u6d88\u606f\u961f\u5217\u5219\u4e0d\u63d0\u4f9b\u7c7b\u4f3c\u7684\u673a\u5236\u3002<\/li>\n\n\n\n<li><strong>\u751f\u547d\u5468\u671f\u7ba1\u7406<\/strong>\uff1a<code>POSIX<\/code>\u6d88\u606f\u961f\u5217\u662f\u5f15\u7528\u8ba1\u6570\u7684\uff0c\u53ea\u6709\u5f53\u6240\u6709\u5f53\u524d\u4f7f\u7528\u961f\u5217\u7684\u8fdb\u7a0b\u90fd\u5173\u95ed\u4e86\u4e4b\u540e\u624d\u4f1a\u5bf9\u961f\u5217\u8fdb\u7a0b\u6807\u8bb0\u4ee5\u4fbf\u5220\u9664\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7684\u6d88\u606f\u961f\u5217\u91c7\u7528\u7684\u662f\uff1a<code>System V<\/code>\u6d88\u606f\u961f\u5217<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">msgget\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u521b\u5efa\u8981\u7ed9\u6d88\u606f\u961f\u5217\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u6807\u8bc6\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/msg.h&gt;\n\nint msgget(key_t key, int msgflg);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>key\uff1a<\/code>\u8fd9\u4e2a\u53c2\u6570\u5df2\u7ecf\u662f\u8001\u670b\u53cb\u4e86\uff0c\u4f5c\u4e3a\u751f\u6210\u6d88\u606f\u961f\u5217\u7684\u552f\u4e00\u6807\u8bc6\u7801\u79cd\u5b50\uff0c\u81ea\u7136\u4f7f\u7528<code>ftok<\/code>\u51fd\u6570\u751f\u6210\u5177\u6709\u552f\u4e00\u6027\u7684id\u66f4\u4e3a\u5408\u9002\uff0c\u4e0d\u591a\u8d58\u8ff0,\u8be6\u60c5\u8bf7\u770b<a href=\"#ftok_function\">&#8211;&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark><\/a><\/li>\n\n\n\n<li><code>msgflg\uff1a<\/code>\u8fd9\u4e2a\u53c2\u6570\u867d\u7136\u540d\u79f0\u51fa\u73b0\u4e86\u53d8\u5316\uff0c\u4f46\u662f\u4e5f\u662f\u8001\u670b\u53cb\u4e86\uff0c\u5b83\u5c31\u662f\u6807\u5fd7\u4f4d\uff1a\u4e24\u79cd\u7c7b\u578b\u4e4b\u95f4\u53ef\u4ee5\u4f7f\u7528<code> | <\/code>\u94fe\u63a5\n<ul class=\"wp-block-list\">\n<li><strong>\u6807\u5fd7\u4f4d<\/strong>\uff1a\u4e24\u4e2a\u53c2\u6570\u4f7f\u7528<code> | <\/code>\u76f8\u8fde\n<ul class=\"wp-block-list\">\n<li><code>IPC_CREAT<\/code>\uff1a\u5982\u679c\u6d88\u606f\u961f\u5217\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6d88\u606f\u961f\u5217\u3002<\/li>\n\n\n\n<li><code>IPC_EXCL<\/code>\uff1a\u8fd9\u4e2a\u6807\u5fd7\u5fc5\u987b\u548c<code>IPC_CREAT<\/code>\u4e00\u8d77\u4f7f\u7528\uff0c\u5355\u72ec\u4f7f\u7528\u6ca1\u6709\u610f\u4e49\u3002\u5f53\u8fd9\u4e24\u4e2a\u6807\u5fd7\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c\u5982\u679c\u4fe1\u53f7\u91cf\u96c6\u5df2\u7ecf\u5b58\u5728\uff0c\u51fd\u6570\u5c31\u4f1a\u5931\u8d25\u5e76\u8fd4\u56de\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6743\u9650\u4f4d<\/strong>\uff1a\u4e0e\u6587\u4ef6\u6743\u9650\u4e00\u6837\uff0c\u8bfb\u5199\u6267\u884c\u4f9d\u65e7\u5bf9\u5e94\u7684\u7528\u6237\u5bf9\u8c61\uff0c\u4f9d\u65e7\u53ef\u4ee5\u91c7\u7528\u6570\u5b57\u8868\u793a\uff0c\u6bd4\u5982\u6240\u6709\u7528\u6237\u5747\u53ef\u8bfb\u5199\uff1a0666<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u64cd\u4f5c\u6210\u529f\uff0cmsgget\u5c06\u8fd4\u56de\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u5373\u8be5\u6d88\u606f\u961f\u5217\u7684\u6807\u8bc6\u7801\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u5931\u8d25\uff0c\u5219\u8fd4\u56de\u201c-1\u201d<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">msgsnd\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/msg.h&gt;\n\nint msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>msqid<\/strong>\uff1a\u7531msgget\u51fd\u6570\u8fd4\u56de\u7684\u6d88\u606f\u961f\u5217\u6807\u8bc6\u7801\u3002<\/li>\n\n\n\n<li><strong>msgp<\/strong>\uff1a\u6307\u5411\u8981\u53d1\u9001\u7684\u6d88\u606f\u7684\u7ed3\u6784\u4f53\u3002\u8fd9\u4e2a\u7ed3\u6784\u4f53\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\uff0c\u4f46\u662f\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u5fc5\u987b\u662flong\u7c7b\u578b\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u5e76\u4e14\u53ea\u80fd\u4e3a\u6b63\u6574\u6570<\/mark>\uff0c\u8fd9\u4e2a\u5b57\u6bb5\u8868\u793a\u6d88\u606f\u7684\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>msgsz<\/strong>\uff1a\u8981\u53d1\u9001\u6d88\u606f\u7684\u5927\u5c0f\uff0c\u4e0d\u5305\u62ec\u6d88\u606f\u7c7b\u578b\u5360\u7528\u76844\u4e2a\u5b57\u8282\uff0c\u6d88\u606f\u6b63\u6587\u7684\u5927\u5c0f\u3002<\/li>\n\n\n\n<li><strong>msgflg<\/strong>\uff1a\u63a7\u5236\u7740\u5f53\u524d\u6d88\u606f\u961f\u5217\u6ee1\u6216\u5230\u8fbe\u7cfb\u7edf\u4e0a\u9650\u65f6\u5c06\u8981\u53d1\u751f\u7684\u4e8b\u60c5\u3002\u5b83\u6709\u4e24\u79cd\u53ef\u9009\u53c2\u6570\n<ul class=\"wp-block-list\">\n<li>0\uff1a\u5f53\u6d88\u606f\u961f\u5217\u6ee1\u65f6\uff0c<code>msgsnd<\/code>\u5c06\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u6d88\u606f\u80fd\u5199\u8fdb\u6d88\u606f\u961f\u5217\u3002<\/li>\n\n\n\n<li><code>IPC_NOWAIT<\/code>\uff1a\u5f53\u6d88\u606f\u961f\u5217\u5df2\u6ee1\u7684\u65f6\u5019\uff0c<code>msgsnd<\/code>\u51fd\u6570\u4e0d\u7b49\u5f85\u7acb\u5373\u8fd4\u56de\uff0c\u6b64\u65f6\u9519\u8bef\u7801\u4e3a<code>EAGAIN<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u53c2\u6570\u4e2d\u6bd4\u8f83\u96be\u4ee5\u7406\u89e3\u7684\u662f<code>msgp<\/code>\u548c<code>msgsz<\/code>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">msgp\u53c2\u6570<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">msgp\u53c2\u6570\u901a\u4fd7\u70b9\u8bb2\u5c31\u662f\u4e00\u4e2a\u6570\u636e\u5305\uff0c\u800c\u4e14\u5bf9\u8fd9\u4e2a\u6570\u636e\u5305\u7684\u5b9a\u4e49\u6ca1\u6709\u5f3a\u5236\u8981\u6c42\uff0c\u53ea\u6709\u4e24\u4e2a\u8981\u6c42\u2014\u2014\u7b2c\u4e00\u4e2a\u8981\u6c42\uff1a<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u5fc5\u987b\u662f\u6b63\u6574\u6570\u7684long\u7c7b\u578b\uff0c\u7b2c\u4e8c\u4e2a\u8981\u6c42\u662f\u5fc5\u987b\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53<\/mark>\u3002\u8fd9\u4e24\u4e2a\u8981\u6c42\u5f88\u597d\u7406\u89e3\uff0c\u524d\u9762\u6211\u4eec\u63d0\u5230\u4e86\u8fd9\u4e2along\u7c7b\u578b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5b57\u6bb5\u7c7b\u578b\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u4e0d\u8fc7\u8fd9\u4e2a\u7c7b\u578b\u4e0d\u662f\u5e38\u89c4\u610f\u4e49\u4e0a\u7684\u50cfchar double int\u8fd9\u6837\u7684\u6570\u636e\u7c7b\u578b\uff0c\u800c\u662f\u6d88\u606f\u7c7b\u578b<\/mark>\u3002\u5c31\u50cf\u8d3a\u5361\u4e00\u6837\uff0c\u5fc5\u987b\u8981\u6709\u4e00\u4e2a\u4e3b\u9898\uff0c\u53ef\u4ee5\u5047\u8bbe\u6625\u8282\u4e3b\u9898\u662f1\uff0c\u7aef\u5348\u8282\u4e3b\u9898\u662f2\u7b49\u7b49\u3002\u7b2c\u4e8c\u4e2a\u8981\u6c42\u4e5f\u597d\u7406\u89e3\uff0c\u56e0\u4e3a\u53d1\u9001\u7684\u6570\u636e\u6709\u4e86\u4e00\u4e2a\u4e3b\u9898\u4e86\uff0c\u4f46\u4e0d\u53ef\u80fd\u7ed9\u4eba\u5bb6\u53d1\u4e00\u4e2a\u4e3b\u9898\u8fc7\u53bb\u4e0d\u53d1\u5185\u5bb9\uff0c\u5982\u679c\u8981\u52a0\u4e0a\u5185\u5bb9\uff0c\u4e5f\u5fc5\u987b\u8981\u4f7f\u7528\u7ed3\u6784\u4f53\u3002\u4f46\u662f\u5e76\u6ca1\u6709\u5bf9\u8fd9\u4e2a\u7ed3\u6784\u4f53\u7684\u6784\u9020\u505a\u51fa\u8981\u6c42\uff0c\u56e0\u4e3a\u6d88\u606f\u6ca1\u6709\u56fa\u5b9a\u7684\u683c\u5f0f\uff0c\u53ef\u80fd\u662f\u53d1int\uff0c\u53ef\u80fd\u53d1char\u3002\u4e0b\u9762\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u5b9a\u4e49\u4e00\u4e2a\u8d3a\u5361\u6d88\u606f\u7c7b\u578b\nstruct msgbuf {\n    long mtype;       \/\/\u6d88\u606f\u7c7b\u578b \n    char name&#91;10];    \/\/\u53d1\u4ef6\u4eba\u540d\n    char mtext&#91;100];  \/\/\u6d88\u606f\u5185\u5bb9\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u6d88\u606f\u7c7b\u578b\u7684\u5b58\u5728\u4e00\u79cd\u89e3\u91ca\u662f\u4e3a\u4e86\u4f18\u5316\u6d88\u606f\u7684\u67e5\u627e\u6548\u7387\uff0c\u6d88\u606f\u961f\u5217\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u94fe\u8868\uff0c\u5982\u679c\u5728\u5355\u6761\u94fe\u8868\u4e0a\u4e0d\u65ad\u5ef6\u4f38\uff0c\u904d\u5386\u7684\u6548\u7387\u4f4e\u4e0b\uff0c\u5982\u679c\u8fdb\u884c\u4e86\u5206\u7c7b\u53ef\u4ee5\u4f18\u5316\u67e5\u627e\u6548\u7387\u3002\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u6839\u636e\u7528\u6237\u7684\u9700\u6c42\u7c7b\u578b\u5728\u6307\u5b9a\u7c7b\u578b\u94fe\u8868\u4e0a\u67e5\u627e\uff0c\u4e25\u8c28\u7684\u8bb2\uff0c\u8fd9\u91cc\u7684\u201c\u94fe\u8868\u201d\u5e76\u4e0d\u662f\u5b9e\u9645\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u4e00\u79cd\u6982\u5ff5\u4e0a\u7684\u7406\u89e3\u3002\u5b9e\u9645\u4e0a\uff0c\u6240\u6709\u7684\u6d88\u606f\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u6d88\u606f\u961f\u5217\u4e2d\uff0c\u53ea\u662f\u901a\u8fc7\u6d88\u606f\u7c7b\u578b\u8fd9\u4e2a\u5b57\u6bb5\u6765\u533a\u5206\u4e0d\u540c\u7684\u6d88\u606f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\"><strong>msgsz<\/strong>\u53c2\u6570<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u53c2\u6570\u6bd4\u8f83\u597d\u7406\u89e3\uff0c\u5c31\u662f\u6b63\u6587\u7684\u5927\u5c0f\uff0c\u56e0\u4e3a\u4f60\u53d1\u9001\u7684\u6570\u636e\u5305\uff0c\u88abvoid*\u7c7b\u578b\u63a5\u6536\uff0cvoid*\u4f1a\u5bfc\u81f4\u7ed3\u6784\u4f53\u672c\u8eab\u7684\u4fe1\u606f\u88ab\u5c4f\u853d\uff0c\u800c\u7531\u4e8e\u7ed3\u6784\u4f53\u6ca1\u6709\u505a\u51fa\u9650\u5236\uff0c\u6240\u4ee5\u4e5f\u6ca1\u6709\u529e\u6cd5\u505a\u4e00\u4e2a\u9884\u5148\u7684\u58f0\u660e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4ec5\u77e5\u9053\u6570\u636e\u5305\u5b58\u5728\u4e00\u4e2along\uff0c\u90a3\u5269\u4e0b\u7684\u6570\u636e\u957f\u5ea6\u5219\u9700\u8981\u7528\u6237\u5bf9\u5176\u505a\u51fa\u901a\u77e5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u64cd\u4f5c\u6210\u529f\uff0cmsgsnd\u5c06\u8fd4\u56de0<\/li>\n\n\n\n<li>\u5982\u679c\u5931\u8d25\uff0c\u5219\u8fd4\u56de-1<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">msgrcv\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/msg.h&gt;\n\nssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>msqid\uff1a<\/strong>\u7531msgget\u51fd\u6570\u8fd4\u56de\u7684\u6d88\u606f\u961f\u5217\u6807\u8bc6\u7801\u3002<\/li>\n\n\n\n<li><strong>msgp\uff1a<\/strong>\u6307\u5411\u63a5\u6536\u6d88\u606f\u7684\u7f13\u51b2\u533a\u3002\u8fd9\u4e2a\u7f13\u51b2\u533a\u5e94\u8be5\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u4e5f\u5c31\u662f\u4f60\u5728\u53d1\u9001\u6d88\u606f\u7684\u65f6\u5019\u5b9a\u4e49\u7684\u90a3\u4e2a\u7ed3\u6784\u4f53\uff0c\u4e3a\u4e86\u63a5\u6536\u5230\u6d88\u606f\u80fd\u8bc6\u522b\u51fa\u6765\uff0c\u6240\u4ee5\u5fc5\u987b\u8981\u4f7f\u7528\u4e00\u4e2a\u76f8\u540c\u7684\u7ed3\u6784\u4f53\u8fdb\u884c\u6d88\u606f\u7684\u89e3\u8bfb\u3002<\/li>\n\n\n\n<li><strong>msgsz\uff1a<\/strong>\u8981\u63a5\u6536\u7684\u6d88\u606f\u7684\u5927\u5c0f\uff0c\u4e0d\u5305\u62ec\u6d88\u606f\u7c7b\u578b\u5360\u7528\u76844\u4e2a\u5b57\u8282\u3002<\/li>\n\n\n\n<li><strong>msgtyp\uff1a<\/strong>\u6307\u5b9a\u8981\u63a5\u6536\u7684\u6d88\u606f\u7c7b\u578b\u3002\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c<code>msgtyp<\/code>\u4e3a0\uff0c\u90a3\u4e48<code>msgrcv<\/code>\u4f1a\u63a5\u6536\u6d88\u606f\u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u6d88\u606f\uff0c\u65e0\u8bba\u8fd9\u4e2a\u6d88\u606f\u7684\u7c7b\u578b\u662f\u4ec0\u4e48\u3002<\/li>\n\n\n\n<li>\u5982\u679c<code>msgtyp<\/code>\u5927\u4e8e0\uff0c\u90a3\u4e48<code>msgrcv<\/code>\u4f1a\u63a5\u6536\u6d88\u606f\u961f\u5217\u4e2d\u7b2c\u4e00\u4e2a\u7c7b\u578b\u4e3a<code>msgtyp<\/code>\u7684\u6d88\u606f\u3002<\/li>\n\n\n\n<li>\u5982\u679c<code>msgtyp<\/code>\u5c0f\u4e8e0\uff0c\u90a3\u4e48<code>msgrcv<\/code>\u4f1a\u63a5\u6536\u6d88\u606f\u961f\u5217\u4e2d\u7c7b\u578b\u503c\u4e0d\u5927\u4e8e<code>msgtyp<\/code>\u7684\u7edd\u5bf9\u503c\u4e14\u7c7b\u578b\u503c\u53c8\u6700\u5c0f\u7684\u6d88\u606f\u3002\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a\u5047\u8bbe\u5f53\u524d\u7684\u6d88\u606f\u67093\u30015\u30017\u30018\u3002\u5982\u679c\u6211\u7684\u63a5\u6536\u7c7b\u578b\u586b\u5199-9\uff0c\u90a3\u4e48\u64cd\u4f5c\u7cfb\u7edf\u5c31\u4f1a\u627e1-9\u4e2d\u6700\u5c0f\u7684\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f3\u3002\u90a3\u4e48\u6700\u7ec8\u7684\u6d88\u606f\u7c7b\u578b\u5c31\u9009\u62e93<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>msgflg\uff1a<\/strong>\u63a7\u5236\u7740\u5f53\u524d\u6d88\u606f\u961f\u5217\u6ee1\u6216\u5230\u8fbe\u7cfb\u7edf\u4e0a\u9650\u65f6\u5c06\u8981\u53d1\u751f\u7684\u4e8b\u60c5\u3002\u5b83\u6709\u56db\u79cd\u53ef\u9009\u53c2\u6570\n<ul class=\"wp-block-list\">\n<li><code>0<\/code>\uff1a\u8fd9\u662f\u9ed8\u8ba4\u7684\u6a21\u5f0f\u3002\u5982\u679c\u6ca1\u6709\u53ef\u7528\u7684\u6d88\u606f\uff0c<code>msgrcv<\/code>\u51fd\u6570\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u6709\u6d88\u606f\u53ef\u7528\u3002<\/li>\n\n\n\n<li><code>IPC_NOWAIT<\/code>\uff1a\u5982\u679c\u6ca1\u6709\u6d88\u606f\uff0c<code>msgrcv<\/code>\u51fd\u6570\u4e0d\u4f1a\u963b\u585e\uff0c\u800c\u662f\u7acb\u5373\u8fd4\u56de<code>ENOMSG<\/code>\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>MSG_NOERROR<\/code>\uff1a\u5982\u679c\u6d88\u606f\u7684\u5927\u5c0f\u8d85\u8fc7<code>msgsz<\/code>\uff0c\u6d88\u606f\u4f1a\u88ab\u622a\u65ad\uff0c\u800c\u4e0d\u662f\u8fd4\u56de<code>E2BIG<\/code>\u9519\u8bef\u3002<\/li>\n\n\n\n<li><code>MSG_EXCEPT<\/code>\uff1a\u5f53<code>msgtyp<\/code>\u5927\u4e8e0\u4e14<code>msgflg<\/code>\u8bbe\u7f6e\u4e3a<code>MSG_EXCEPT<\/code>\uff0c<code>msgrcv<\/code>\u4f1a\u63a5\u6536\u7c7b\u578b\u4e0d\u7b49\u4e8e<code>msgtyp<\/code>\u7684\u7b2c\u4e00\u6761\u6d88\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u64cd\u4f5c\u6210\u529f\uff0cmsgrcv\u5c06\u8fd4\u56de\u63a5\u6536\u5230\u7684\u6d88\u606f\u7684\u957f\u5ea6<\/li>\n\n\n\n<li>\u5982\u679c\u5931\u8d25\uff0c\u5219\u8fd4\u56de-1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">msgctl\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u539f\u578b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/msg.h&gt;\n\nint msgctl(int msqid, int cmd, struct msqid_ds *buf);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u53c2\u6570<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>msqid\uff1a<\/strong>\u7531<code>msgget<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u6d88\u606f\u961f\u5217\u6807\u8bc6\u7801\u3002<\/li>\n\n\n\n<li><strong>cmd\uff1a<\/strong>\u63a7\u5236\u547d\u4ee4\uff0c\u53ef\u4ee5\u662f\u4ee5\u4e0b\u51e0\u79cd\uff1a\n<ul class=\"wp-block-list\">\n<li>IPC_STAT\uff1a\u5c06<code>msqid<\/code>\u7684\u6d88\u606f\u961f\u5217\u7684\u5f53\u524d\u4fe1\u606f\u590d\u5236\u5230<code>buf<\/code>\u6307\u5411\u7684\u7ed3\u6784\u4f53\u4e2d\u3002<\/li>\n\n\n\n<li>IPC_SET\uff1a\u8bbe\u7f6e\u6d88\u606f\u961f\u5217\u7684\u5c5e\u6027\uff0c\u8981\u8bbe\u7f6e\u7684\u5c5e\u6027\u9700\u5148\u5b58\u50a8\u5728buf\u4e2d\uff0c\u53ef\u8bbe\u7f6e\u7684\u5c5e\u6027\u5305\u62ec\uff1a\n<ul class=\"wp-block-list\">\n<li>msg_perm.uid\u3002<\/li>\n\n\n\n<li>msg_perm.gid\u3002<\/li>\n\n\n\n<li>msg_perm.mode\u3002<\/li>\n\n\n\n<li>msg_qbytes\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>IPC_RMID\uff1a\u7acb\u5373\u5220\u9664<code>msqid<\/code>\u6807\u8bc6\u7684\u6d88\u606f\u961f\u5217\uff0c\u4ee5\u53ca\u8be5\u961f\u5217\u4e2d\u7684\u6240\u6709\u6d88\u606f\uff0c\u6b64\u65f6\u7684<code>buf<\/code>\u53ef\u4ee5\u8bbe\u4e3a<code>NULL<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>buf<\/strong>\uff1a\u6307\u5411\u4e00\u4e2a<code>msqid_ds<\/code>\u7ed3\u6784\u4f53\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4e2d\u5305\u542b\u4e86\u6d88\u606f\u961f\u5217\u7684\u4fe1\u606f<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e9b\u53c2\u6570\u4e2d\u6d89\u53ca\u5230\u4e86\u4e00\u4e2a\u53eb\u505a<code>msqid_ds<\/code> \u7684\u7ed3\u6784\u4f53\uff0c\u8be5\u7ed3\u6784\u4f53\u7684\u6210\u5458\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct msqid_ds {\n    struct ipc_perm msg_perm;   \/* \u6240\u6709\u6743\u548c\u6743\u9650\u4fe1\u606f *\/\n    struct msg *msg_first;      \/* \u961f\u5217\u4e2d\u7684\u7b2c\u4e00\u6761\u6d88\u606f\uff0c\u672a\u5728\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u4e2d\u4f7f\u7528 *\/\n    struct msg *msg_last;       \/* \u961f\u5217\u4e2d\u7684\u6700\u540e\u4e00\u6761\u6d88\u606f\uff0c\u672a\u5728\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u4e2d\u4f7f\u7528 *\/\n    __kernel_time_t msg_stime;  \/* \u6700\u540e\u4e00\u6b21\u8c03\u7528msgsnd\u51fd\u6570\u7684\u65f6\u95f4 *\/\n    __kernel_time_t msg_rtime;  \/* \u6700\u540e\u4e00\u6b21\u8c03\u7528msgrcv\u51fd\u6570\u7684\u65f6\u95f4 *\/\n    __kernel_time_t msg_ctime;  \/* \u6700\u540e\u4e00\u6b21\u6539\u53d8\u6d88\u606f\u961f\u5217\u72b6\u6001\u7684\u65f6\u95f4 *\/\n    unsigned long msg_lcbytes;  \/* \u572832\u4f4d\u7cfb\u7edf\u4e2d\u88ab\u91cd\u7528\uff0c\u4f46\u5728\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u4e2d\u5e76\u672a\u4f7f\u7528 *\/\n    unsigned long msg_lqbytes;  \/* \u572832\u4f4d\u7cfb\u7edf\u4e2d\u88ab\u91cd\u7528\uff0c\u4f46\u5728\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u4e2d\u5e76\u672a\u4f7f\u7528 *\/\n    unsigned short msg_cbytes;  \/* \u5f53\u524d\u6d88\u606f\u961f\u5217\u4e2d\u7684\u5b57\u8282\u6570 *\/\n    unsigned short msg_qnum;    \/* \u5f53\u524d\u6d88\u606f\u961f\u5217\u4e2d\u7684\u6d88\u606f\u6570 *\/\n    unsigned short msg_qbytes;  \/* \u6d88\u606f\u961f\u5217\u5141\u8bb8\u7684\u6700\u5927\u5b57\u8282\u6570 *\/\n    __kernel_ipc_pid_t msg_lspid; \/* \u6700\u540e\u4e00\u6b21\u8c03\u7528msgsnd\u51fd\u6570\u7684\u8fdb\u7a0bID *\/\n    __kernel_ipc_pid_t msg_lrpid; \/* \u6700\u540e\u4e00\u6b21\u63a5\u6536\u6d88\u606f\u7684\u8fdb\u7a0bID *\/\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u51fd\u6570\u8fd4\u56de\u503c<\/mark><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u64cd\u4f5c\u6210\u529f\uff0cmsgctl\u5c06\u8fd4\u56de0<\/li>\n\n\n\n<li>\u5982\u679c\u5931\u8d25\uff0c\u5219\u8fd4\u56de-1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7efc\u5408\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys\/types.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/ipc.h&gt;\n#include &lt;sys\/msg.h&gt;\n\nint main(){\n    pid_t pid = fork();\n\n    if(pid == 0){\n        \/\/\u7b49\u5f85\u7236\u8fdb\u7a0b\u521b\u5efa\u6d88\u606f\u961f\u5217\n        sleep(1);\n        \/\/\u521b\u5efa\u952e\u503c\n        key_t key = ftok(\"\/\",1);\n        \/\/\u83b7\u53d6\u961f\u5217id\n        int msgid = msgget(key,IPC_CREAT | 0666);\n        if(msgid == -1){\n            return 0;\n        }\n\n        \/\/\u521b\u5efa\u6d88\u606f\u7ed3\u6784\u4f53\n        struct msgdata{\n            long mtype;\n            char text&#91;20];\n        };\n        \n        \/\/\u5b9e\u4f8b\u5316\u6d88\u606f\n        struct msgdata mymsg;\n        \/\/\u7b49\u5f85\u7236\u8fdb\u7a0b\u6d88\u606f\u53d1\u9001\n        sleep(3);\n        \/\/\u63a5\u6536\u6d88\u606f\uff0c\u9009\u53d610\u4ee5\u5185\u6700\u5c0f\u7684\u7c7b\u578b\u7684\u6d88\u606f\n        msgrcv(msgid,&amp;mymsg,20,-10,0);\n        printf(\"\u6d88\u606f\u63a5\u6536\uff1a&#91;%s]\\n\",mymsg.text);\n\n\n    }else if(pid &gt; 0){\n        \/\/\u521b\u5efa\u952e\u503c\n        key_t key = ftok(\"\/\",1);\n        \/\/\u83b7\u53d6\u961f\u5217id\n        int msgid = msgget(key,IPC_CREAT | 0666);\n        if(msgid == -1){\n            return 0;\n        }\n\n        \/\/\u521b\u5efa\u6d88\u606f\u7ed3\u6784\u4f53\n        struct msgdata{\n            long mtype;\n            char text&#91;20];\n        };\n        \n        \/\/\u5b9e\u4f8b\u5316\u6d88\u606f\n        struct msgdata mymsg;\n        mymsg.mtype = 1;\n        strcpy(mymsg.text,\"hello world\");\n        \/\/\u6d88\u606f\u53d1\u9001\n        msgsnd(msgid,&amp;mymsg,20,0);\n        printf(\"\u6d88\u606f\u53d1\u9001\u5b8c\u6210\\n\");\n        \/\/\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5904\u7406\u6d88\u606f\n        sleep(5);\n        \/\/\u5220\u9664\u961f\u5217\n        msgctl(msgid,IPC_RMID,NULL);\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u8fd0\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6d88\u606f\u53d1\u9001\u5b8c\u6210\n\u6d88\u606f\u63a5\u6536\uff1a&#91;hello world]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u8fdb\u7a0b\u901a\u4fe1\uff08Inter-Process Communication\uff0cIPC\uff09\u662f\u6307\u5728\u4e0d\u540c\u7684\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u548c\u5171\u4eab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1,4],"tags":[18,33,40],"class_list":["post-1088","post","type-post","status-publish","format-standard","hentry","category-c-c","category-learning","category-os","tag-c","tag-linux","tag-40"],"_links":{"self":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/1088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1088"}],"version-history":[{"count":15,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"predecessor-version":[{"id":1107,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/1088\/revisions\/1107"}],"wp:attachment":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}