{"id":235542,"date":"2022-02-14T10:06:46","date_gmt":"2022-02-14T02:06:46","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=235542"},"modified":"2022-02-08T10:07:27","modified_gmt":"2022-02-08T02:07:27","slug":"how-to-lock-process-communication","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/how-to-lock-process-communication.html","title":{"rendered":"\u8fdb\u7a0b\u95f4\u901a\u4fe1\u5982\u4f55\u52a0\u9501"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fdb\u7a0b\u95f4\u901a\u4fe1\u6709\u4e00\u79cd[\u5171\u4eab\u5185\u5b58]\u65b9\u5f0f\uff0c\u5927\u5bb6\u6709\u6ca1\u6709\u60f3\u8fc7\uff0c\u8fd9\u79cd\u901a\u4fe1\u65b9\u5f0f\u4e2d\u5982\u4f55\u89e3\u51b3\u6570\u636e\u7ade\u4e89\u95ee\u9898\uff1f\u6211\u4eec\u53ef\u80fd\u81ea\u7136\u800c\u7136\u7684\u5c31\u4f1a\u60f3\u5230\u7528\u9501\u3002\u4f46\u6211\u4eec\u5e73\u65f6\u4f7f\u7528\u7684\u9501\u90fd\u662f\u7528\u4e8e\u89e3\u51b3\u7ebf\u7a0b\u95f4\u6570\u636e\u7ade\u4e89\u95ee\u9898\uff0c\u8c8c\u4f3c\u6ca1\u6709\u770b\u5230\u8fc7\u5b83\u7528\u5728\u8fdb\u7a0b\u4e2d\uff0c\u90a3\u600e\u4e48\u529e\uff1f<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5173\u4e8e\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u65b9\u5f0f\u4f30\u8ba1\u5927\u591a\u6570\u4eba\u90fd\u77e5\u9053\uff0c\u8fd9\u4e5f\u662f\u5e38\u89c1\u7684\u9762\u8bd5\u516b\u80a1\u6587\u4e4b\u4e00\u3002<\/p>\n

\u4e2a\u4eba\u8ba4\u4e3a\u8fd9\u79cd\u9762\u8bd5\u9898\u6ca1\u4ec0\u4e48\u610f\u4e49\uff0c\u65e0\u975e\u5c31\u662f\u7b54\u51e0\u4e2a\u5173\u952e\u8bcd\u800c\u5df2\uff0c\u66f4\u6df1\u5165\u7684\u53ef\u80fd\u9762\u8bd5\u5b98\u548c\u9762\u8bd5\u8005\u90fd\u4e0d\u592a\u4e86\u89e3\u3002<\/p>\n

\u5173\u4e8e\u8fdb\u7a0b\u95f4\u901a\u4fe1\u65b9\u5f0f\u548c\u4f18\u7f3a\u70b9\u6211\u4e4b\u524d\u5728\u3010\u8fd9\u7bc7\u6587\u7ae0\u3011\u4e2d\u6709\u8fc7\u4ecb\u7ecd\uff0c\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u79fb\u6b65\u53bb\u770b\u54c8\u3002<\/p>\n

\u8fdb\u7a0b\u95f4\u901a\u4fe1\u6709\u4e00\u79cd[\u5171\u4eab\u5185\u5b58]\u65b9\u5f0f\uff0c\u5927\u5bb6\u6709\u6ca1\u6709\u60f3\u8fc7\uff0c\u8fd9\u79cd\u901a\u4fe1\u65b9\u5f0f\u4e2d\u5982\u4f55\u89e3\u51b3\u6570\u636e\u7ade\u4e89\u95ee\u9898?<\/p>\n

\u6211\u4eec\u53ef\u80fd\u81ea\u7136\u800c\u7136\u7684\u5c31\u4f1a\u60f3\u5230\u7528\u9501\u3002\u4f46\u6211\u4eec\u5e73\u65f6\u4f7f\u7528\u7684\u9501\u90fd\u662f\u7528\u4e8e\u89e3\u51b3\u7ebf\u7a0b\u95f4\u6570\u636e\u7ade\u4e89\u95ee\u9898\uff0c\u8c8c\u4f3c\u6ca1\u6709\u770b\u5230\u8fc7\u5b83\u7528\u5728\u8fdb\u7a0b\u4e2d\uff0c\u90a3\u600e\u4e48\u529e?<\/p>\n

\u6211\u627e\u5230\u4e86\u4e24\u79cd\u65b9\u6cd5\uff0c\u4fe1\u53f7\u91cf\u548c\u4e92\u65a5\u9501\u3002<\/p>\n

\u76f4\u63a5\u7ed9\u5927\u5bb6\u8d34\u4ee3\u7801\u5427\uff0c\u9996\u5148\u662f\u4fe1\u53f7\u91cf\u65b9\u5f0f\uff1a<\/p>\n

\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n\r\nconstexpr int kMappingSize = 4096;\r\n\r\nvoid sem() {\r\n    const char* mapname = \"\/mapname\";\r\n    int mapfd = shm_open(mapname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);\r\n\r\n    MEOW_DEFER {\r\n        if (mapfd > 0) {\r\n            close(mapfd);\r\n            mapfd = 0;\r\n        }\r\n        shm_unlink(mapname);\r\n    };\r\n\r\n    if (mapfd == -1) {\r\n        perror(\"shm_open failed \\n\");\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    if (ftruncate(mapfd, kMappingSize) == -1) {\r\n        perror(\"ftruncate failed \\n\");\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    void* sp = mmap(nullptr, kMappingSize, PROT_READ | PROT_WRITE, MAP_SHARED, mapfd, 0);\r\n    if (!sp) {\r\n        perror(\"mmap failed \\n\");\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    sem_t* mutex = (sem_t*)sp;\r\n\r\n    if (sem_init(mutex, 1, 1) != 0) {\r\n        perror(\"sem_init failed \\n\");\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    MEOW_DEFER { sem_destroy(mutex); };\r\n\r\n    int* num = (int*)((char*)sp + sizeof(sem_t));\r\n    int cid, proc_count = 0, max_proc_count = 8;\r\n    for (int i = 0; i < max_proc_count; ++i) {\r\n        cid = fork();\r\n        if (cid == -1) {\r\n            perror(\"fork failed \\n\");\r\n            continue;\r\n        }\r\n        if (cid == 0) {\r\n            sem_wait(mutex);\r\n            (*num)++;\r\n            printf(\"process %d : %d \\n\", getpid(), *num);\r\n            sem_post(mutex);\r\n\r\n            if (munmap(sp, kMappingSize) == -1) {\r\n                perror(\"munmap failed\\n\");\r\n            }\r\n            close(mapfd);\r\n            exit(EXIT_SUCCESS);\r\n        }\r\n        ++proc_count;\r\n    }\r\n\r\n    int stat;\r\n    while (proc_count--) {\r\n        cid = wait(&stat);\r\n        if (cid == -1) {\r\n            perror(\"wait failed \\n\");\r\n            break;\r\n        }\r\n    }\r\n\r\n    printf(\"ok \\n\");\r\n}\r\n<\/pre>\n

\u4ee3\u7801\u4e2d\u7684MEOW_DEFER\u6211\u5728\u4e4b\u524d\u7684RAII\u76f8\u5173\u6587\u7ae0\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u5b83\u5185\u90e8\u7684\u51fd\u6570\u4f1a\u5728\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e\u89e6\u53d1\u3002\u5b83\u7684\u6838\u5fc3\u51fd\u6570\u5176\u5b9e\u5c31\u662f\u4e0b\u9762\u8fd9\u56db\u4e2a\uff1a<\/p>\n

\r\nint sem_init(sem_t *sem,int pshared,unsigned int value);\r\nint sem_post(sem_t *sem);\r\nint sem_wait(sem_t *sem);\r\nint sem_destroy(sem_t *sem);\r\n<\/pre>\n

\u5177\u4f53\u542b\u4e49\u5927\u5bb6\u5e94\u8be5\u770b\u540d\u5b57\u5c31\u77e5\u9053\uff0c\u8fd9\u91cc\u7684\u91cd\u70b9\u5c31\u662fsem_init\u4e2d\u7684pshared\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u4e3a1\u8868\u793a\u53ef\u5728\u8fdb\u7a0b\u95f4\u5171\u4eab\uff0c\u4e3a0\u8868\u793a\u53ea\u5728\u8fdb\u7a0b\u5185\u90e8\u5171\u4eab\u3002<\/p>\n

\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528\u9501\uff0c\u5373pthread_mutex_t\uff0c\u53ef\u662fpthread_mutex\u4e0d\u662f\u7528\u4f5c\u7ebf\u7a0b\u95f4\u6570\u636e\u7ade\u4e89\u7684\u5417\uff0c\u600e\u4e48\u80fd\u7528\u5728\u8fdb\u7a0b\u95f4\u5462?<\/p>\n

\u6211\u4e5f\u662f\u6700\u8fd1\u624d\u77e5\u9053\uff0c\u53ef\u4ee5\u7ed9\u5b83\u914d\u7f6e\u4e00\u4e2a\u5c5e\u6027\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\npthread_mutex_t* mutex;\r\npthread_mutexattr_t mutexattr;\r\n\r\npthread_mutexattr_init(&mutexattr);\r\npthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);\r\npthread_mutex_init(mutex, &mutexattr);\r\n<\/pre>\n

\u5b83\u7684\u9ed8\u8ba4\u5c5e\u6027\u662f\u8fdb\u7a0b\u5185\u79c1\u6709\uff0c\u4f46\u662f\u5982\u679c\u7ed9\u5b83\u914d\u7f6e\u6210PTHREAD_PROCESS_SHARED\uff0c\u5b83\u5c31\u53ef\u4ee5\u7528\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e2d\u3002<\/p>\n

\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nvoid func() {\r\n    const char* mapname = \"\/mapname\";\r\n    int mapfd = shm_open(mapname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);\r\n\r\n    MEOW_DEFER {\r\n        if (mapfd > 0) {\r\n            close(mapfd);\r\n            mapfd = 0;\r\n        }\r\n        shm_unlink(mapname);\r\n    };\r\n\r\n    if (mapfd == -1) {\r\n        perror(\"shm_open failed \\n\");\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    if (ftruncate(mapfd, kMappingSize) == -1) {\r\n        perror(\"ftruncate failed \\n\");\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    void* sp = mmap(nullptr, kMappingSize, PROT_READ | PROT_WRITE, MAP_SHARED, mapfd, 0);\r\n    if (!sp) {\r\n        perror(\"mmap failed \\n\");\r\n        exit(EXIT_FAILURE);\r\n    }\r\n\r\n    pthread_mutex_t* mutex = (pthread_mutex_t*)sp;\r\n    pthread_mutexattr_t mutexattr;\r\n\r\n    pthread_mutexattr_init(&mutexattr);\r\n    pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);\r\n    pthread_mutex_init(mutex, &mutexattr);\r\n\r\n    MEOW_DEFER {\r\n        pthread_mutexattr_destroy(&mutexattr);\r\n        pthread_mutex_destroy(mutex);\r\n    };\r\n\r\n    int* num = (int*)((char*)sp + sizeof(pthread_mutex_t));\r\n    int cid, proc_count = 0, max_proc_count = 8;\r\n    for (int i = 0; i < max_proc_count; ++i) {\r\n        cid = fork();\r\n        if (cid == -1) {\r\n            perror(\"fork failed \\n\");\r\n            continue;\r\n        }\r\n        if (cid == 0) {\r\n            pthread_mutex_lock(mutex);\r\n            (*num)++;\r\n            printf(\"process %d : %d \\n\", getpid(), *num);\r\n            pthread_mutex_unlock(mutex);\r\n\r\n            if (munmap(sp, kMappingSize) == -1) {\r\n                perror(\"munmap failed\\n\");\r\n            }\r\n            close(mapfd);\r\n            exit(EXIT_SUCCESS);\r\n        }\r\n        ++proc_count;\r\n    }\r\n\r\n    int stat;\r\n    while (proc_count--) {\r\n        cid = wait(&stat);\r\n        if (cid == -1) {\r\n            perror(\"wait failed \\n\");\r\n            break;\r\n        }\r\n    }\r\n\r\n    printf(\"ok \\n\");\r\n}\r\n<\/pre>\n

\u6211\u60f3\u8fd9\u4e24\u79cd\u65b9\u5f0f\u5e94\u8be5\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u65e5\u5e38\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684\u5927\u591a\u6570\u9700\u6c42\u3002<\/p>\n

\u9501\u7684\u65b9\u5f0f\u4ecb\u7ecd\u5b8c\u4e4b\u540e\uff0c\u53ef\u80fd\u5f88\u591a\u670b\u53cb\u81ea\u7136\u5c31\u4f1a\u60f3\u5230\u539f\u5b50\u53d8\u91cf\uff0c\u8fd9\u5757\u6211\u4e5f\u641c\u7d22\u4e86\u4e00\u4e0b\u3002\u4f46\u662f\u4e5f\u4e0d\u592a\u786e\u5b9aC++\u6807\u51c6\u4e2d\u7684atomic\u662f\u5426\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e2d\u6709\u4f5c\u7528\uff0c\u4e0d\u8fc7\u770b\u6837\u5b50boost\u4e2d\u7684atomic\u662f\u53ef\u4ee5\u7528\u5728\u8fdb\u7a0b\u95f4\u901a\u4fe1\u4e2d\u7684\u3002<\/p>\n

\u5176\u5b9e\u5728\u7814\u7a76\u8fd9\u4e2a\u95ee\u9898\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u627e\u5230\u4e86\u4e00\u4e9b\u5f88\u591a\u89e3\u51b3\u529e\u6cd5\uff0c\u5305\u62ec\uff1a<\/p>\n

    Disabling Interrupts<\/ol>\n
      Lock Variables<\/ol>\n
        Strict Alternation<\/ol>\n
          Peterson's Solution<\/ol>\n
            The TSL Instruction<\/ol>\n
              Sleep and Wakeup<\/ol>\n
                Semaphores<\/ol>\n
                  Mutexes<\/ol>\n
                    Monitors<\/ol>\n
                      Message Passing<\/ol>\n
                        Barriers<\/ol>\n

                        \u8fd9\u91cc\u5c31\u4e0d\u8fc7\u591a\u4ecb\u7ecd\u5566\uff0c\u5927\u5bb6\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u884c\u67e5\u9605\u8d44\u6599\u54c8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

                        \u5173\u4e8e\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u65b9\u5f0f\u4f30\u8ba1\u5927\u591a\u6570\u4eba\u90fd\u77e5\u9053\uff0c\u8fd9\u4e5f\u662f\u5e38\u89c1\u7684\u9762\u8bd5\u516b\u80a1\u6587\u4e4b\u4e00\u3002 \u4e2a\u4eba\u8ba4\u4e3a\u8fd9\u79cd\u9762\u8bd5\u9898\u6ca1\u4ec0\u4e48\u610f\u4e49\uff0c\u65e0\u975e\u5c31\u662f\u7b54 […]<\/p>\n","protected":false},"author":1329,"featured_media":235546,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-235542","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=235542"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235542\/revisions"}],"predecessor-version":[{"id":235547,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/235542\/revisions\/235547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/235546"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=235542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=235542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=235542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}