{"id":17787,"date":"2024-11-18T08:05:22","date_gmt":"2024-11-18T00:05:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17787\/"},"modified":"2024-11-18T08:05:22","modified_gmt":"2024-11-18T00:05:22","slug":"linux%e8%bf%9b%e7%a8%8b%e7%9a%84%e4%b8%89%e4%b8%aa%e9%83%a8%e5%88%86%e5%8f%8a%e5%85%b6%e4%bd%9c%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17787\/","title":{"rendered":"Linux\u8fdb\u7a0b\u7684\u4e09\u4e2a\u90e8\u5206\u53ca\u5176\u4f5c\u7528"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/171092917039085.jpg\" class=\"aligncenter\" title=\"Linux\u8fdb\u7a0b\u7684\u4e09\u4e2a\u90e8\u5206\u53ca\u5176\u4f5c\u7528\u63d2\u56fe\" alt=\"Linux\u8fdb\u7a0b\u7684\u4e09\u4e2a\u90e8\u5206\u53ca\u5176\u4f5c\u7528\u63d2\u56fe\" \/><\/p>\n<p>Linux\u64cd\u4f5c\u7cfb\u7edf\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u5f00\u6e90\u64cd\u4f5c\u7cfb\u7edf\uff0c\u91c7\u7528\u4e86\u591a\u4efb\u52a1\u5904\u7406\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u8fdb\u7a0b\u3002\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\uff0c\u4e00\u4e2a\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u3002Linux\u8fdb\u7a0b\u53ef\u4ee5\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff09\u3001\u5185\u6838\u6808\u548c\u7528\u6237\u6808\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u4e2a\u90e8\u5206\u7684\u4f5c\u7528\uff0c\u5e76\u4e14\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b83\u4eec\u7684\u5b9e\u9645\u5e94\u7528\u3002<\/p>\n<p>\u4e00\u3001\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff09<\/p>\n<p>\u8fdb\u7a0b\u63a7\u5236\u5757\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7528\u6765\u7ba1\u7406\u8fdb\u7a0b\u4fe1\u606f\u7684\u6570\u636e\u7ed3\u6784\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u8fdb\u7a0b\u63a7\u5236\u5757\uff0c\u7528\u6765\u4fdd\u5b58\u8fdb\u7a0b\u7684\u72b6\u6001\u3001\u6807\u8bc6\u7b26\u3001\u4f18\u5148\u7ea7\u7b49\u4fe1\u606f\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u88ab\u521b\u5efa\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u63a7\u5236\u5757\uff0c\u7528\u6765\u8ddf\u8e2a\u5e76\u7ba1\u7406\u8be5\u8fdb\u7a0b\u7684\u8fd0\u884c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684C\u8bed\u8a00\u7a0b\u5e8f\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u5e76\u8bbf\u95ee\u5176\u8fdb\u7a0b\u63a7\u5236\u5757\u4fe1\u606f\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;sys&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    pid_t pid = fork();\n\n    if (pid == 0) {\n        printf(\"Child process PID: %d\n\", getpid());\n    } else {\n        printf(\"Parent process PID: %d\n\", getpid());\n    }\n\n    return 0;\n}&lt;\/unistd.h&gt;&lt;\/sys&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528fork()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\u3002fork()\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0bID\uff0c\u901a\u8fc7\u5224\u65ad\u8fd4\u56de\u503c\u7684\u5927\u5c0f\u53ef\u4ee5\u533a\u5206\u7236\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4e14\u901a\u8fc7getpid()\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684PID\u3002<\/p>\n<p>\u4e8c\u3001\u5185\u6838\u6808<\/p>\n<p>\u5185\u6838\u6808\u662f\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u4e0b\u7528\u6765\u4fdd\u5b58\u4e34\u65f6\u6570\u636e\u548c\u72b6\u6001\u4fe1\u606f\u7684\u6570\u636e\u7ed3\u6784\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u6267\u884c\u65f6\uff0c\u4f1a\u4f7f\u7528\u5185\u6838\u6808\u6765\u4fdd\u5b58\u51fd\u6570\u8c03\u7528\u7684\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u3001\u8fd4\u56de\u5730\u5740\u7b49\u4fe1\u606f\u3002\u5185\u6838\u6808\u5728\u8fdb\u7a0b\u5207\u6362\u6216\u8005\u7cfb\u7edf\u8c03\u7528\u65f6\u8d77\u7740\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u786e\u4fdd\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u8fd0\u884c\u65f6\u4e0d\u4f1a\u51fa\u73b0\u6570\u636e\u6df7\u4e71\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6c47\u7f16\u4ee3\u7801\u7247\u6bb5\uff0c\u5c55\u793a\u4e86\u5185\u6838\u6808\u7684\u4f7f\u7528\uff1a<\/p>\n<pre>section .data\n    message db 'Hello, World!', 0\n\nsection .text\n    global _start\n\n_start:\n    mov eax, 4       ; sys_write\n    mov ebx, 1       ; file descriptor stdout\n    mov ecx, message ; message address\n    mov edx, 13      ; message length\n    int 0x80         ; trigger system call<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u6bb5\u6c47\u7f16\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86\u5185\u6838\u6001\u7684\u7cfb\u7edf\u8c03\u7528sys_write\u6765\u5411\u63a7\u5236\u53f0\u8f93\u51fa\u4e00\u6bb5\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u5c06\u53c2\u6570\u4fdd\u5b58\u5728\u5bc4\u5b58\u5668\u4e2d\uff0c\u53ef\u4ee5\u907f\u514d\u5728\u5185\u6838\u6001\u4e0b\u4f7f\u7528\u7528\u6237\u6001\u6808\u9020\u6210\u7684\u6570\u636e\u51b2\u7a81\u3002<\/p>\n<p>\u4e09\u3001\u7528\u6237\u6808<\/p>\n<p>\u7528\u6237\u6808\u662f\u7528\u6765\u4fdd\u5b58\u7528\u6237\u6001\u4e0b\u51fd\u6570\u8c03\u7528\u7684\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u7b49\u6570\u636e\u7684\u6570\u636e\u7ed3\u6784\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u8fd0\u884c\u65f6\u90fd\u4f1a\u4f7f\u7528\u81ea\u5df1\u7684\u7528\u6237\u6808\u6765\u4fdd\u6301\u7a0b\u5e8f\u6267\u884c\u7684\u4e0a\u4e0b\u6587\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u4ece\u7528\u6237\u6001\u5207\u6362\u5230\u5185\u6838\u6001\u65f6\uff0c\u7528\u6237\u6808\u4e2d\u7684\u6570\u636e\u4f1a\u88ab\u4fdd\u5b58\u5728\u5185\u6838\u6808\u4e2d\uff0c\u4ee5\u4fbf\u5728\u4e4b\u540e\u7684\u6062\u590d\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684C\u8bed\u8a00\u7a0b\u5e8f\uff0c\u5c55\u793a\u4e86\u7528\u6237\u6808\u7684\u4f7f\u7528\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n\nvoid func(int n) {\n    int result = n * n;\n    printf(\"Result: %d\n\", result);\n}\n\nint main() {\n    int num = 5;\n    func(num);\n    return 0;\n}&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570func\u6765\u8ba1\u7b97\u8f93\u5165\u53c2\u6570\u7684\u5e73\u65b9\uff0c\u5e76\u4e14\u5728\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528\u8be5\u51fd\u6570\u3002\u5728\u51fd\u6570\u8c03\u7528\u8fc7\u7a0b\u4e2d\uff0c\u53c2\u6570n\u548c\u5c40\u90e8\u53d8\u91cfresult\u90fd\u4f1a\u88ab\u4fdd\u5b58\u5728\u7528\u6237\u6808\u4e2d\uff0c\u786e\u4fdd\u4e86\u51fd\u6570\u8c03\u7528\u7684\u6b63\u786e\u6267\u884c\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cLinux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u8fdb\u7a0b\u53ef\u4ee5\u5206\u4e3a\u8fdb\u7a0b\u63a7\u5236\u5757\u3001\u5185\u6838\u6808\u548c\u7528\u6237\u6808\u4e09\u4e2a\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u90fd\u6709\u7740\u4e0d\u540c\u7684\u4f5c\u7528\u548c\u529f\u80fd\u3002\u4e86\u89e3\u8fd9\u4e9b\u90e8\u5206\u7684\u539f\u7406\u548c\u5b9e\u9645\u5e94\u7528\uff0c\u6709\u52a9\u4e8e\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u8fdb\u7a0b\u7ba1\u7406\u7684\u673a\u5236\uff0c\u5e76\u4e14\u7f16\u5199\u51fa\u66f4\u52a0\u9ad8\u6548\u548c\u7a33\u5b9a\u7684\u7a0b\u5e8f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLinux\u8fdb\u7a0b\u7684\u4e09\u4e2a\u90e8\u5206\u53ca\u5176\u4f5c\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u64cd\u4f5c\u7cfb\u7edf\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u5f00\u6e90\u64cd\u4f5c\u7cfb\u7edf\uff0c\u91c7\u7528\u4e86\u591a\u4efb\u52a1\u5904\u7406\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u8fdb\u7a0b\u3002\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\uff0c\u4e00\u4e2a\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u4f1a\u88ab\u64cd\u4f5c\u7cfb\u7edf\u5206\u914d\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u3002Linux\u8fdb\u7a0b\u53ef\u4ee5\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff09\u3001\u5185\u6838\u6808\u548c\u7528\u6237\u6808\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u4e2a\u90e8\u5206\u7684\u4f5c\u7528\uff0c\u5e76\u4e14\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b83\u4eec\u7684\u5b9e\u9645\u5e94\u7528\u3002 \u4e00\u3001\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff09 \u8fdb\u7a0b\u63a7\u5236\u5757\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7528\u6765\u7ba1\u7406\u8fdb\u7a0b\u4fe1\u606f\u7684\u6570\u636e\u7ed3\u6784\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u8fdb\u7a0b\u63a7\u5236\u5757\uff0c\u7528\u6765\u4fdd\u5b58\u8fdb\u7a0b\u7684\u72b6\u6001\u3001\u6807\u8bc6\u7b26\u3001\u4f18\u5148\u7ea7\u7b49\u4fe1\u606f\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u88ab\u521b\u5efa\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u63a7\u5236\u5757\uff0c\u7528\u6765\u8ddf\u8e2a\u5e76\u7ba1\u7406\u8be5\u8fdb\u7a0b\u7684\u8fd0\u884c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684C\u8bed\u8a00\u7a0b\u5e8f\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u5e76\u8bbf\u95ee\u5176\u8fdb\u7a0b\u63a7\u5236\u5757\u4fe1\u606f\uff1a #include &lt;stdio.h&gt; #include &lt;sys&gt; #include &lt;unistd.h&gt; int main() { pid_t pid = fork(); if (pid == 0) { printf(&#8220;Child process PID: %d &#8220;, getpid()); } else { printf(&#8220;Parent process PID: %d &#8220;, getpid()); } return 0; }&lt;\/unistd.h&gt;&lt;\/sys&gt;&lt;\/stdio.h&gt; \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528fork()\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b50\u8fdb\u7a0b\u3002fork()\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0bID\uff0c\u901a\u8fc7\u5224\u65ad\u8fd4\u56de\u503c\u7684\u5927\u5c0f\u53ef\u4ee5\u533a\u5206\u7236\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4e14\u901a\u8fc7getpid()\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684PID\u3002 \u4e8c\u3001\u5185\u6838\u6808 \u5185\u6838\u6808\u662f\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u4e0b\u7528\u6765\u4fdd\u5b58\u4e34\u65f6\u6570\u636e\u548c\u72b6\u6001\u4fe1\u606f\u7684\u6570\u636e\u7ed3\u6784\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u6267\u884c\u65f6\uff0c\u4f1a\u4f7f\u7528\u5185\u6838\u6808\u6765\u4fdd\u5b58\u51fd\u6570\u8c03\u7528\u7684\u53c2\u6570\u3001\u5c40\u90e8\u53d8\u91cf\u3001\u8fd4\u56de\u5730\u5740\u7b49\u4fe1\u606f\u3002\u5185\u6838\u6808\u5728\u8fdb\u7a0b\u5207\u6362\u6216\u8005\u7cfb\u7edf\u8c03\u7528\u65f6\u8d77\u7740\u91cd\u8981\u7684\u4f5c\u7528\uff0c\u786e\u4fdd\u8fdb\u7a0b\u5728\u5185\u6838\u6001\u8fd0\u884c\u65f6\u4e0d\u4f1a\u51fa\u73b0\u6570\u636e\u6df7\u4e71\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6c47\u7f16\u4ee3\u7801\u7247\u6bb5\uff0c\u5c55\u793a\u4e86\u5185\u6838\u6808\u7684\u4f7f\u7528\uff1a section .data message db &#8216;Hello, World!&#8217;, 0 section .text [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-17787","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=17787"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17787\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}