{"id":3828,"date":"2020-07-15T13:32:27","date_gmt":"2020-07-15T04:32:27","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=3828"},"modified":"2020-07-15T13:36:45","modified_gmt":"2020-07-15T04:36:45","slug":"message-queue-unix-network-programming-75p-90p","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/message-queue-unix-network-programming-75p-90p\/","title":{"rendered":"message queue, unix network programming 75p ~ 90p"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">fifo\ub294 \uba3c\uc800 \ub4e4\uc5b4\uc628 \uc21c\uc11c\ub85c \ub3d9\uc791\ud558\uc9c0\ub9cc, message queue\ub294 \ube60\ub978 \uc6b0\uc120\uc21c\uc704\ub85c \ub3d9\uc791\ud55c\ub2e4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">signal handler\ub97c \uc815\uc758\ud558\uc5ec \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\ub2e4. message noty\ub294 queue\uac00 \ube44\uc5b4\uc788\uc744 \uacbd\uc6b0 \ud55c\ubc88 \ub3d9\uc791\ud55c\ub2e4. queue\uac00 \ucc28 \uc788\ub2e4\uba74 signal\uc744 \ubb34\uc2dc\ud55c\ub2e4. \ud55c \ubc88 \uc0ac\uc6a9\ud558\uba74 \ub2e4\uc2dc \uc2dc\uadf8\ub110\uc744 \ub4f1\ub85d\ud574\uc57c \ud55c\ub2e4.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC $ cat mesquereceive.c \n\/\/message que header\n#include &lt;unistd.h>\n#include &lt;stdio.h>\n#include &lt;fcntl.h>\n#include &lt;sys\/stat.h>\n#include &lt;mqueue.h>\n#include &lt;signal.h>\n#define MAX_SIZE 100\n\n\/\/signal \ucc98\ub9ac\ud558\uae30 \uc704\ud55c hanlder\nstatic void sig_usr1(int mysig);\n\nstruct sigevent sigev;\n\/\/signal hanlder\ub85c \uc0ac\uc6a9\ud558\ub824\uace0 global\ub85c \ub4f1\ub85d.\nmqd_t\tmqd;\nstruct mq_attr attr;\nint priorecv;\nchar buff[100];\n\nint main(){\n\tint\t\tc, flags;\n\tchar const *quename=\"\/tmp_message\";\n\tchar messageptr[50]= \"this is a test.\";\n\t\/\/\uba54\uc138\uc9c0 \ud050 \ucd08\uae30\ud654\n\tattr.mq_flags = 0;\n    attr.mq_maxmsg = 10;\n    attr.mq_msgsize = MAX_SIZE;\n    attr.mq_curmsgs = 0;\n\n\n\t\/\/message\ub97c \ub123\uae30\uc804\uc5d0 mq_notify \uc815\uc758\n\tsignal(SIGUSR1, sig_usr1);\n\tsigev.sigev_notify = SIGEV_SIGNAL;\n\tsigev.sigev_signo = SIGUSR1;\n\tmq_notify(mqd, &amp;sigev);\n\n\tflags = O_RDWR | O_CREAT;\n\t\/\/gcc \ucef4\ud30c\uc77c\uc2dc gcc mesque.c -lrt\n\t\/\/\ub85c \ud574\uc57c \ucef4\ud30c\uc77c \ub428.\n\tmqd = mq_open(quename, flags, 0666, &amp;attr);\n\tsize_t len = sizeof(messageptr);\n\t\/\/mq_send(mqd, messageptr, len, 10);\n\t\/\/sleep(1);\n\n\n\t\/\/\ub123\uc740 \uba54\uc138\uc9c0\ub97c \ud655\uc778\n\t\/\/mq_receive(mqd, buff, attr.mq_msgsize, &amp;priorecv);\n\t\/\/printf(\"\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\\n %s\ub97c %d \uc6b0\uc120 \uc21c\uc704\ub85c\\n\", buff, priorecv);\n\t\/\/mq_receive(mqd, buff, attr.mq_msgsize, &amp;priorecv);\n\t\/\/printf(\"\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\\n %s\ub97c %d \uc6b0\uc120 \uc21c\uc704\ub85c\\n\", buff, priorecv);\n\tfor(;;)\n\t\tpause();\n\tmq_close(mqd);\n\treturn 0;\n}\n\nvoid sig_usr1(int mysig){\n\t\/\/signal hanlder\ub85c message receive..\n\n\t\/\/\ud55c\ubc88 \uc0ac\uc6a9\ud55c noty\ub294 \ub2e4\uc2dc \ub4f1\ub85d.\n\tmq_notify(mqd, &amp;sigev);\n\tmq_receive(mqd, buff, attr.mq_msgsize, &amp;priorecv);\n\tprintf(\"\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\\n %s\ub97c %d \uc6b0\uc120 \uc21c\uc704\ub85c\\n\", buff, priorecv);\n\treturn;\n\n\n\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC $ cat mesquesender.c \n\/\/message que header\n#include &lt;unistd.h>\n#include &lt;stdio.h>\n#include &lt;fcntl.h>\n#include &lt;sys\/stat.h>\n#include &lt;mqueue.h>\n#include &lt;stdlib.h>\n#include &lt;signal.h>\n#define MAX_SIZE 100\n\n\nmqd_t\tmqd;\nstruct mq_attr attr;\nint priorecv;\nchar buff[100];\n\nint main(int argc, char** argv){\n\tint\t\tc, flags;\n\tint prio;\n\tchar const *quename=\"\/tmp_message\";\n\tchar messageptr[50]= \"this is a test.\";\n\t\/\/\uba54\uc138\uc9c0 \ud050 \ucd08\uae30\ud654\n\tattr.mq_flags = 0;\n    attr.mq_maxmsg = 10;\n    attr.mq_msgsize = MAX_SIZE;\n    attr.mq_curmsgs = 0;\n\n\tflags = O_RDWR | O_CREAT;\n\t\/\/gcc \ucef4\ud30c\uc77c\uc2dc gcc mesque.c -lrt\n\t\/\/\ub85c \ud574\uc57c \ucef4\ud30c\uc77c \ub428.\n\tmqd = mq_open(quename, flags, 0666, &amp;attr);\n\tsize_t len = sizeof(messageptr);\n\n\tprio = atoi(argv[1]);\n\tmq_send(mqd, messageptr, len, prio);\n\tmq_close(mqd);\n\treturn 0;\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pi@raspberrypi:~\/Project\/cCode\/IPC $ mv mesque.c mesquereceive.c\npi@raspberrypi:~\/Project\/cCode\/IPC $ gcc -lrt mesquereceive.c -o recv\npi@raspberrypi:~\/Project\/cCode\/IPC $ gcc -lrt mesquesender.c -o send\npi@raspberrypi:~\/Project\/cCode\/IPC $ cat \/dev\/mqueue\/tmp_message \nQSIZE:0          NOTIFY:0     SIGNO:0     NOTIFY_PID:0     \npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send \n\uc138\uadf8\uba58\ud14c\uc774\uc158 \uc624\ub958\npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send 10\npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send 20\npi@raspberrypi:~\/Project\/cCode\/IPC $ cat \/dev\/mqueue\/tmp_message \nQSIZE:100        NOTIFY:0     SIGNO:0     NOTIFY_PID:0     \npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send 100\npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/recv \n^Z\n[1]+  Stopped                 .\/recv\npi@raspberrypi:~\/Project\/cCode\/IPC $ bg\n[1]+ .\/recv &amp;\npi@raspberrypi:~\/Project\/cCode\/IPC $ cat \/dev\/mqueue\/tmp_message \nQSIZE:150        NOTIFY:0     SIGNO:0     NOTIFY_PID:0     \npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send 15\npi@raspberrypi:~\/Project\/cCode\/IPC $ bg\n-bash: bg: job 1 already in background\npi@raspberrypi:~\/Project\/cCode\/IPC $ jobs\n[1]+  Running                 .\/recv &amp;\npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send 20\npi@raspberrypi:~\/Project\/cCode\/IPC $ pidof recv \n16120\npi@raspberrypi:~\/Project\/cCode\/IPC $ pidof recv |xargs kill -10\n\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\n this is a test.\ub97c 100 \uc6b0\uc120 \uc21c\uc704\ub85c\npi@raspberrypi:~\/Project\/cCode\/IPC $ pidof recv |xargs kill -10\n\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\n this is a test.\ub97c 20 \uc6b0\uc120 \uc21c\uc704\ub85c\npi@raspberrypi:~\/Project\/cCode\/IPC $ pidof recv |xargs kill -10\n\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\n this is a test.\ub97c 20 \uc6b0\uc120 \uc21c\uc704\ub85c\npi@raspberrypi:~\/Project\/cCode\/IPC $ pidof recv |xargs kill -10\n\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\n this is a test.\ub97c 15 \uc6b0\uc120 \uc21c\uc704\ub85c\npi@raspberrypi:~\/Project\/cCode\/IPC $ pidof recv |xargs kill -10\n\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\n this is a test.\ub97c 10 \uc6b0\uc120 \uc21c\uc704\ub85c\npi@raspberrypi:~\/Project\/cCode\/IPC $ pidof recv |xargs kill -10\npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send 100\n\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\n this is a test.\ub97c 100 \uc6b0\uc120 \uc21c\uc704\ub85c\npi@raspberrypi:~\/Project\/cCode\/IPC $ cat \/dev\/mqueue\/tmp_message \nQSIZE:0          NOTIFY:0     SIGNO:10    NOTIFY_PID:16120 \npi@raspberrypi:~\/Project\/cCode\/IPC $ .\/send 10\n\ubc1b\uc740 \uba54\uc138\uc9c0\ub294\n this is a test.\ub97c 10 \uc6b0\uc120 \uc21c\uc704\ub85c\npi@raspberrypi:~\/Project\/cCode\/IPC $ cat \/dev\/mqueue\/tmp_message \nQSIZE:0          NOTIFY:0     SIGNO:10    NOTIFY_PID:16120 \npi@raspberrypi:~\/Project\/cCode\/IPC $ \n<\/pre>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/www.joinc.co.kr\/w\/Site\/system_programing\/IPC\/MessageQueue\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">gcc \uae30\ubcf8 \ucef4\ud30c\uc77c\ud558\uba74 \uc5d0\ub7ec\ub97c \ubcf8\ub2e4. \uc635\uc158 \ub123\uc5b4\uc57c \ud55c\ub2e4. -lrt.<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/stackoverflow.com\/questions\/19964206\/weird-posix-message-queue-linking-issue-sometimes-it-doesnt-link-correctly\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>fifo\ub294 \uba3c\uc800 \ub4e4\uc5b4\uc628 \uc21c\uc11c\ub85c \ub3d9\uc791\ud558\uc9c0\ub9cc, message queue\ub294 \ube60\ub978 \uc6b0\uc120\uc21c\uc704\ub85c \ub3d9\uc791\ud55c\ub2e4. signal handler\ub97c \uc815\uc758\ud558\uc5ec \uc0ac\uc6a9\ud560 \uc218\ub3c4 \uc788\ub2e4. message noty\ub294 queue\uac00 \ube44\uc5b4\uc788\uc744 \uacbd\uc6b0 \ud55c\ubc88 \ub3d9\uc791\ud55c\ub2e4. queue\uac00 \ucc28 \uc788\ub2e4\uba74 signal\uc744 \ubb34\uc2dc\ud55c\ub2e4. \ud55c \ubc88 \uc0ac\uc6a9\ud558\uba74 \ub2e4\uc2dc \uc2dc\uadf8\ub110\uc744 \ub4f1\ub85d\ud574\uc57c \ud55c\ub2e4. gcc \uae30\ubcf8 \ucef4\ud30c\uc77c\ud558\uba74 \uc5d0\ub7ec\ub97c \ubcf8\ub2e4. \uc635\uc158 \ub123\uc5b4\uc57c \ud55c\ub2e4. -lrt.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[12],"tags":[775,780,53,783,781],"class_list":["post-3828","post","type-post","status-publish","format-standard","hentry","category-12","tag-c","tag-ipc","tag-linux","tag-message-queue","tag-network-program"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=3828"}],"version-history":[{"count":5,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3828\/revisions"}],"predecessor-version":[{"id":3839,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/3828\/revisions\/3839"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=3828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=3828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=3828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}