{"id":9572,"date":"2023-03-10T22:28:30","date_gmt":"2023-03-10T14:28:30","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=9572"},"modified":"2024-05-05T14:18:11","modified_gmt":"2024-05-05T06:18:11","slug":"c-%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0%e5%8d%95%e8%bf%9b%e7%a8%8b%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=9572","title":{"rendered":"C \u8bed\u8a00\u5355\u5b9e\u4f8b"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#Windows\" >Windows<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#%E7%8E%AF%E5%A2%83\" >\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#%E4%BA%92%E6%96%A5%E9%94%81%E6%96%B9%E6%A1%88\" >\u4e92\u65a5\u9501\u65b9\u6848<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#%E5%91%BD%E5%90%8D%E7%AE%A1%E9%81%93%E6%96%B9%E6%A1%88\" >\u547d\u540d\u7ba1\u9053\u65b9\u6848<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#Linux\" >Linux<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#%E7%8E%AF%E5%A2%83-2\" >\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#%E6%96%87%E4%BB%B6%E9%94%81%E6%96%B9%E6%A1%88\" >\u6587\u4ef6\u9501\u65b9\u6848<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.iyatt.com\/?p=9572\/#%E6%B3%A8\" >\u6ce8<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p>\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u8fd0\u884c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\uff0c\u4e00\u65e6\u6709\u591a\u4e2a\u5b9e\u4f8b\u8fd0\u884c\uff0c\u5219\u540e\u7eed\u5b9e\u4f8b\u4f1a\u68c0\u6d4b\u5230\u5148\u524d\u7684\u5b9e\u4f8b\u6b63\u5728\u8fd0\u884c\uff0c\u5e76\u9000\u51fa\u6216\u5411\u5148\u524d\u7684\u5b9e\u4f8b\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Windows\"><\/span>Windows<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E7%8E%AF%E5%A2%83\"><\/span>\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Windows SDK 10.0\uff08Windows 11\uff09<br \/>\n\u5e73\u53f0\u5de5\u5177\u96c6\uff1aVisual Studio 2022 v143<br \/>\nC \u8bed\u8a00\u6807\u51c6\uff1aC17<br \/>\n\u5b57\u7b26\u96c6\uff1aUnicode<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E4%BA%92%E6%96%A5%E9%94%81%E6%96%B9%E6%A1%88\"><\/span>\u4e92\u65a5\u9501\u65b9\u6848<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-c\">#include &lt;Windows.h&gt;\n#include &lt;stdio.h&gt;\n\nint main()\n{\n    HANDLE mutex = CreateMutex(NULL, TRUE, L&quot;MyUniqueMutex&quot;);\n\n    if (GetLastError() == ERROR_ALREADY_EXISTS)\n    {\n        wprintf(L&quot;\u5df2\u7ecf\u6709\u4e00\u4e2a\u5b9e\u4f8b\u6b63\u5728\u8fd0\u884c\u4e2d\uff01\\n&quot;);\n        CloseHandle(mutex);\n        return -1;\n    }\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ \u5728\u8fd9\u91cc\u5b9e\u73b0\u529f\u80fd\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    CloseHandle(mutex);\n\n    return 0;\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E5%91%BD%E5%90%8D%E7%AE%A1%E9%81%93%E6%96%B9%E6%A1%88\"><\/span>\u547d\u540d\u7ba1\u9053\u65b9\u6848<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;windows.h&gt;\n\nint main()\n{\n    HANDLE hPipe;\n    WCHAR pipeName[] = L&quot;\\\\\\\\.\\\\pipe\\\\SingleInstancePipe&quot;;\n    BOOL isAnotherInstanceRunning;\n\n    hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, 1, 0, 0, NMPWAIT_USE_DEFAULT_WAIT, NULL);\n    if (hPipe == INVALID_HANDLE_VALUE)\n    {\n        printf(&quot;\u521b\u5efa\u547d\u540d\u7ba1\u9053\u5931\u8d25 %ld\\n&quot;, GetLastError());\n        exit(1);\n    }\n\n    isAnotherInstanceRunning = ConnectNamedPipe(hPipe, NULL) ? FALSE : (GetLastError() == ERROR_PIPE_CONNECTED);\n    if (isAnotherInstanceRunning)\n    {\n        printf(&quot;\u5df2\u7ecf\u6709\u4e00\u4e2a\u5b9e\u4f8b\u6b63\u5728\u8fd0\u884c\u4e2d\uff01\\n&quot;);\n        exit(0);\n    }\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ \u5728\u8fd9\u91cc\u5b9e\u73b0\u529f\u80fd\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    Sleep(1000000);\n\n    \/\/ \u5173\u95ed\u7ba1\u9053\n    CloseHandle(hPipe);\n    return 0;\n}<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Linux\"><\/span>Linux<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E7%8E%AF%E5%A2%83-2\"><\/span>\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Debian 11\uff08arm64\uff09<br \/>\n\u7f16\u8bd1\u5668\uff1aGNU\uff08gcc\uff0910.2.1\uff1bC \u8bed\u8a00\u6807\u51c6\uff1aC17\uff1b\u7f16\u8bd1\u53c2\u6570\uff1a-std=c17 -no-pie -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Werror=format-security -Wextra -pedantic -Wimplicit-fallthrough -Wsequence-point -Wswitch-unreachable -Wswitch-enum -Wstringop-truncation -Wbool-compare -Wtautological-compare -Wfloat-equal -Wshadow=global -Wpointer-arith -Wpointer-compare -Wcast-align -Wcast-qual -Wwrite-strings -Wdangling-else -Wlogical-op -Wconversion -g -O0<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E6%96%87%E4%BB%B6%E9%94%81%E6%96%B9%E6%A1%88\"><\/span>\u6587\u4ef6\u9501\u65b9\u6848<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6587\u4ef6\u9501\u65b9\u6848\u672c\u8eab\u5bf9\u4e8e\u8fd0\u884c\u6743\u9650\u6ca1\u6709\u8981\u6c42\uff0c\u4f46\u662f\u6587\u4ef6\u9501\u4f9d\u8d56\u6587\u4ef6\uff0c\u6240\u4ee5\u521b\u5efa\u6587\u4ef6\u7684\u8def\u5f84\u5c31\u8981\u8003\u8651\u6743\u9650\u7684\u95ee\u9898\u4e86\uff0c\u4e00\u822c\u6587\u4ef6\u9501\u90fd\u662f\u653e\u5230 \/var\/run\uff0c\u4f46\u662f\u8fd9\u4e2a\u8def\u5f84\u9700\u8981 root \u6743\u9650\u624d\u884c\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4f8b\u5b50\u9700\u8981\u4ee5 root \u8fd0\u884c\u624d\u53ef\u884c\u3002<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;sys\/file.h&gt;\n\nint main()\n{\n    \/\/ \u6253\u5f00\u9501\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u5b83\n    int lockfile = open(&quot;\/var\/run\/myprogram.lock&quot;, O_CREAT | O_RDWR, 0644);\n    if (lockfile &lt; 0)\n    {\n        printf(&quot;\u65e0\u6cd5\u6253\u5f00\u9501\u6587\u4ef6\uff01\\n&quot;);\n        return -1;\n    }\n\n    \/\/ \u5c1d\u8bd5\u83b7\u53d6\u6587\u4ef6\u9501\n    if (flock(lockfile, LOCK_EX | LOCK_NB) &lt; 0)\n    {\n        printf(&quot;\u5df2\u7ecf\u6709\u4e00\u4e2a\u5b9e\u4f8b\u6b63\u5728\u8fd0\u884c\u4e2d\uff01\\n&quot;);\n        close(lockfile);\n        return -1;\n    }\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ \u5728\u8fd9\u91cc\u5b9e\u73b0\u529f\u80fd\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    \/\/ \u91ca\u653e\u6587\u4ef6\u9501\n    flock(lockfile, LOCK_UN);\n    close(lockfile);\n\n    return 0;\n}<\/code><\/pre>\n<p>\u6216\u8005\u53ef\u4ee5\u8003\u8651\u653e\u5230\u5bb6\u76ee\u5f55\u4e0b\uff0c\u8fd9\u4e2a\u5728\u5f53\u524d\u666e\u901a\u7528\u6237\u4e0b\u662f\u6709\u8db3\u591f\u6743\u9650\u7684\uff0c\u4e0b\u9762\u662f\u793a\u4f8b\u4ee3\u7801<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;sys\/file.h&gt;\n\nint main()\n{\n    char filePath[256];\n    memset(filePath, 0, sizeof(filePath));\n    const char *homePath = getenv(&quot;HOME&quot;); \/\/ \u83b7\u53d6\u5bb6\u76ee\u5f55\u8def\u5f84\n    sprintf(filePath, &quot;%s\/myprogram.lock&quot;, homePath); \/\/ \u62fc\u63a5\u9501\u6587\u4ef6\u8def\u5f84\n    \/\/ \u6253\u5f00\u9501\u6587\u4ef6\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u5b83\n    int lockfile = open(filePath, O_CREAT | O_RDWR, 0644);\n    if (lockfile &lt; 0)\n    {\n        printf(&quot;\u65e0\u6cd5\u6253\u5f00\u9501\u6587\u4ef6\uff01\\n&quot;);\n        return -1;\n    }\n\n    \/\/ \u5c1d\u8bd5\u83b7\u53d6\u6587\u4ef6\u9501\n    if (flock(lockfile, LOCK_EX | LOCK_NB) &lt; 0)\n    {\n        printf(&quot;\u5df2\u7ecf\u6709\u4e00\u4e2a\u5b9e\u4f8b\u6b63\u5728\u8fd0\u884c\u4e2d\uff01\\n&quot;);\n        close(lockfile);\n        return -1;\n    }\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ \u5728\u8fd9\u91cc\u5b9e\u73b0\u529f\u80fd\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    \/\/ \u91ca\u653e\u6587\u4ef6\u9501\n    flock(lockfile, LOCK_UN);\n    close(lockfile);\n\n    return 0;\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E6%B3%A8\"><\/span>\u6ce8<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Linux \u7684\u6211\u8003\u8651\u4e86\u547d\u540d\u7ba1\u9053\u7684\u65b9\u6848\uff0c\u4f46\u662f\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u7a0b\u5e8f\u662f\u975e\u6b63\u5e38\u7ed3\u675f\uff0c\u6ca1\u6709\u6267\u884c\u547d\u540d\u7ba1\u9053\u7684\u5220\u9664\u64cd\u4f5c\uff0c\u90a3\u4e48\u4e5f\u65e0\u6cd5\u518d\u6b21\u8fd0\u884c\uff0c\u6240\u4ee5\u8fd9\u91cc\u6211\u6ca1\u6709\u5199\u51fa\u6765\u3002<br \/>\nPOSIX \u4fe1\u53f7\u91cf\u65b9\u6848\u4e5f\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u53e6\u5916\u7f16\u8bd1\u7684\u65f6\u5019\u8981\u94fe\u63a5 Threads \u591a\u7ebf\u7a0b\u5e93\uff0c\u8fd9\u91cc\u4e5f\u6ca1\u5199\u4e86\u3002<br \/>\n\u6700\u7a33\u59a5\u7684\u65b9\u6848\u8fd8\u662f\u6587\u4ef6\u9501\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u8fd0\u884c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\uff0c\u4e00\u65e6\u6709\u591a\u4e2a\u5b9e\u4f8b\u8fd0\u884c\uff0c\u5219\u540e\u7eed\u5b9e\u4f8b\u4f1a\u68c0\u6d4b\u5230\u5148\u524d\u7684\u5b9e\u4f8b\u6b63\u5728\u8fd0\u884c\uff0c\u5e76\u9000\u51fa\u6216\u5411\u5148\u524d\u7684\u5b9e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,589],"tags":[],"class_list":["post-9572","post","type-post","status-publish","format-standard","hentry","category-all","category-c-c"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/9572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9572"}],"version-history":[{"count":0,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/9572\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}