{"id":198570,"date":"2024-11-14T20:22:11","date_gmt":"2024-11-14T12:22:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/198570\/"},"modified":"2024-11-14T20:22:11","modified_gmt":"2024-11-14T12:22:11","slug":"%e3%80%8c%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3%e3%80%8dlinux-c-%e7%b7%9a%e7%a8%8b%e6%a3%a7%e5%a4%a7%e5%b0%8f%e5%8f%8a%e5%85%b6%e5%bd%b1%e9%9f%bf-linux-c-%e7%b7%9a%e7%a8%8b%e6%a3%a7%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/198570\/","title":{"rendered":"\u300c\u6df1\u5165\u7406\u89e3\u300dLinux C \u7dda\u7a0b\u68e7\u5927\u5c0f\u53ca\u5176\u5f71\u97ff (linux c \u7dda\u7a0b\u68e7\u5927\u5c0f)"},"content":{"rendered":"<h1 id=\"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-linux-c-%e7%b7%9a%e7%a8%8b%e6%a3%a7%e5%a4%a7%e5%b0%8f%e5%8f%8a%e5%85%b6%e5%bd%b1%e9%9f%bf-tntSxawmYo\">\u6df1\u5165\u7406\u89e3 Linux C \u7dda\u7a0b\u68e7\u5927\u5c0f\u53ca\u5176\u5f71\u97ff<\/h1>\n<p>\u5728 Linux \u74b0\u5883\u4e2d\uff0c\u7dda\u7a0b\uff08Thread\uff09\u662f\u9032\u7a0b\uff08Process\uff09\u5167\u90e8\u7684\u4e00\u500b\u57f7\u884c\u55ae\u5143\u3002\u6bcf\u500b\u7dda\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u68e7\uff08Stack\uff09\uff0c\u7528\u65bc\u5b58\u5132\u5c40\u90e8\u8b8a\u91cf\u3001\u51fd\u6578\u53c3\u6578\u53ca\u8fd4\u56de\u5730\u5740\u7b49\u4fe1\u606f\u3002\u7dda\u7a0b\u68e7\u7684\u5927\u5c0f\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u6709\u8457\u91cd\u8981\u5f71\u97ff\uff0c\u56e0\u6b64\u7406\u89e3\u5176\u8a2d\u7f6e\u548c\u8abf\u6574\u662f\u958b\u767c\u8005\u5fc5\u9808\u638c\u63e1\u7684\u6280\u80fd\u4e4b\u4e00\u3002<\/p>\n<h2 id=\"%e7%b7%9a%e7%a8%8b%e6%a3%a7%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-tntSxawmYo\">\u7dda\u7a0b\u68e7\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u7dda\u7a0b\u68e7\u662f\u7dda\u7a0b\u904b\u884c\u6642\u7528\u4f86\u5b58\u5132\u6578\u64da\u7684\u5340\u57df\u3002\u6bcf\u7576\u7dda\u7a0b\u8abf\u7528\u4e00\u500b\u51fd\u6578\u6642\uff0c\u8a72\u51fd\u6578\u7684\u5c40\u90e8\u8b8a\u91cf\u548c\u53c3\u6578\u6703\u88ab\u63a8\u5165\u68e7\u4e2d\uff0c\u7576\u51fd\u6578\u8fd4\u56de\u6642\uff0c\u9019\u4e9b\u6578\u64da\u6703\u88ab\u5f48\u51fa\u3002\u7dda\u7a0b\u68e7\u7684\u5927\u5c0f\u6c7a\u5b9a\u4e86\u7dda\u7a0b\u53ef\u4ee5\u4f7f\u7528\u7684\u5167\u5b58\u91cf\uff0c\u904e\u5c0f\u7684\u68e7\u53ef\u80fd\u5c0e\u81f4\u68e7\u6ea2\u51fa\uff08Stack Overflow\uff09\uff0c\u800c\u904e\u5927\u7684\u68e7\u5247\u6703\u6d6a\u8cbb\u5167\u5b58\u8cc7\u6e90\u3002<\/p>\n<h2 id=\"linux-%e4%b8%ad%e7%b7%9a%e7%a8%8b%e6%a3%a7%e7%9a%84%e5%a4%a7%e5%b0%8f%e8%a8%ad%e7%bd%ae-tntSxawmYo\">Linux \u4e2d\u7dda\u7a0b\u68e7\u7684\u5927\u5c0f\u8a2d\u7f6e<\/h2>\n<p>\u5728 Linux \u4e2d\uff0c\u7dda\u7a0b\u68e7\u7684\u5927\u5c0f\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u65b9\u5f0f\u9032\u884c\u8a2d\u7f6e\u3002\u6700\u5e38\u898b\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 pthread \u5eab\u4e2d\u7684 <code>pthread_attr_setstacksize<\/code> \u51fd\u6578\u3002\u9019\u500b\u51fd\u6578\u5141\u8a31\u958b\u767c\u8005\u5728\u5275\u5efa\u7dda\u7a0b\u4e4b\u524d\u8a2d\u7f6e\u5176\u68e7\u5927\u5c0f\u3002<\/p>\n<pre><code>\n#include &lt;pthread.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nvoid* thread_function(void* arg) {\n    \/\/ \u7dda\u7a0b\u57f7\u884c\u7684\u4ee3\u78bc\n    return NULL;\n}\n\nint main() {\n    pthread_t thread;\n    pthread_attr_t attr;\n    size_t stacksize = 1024 * 1024; \/\/ \u8a2d\u7f6e\u68e7\u5927\u5c0f\u70ba 1MB\n\n    pthread_attr_init(&amp;attr);\n    pthread_attr_setstacksize(&amp;attr, stacksize);\n\n    if (pthread_create(&amp;thread, &amp;attr, thread_function, NULL) != 0) {\n        perror(\"pthread_create\");\n        exit(EXIT_FAILURE);\n    }\n\n    pthread_join(thread, NULL);\n    pthread_attr_destroy(&amp;attr);\n    return 0;\n}\n<\/code><\/pre>\n<h2 id=\"%e6%a3%a7%e5%a4%a7%e5%b0%8f%e7%9a%84%e5%bd%b1%e9%9f%bf-tntSxawmYo\">\u68e7\u5927\u5c0f\u7684\u5f71\u97ff<\/h2>\n<p>\u7dda\u7a0b\u68e7\u7684\u5927\u5c0f\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u6709\u8457\u76f4\u63a5\u7684\u5f71\u97ff\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u4e3b\u8981\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u5167\u5b58\u4f7f\u7528\uff1a<\/strong>\u904e\u5927\u7684\u68e7\u6703\u5c0e\u81f4\u5167\u5b58\u6d6a\u8cbb\uff0c\u7279\u5225\u662f\u5728\u5275\u5efa\u5927\u91cf\u7dda\u7a0b\u6642\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u7cfb\u7d71\u5167\u5b58\u4e0d\u8db3\u3002<\/li>\n<li><strong>\u68e7\u6ea2\u51fa\uff1a<\/strong>\u5982\u679c\u7dda\u7a0b\u7684\u68e7\u5927\u5c0f\u8a2d\u7f6e\u5f97\u904e\u5c0f\uff0c\u7576\u7dda\u7a0b\u57f7\u884c\u6df1\u5ea6\u905e\u6b78\u6216\u4f7f\u7528\u5927\u91cf\u5c40\u90e8\u8b8a\u91cf\u6642\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u68e7\u6ea2\u51fa\uff0c\u5f9e\u800c\u5f15\u767c\u7a0b\u5e8f\u5d29\u6f70\u3002<\/li>\n<li><strong>\u6027\u80fd\u5f71\u97ff\uff1a<\/strong>\u68e7\u7684\u5927\u5c0f\u4e5f\u6703\u5f71\u97ff\u4e0a\u4e0b\u6587\u5207\u63db\u7684\u6027\u80fd\u3002\u8f03\u5927\u7684\u68e7\u53ef\u80fd\u6703\u5c0e\u81f4\u66f4\u9577\u7684\u4e0a\u4e0b\u6587\u5207\u63db\u6642\u9593\uff0c\u5f9e\u800c\u5f71\u97ff\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e9%81%b8%e6%93%87%e5%90%88%e9%81%a9%e7%9a%84%e6%a3%a7%e5%a4%a7%e5%b0%8f-tntSxawmYo\">\u5982\u4f55\u9078\u64c7\u5408\u9069\u7684\u68e7\u5927\u5c0f<\/h2>\n<p>\u9078\u64c7\u5408\u9069\u7684\u68e7\u5927\u5c0f\u9700\u8981\u8003\u616e\u591a\u500b\u56e0\u7d20\uff0c\u5305\u62ec\u61c9\u7528\u7a0b\u5e8f\u7684\u7279\u6027\u3001\u7dda\u7a0b\u7684\u6578\u91cf\u4ee5\u53ca\u7cfb\u7d71\u7684\u5167\u5b58\u9650\u5236\u3002\u4e00\u822c\u4f86\u8aaa\uff0c\u958b\u767c\u8005\u61c9\u8a72\u6839\u64da\u5be6\u969b\u9700\u6c42\u9032\u884c\u6e2c\u8a66\uff0c\u4e26\u6839\u64da\u6e2c\u8a66\u7d50\u679c\u8abf\u6574\u68e7\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u5c0d\u65bc\u5927\u591a\u6578\u61c9\u7528\u7a0b\u5e8f\uff0c\u9ed8\u8a8d\u7684\u68e7\u5927\u5c0f\uff08\u901a\u5e38\u70ba 8MB\uff09\u5df2\u7d93\u8db3\u5920\u3002<\/li>\n<li>\u5982\u679c\u61c9\u7528\u7a0b\u5e8f\u4f7f\u7528\u5927\u91cf\u7684\u905e\u6b78\u8abf\u7528\uff0c\u5247\u61c9\u8003\u616e\u589e\u52a0\u68e7\u5927\u5c0f\u3002<\/li>\n<li>\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\uff0c\u61c9\u6839\u64da\u7dda\u7a0b\u7684\u6578\u91cf\u548c\u5167\u5b58\u4f7f\u7528\u60c5\u6cc1\u9032\u884c\u8abf\u6574\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tntSxawmYo\">\u7e3d\u7d50<\/h2>\n<p>\u7406\u89e3 Linux C \u7dda\u7a0b\u68e7\u5927\u5c0f\u53ca\u5176\u5f71\u97ff\u5c0d\u65bc\u958b\u767c\u9ad8\u6548\u7a69\u5b9a\u7684\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\u3002\u9069\u7576\u7684\u68e7\u5927\u5c0f\u8a2d\u7f6e\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u6e1b\u5c11\u5167\u5b58\u6d6a\u8cbb\uff0c\u4e26\u907f\u514d\u68e7\u6ea2\u51fa\u7b49\u554f\u984c\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u4e00\u500b\u91cd\u8981\u8003\u91cf\uff0c\u7279\u5225\u662f\u5728\u9999\u6e2f\u9019\u6a23\u7684\u5e02\u5834\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u53ef\u4ee5\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u66f4\u597d\u7684\u652f\u6301\u548c\u8cc7\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u7406\u89e3Linux C\u7dda\u7a0b\u68e7\u5927\u5c0f\u53ca\u5176\u5f71\u97ff\uff0c\u63a2\u8a0e\u5982\u4f55\u8a2d\u5b9a\u8207\u512a\u5316\u7dda\u7a0b\u68e7\u4ee5\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-198570","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=198570"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/198570\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=198570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=198570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=198570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}