{"id":17815,"date":"2024-11-18T11:39:26","date_gmt":"2024-11-18T03:39:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17815\/"},"modified":"2024-11-18T11:39:26","modified_gmt":"2024-11-18T03:39:26","slug":"%e6%8e%a2%e7%a7%98linux%e5%86%85%e6%a0%b8%e5%8a%9f%e8%83%bd%ef%bc%9a%e4%ba%94%e5%a4%a7%e9%83%a8%e5%88%86%e7%9a%84%e8%af%a6%e7%bb%86%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17815\/","title":{"rendered":"\u63a2\u79d8Linux\u5185\u6838\u529f\u80fd\uff1a\u4e94\u5927\u90e8\u5206\u7684\u8be6\u7ec6\u4ecb\u7ecd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/171098622767913.jpg\" class=\"aligncenter\" title=\"\u63a2\u79d8Linux\u5185\u6838\u529f\u80fd\uff1a\u4e94\u5927\u90e8\u5206\u7684\u8be6\u7ec6\u4ecb\u7ecd\u63d2\u56fe\" alt=\"\u63a2\u79d8Linux\u5185\u6838\u529f\u80fd\uff1a\u4e94\u5927\u90e8\u5206\u7684\u8be6\u7ec6\u4ecb\u7ecd\u63d2\u56fe\" \/><\/p>\n<p>Linux\u5185\u6838\u4f5c\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u627f\u62c5\u7740\u7ba1\u7406\u786c\u4ef6\u8d44\u6e90\u3001\u63d0\u4f9b\u7cfb\u7edf\u8c03\u7528\u7b49\u91cd\u8981\u529f\u80fd\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8Linux\u5185\u6838\u7684\u4e94\u5927\u90e8\u5206\uff0c\u5305\u62ec\u8fdb\u7a0b\u7ba1\u7406\u3001\u6587\u4ef6\u7cfb\u7edf\u3001\u7f51\u7edc\u901a\u4fe1\u3001\u8bbe\u5907\u9a71\u52a8\u548c\u5185\u5b58\u7ba1\u7406\uff0c\u5e76\u63d0\u4f9b\u8be6\u7ec6\u7684\u4ecb\u7ecd\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<h2>\u4e00\u3001\u8fdb\u7a0b\u7ba1\u7406<\/h2>\n<h3>\u8fdb\u7a0b\u7684\u521b\u5efa<\/h3>\n<p>\u5728Linux\u5185\u6838\u4e2d\uff0c\u8fdb\u7a0b\u7684\u521b\u5efa\u901a\u8fc7fork()\u7cfb\u7edf\u8c03\u7528\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    pid_t pid = fork();\n    \n    if (pid &lt;h3&gt;\u8fdb\u7a0b\u7684\u8c03\u5ea6&lt;\/h3&gt;\n&lt;p&gt;Linux\u5185\u6838\u4f7f\u7528\u8c03\u5ea6\u5668\u6765\u51b3\u5b9a\u8fdb\u7a0b\u7684\u8fd0\u884c\u987a\u5e8f\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\u6765\u5f71\u54cd\u8c03\u5ea6\u884c\u4e3a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4fee\u6539\u8fdb\u7a0b\u4f18\u5148\u7ea7\u7684\u793a\u4f8b\u4ee3\u7801\uff1a&lt;\/p&gt;\n&lt;pre class=\"brush:c;toolbar:false;\"&gt;#include &lt;stdio.h&gt;\n#include &lt;sys&gt;\n\nint main() {\n    int ret;\n    const int priority = 10;\n    \n    ret = setpriority(PRIO_PROCESS, 0, priority);\n    \n    if (ret == 0) {\n        printf(\"Set priority successfully\n\");\n    } else {\n        perror(\"setpriority failed\");\n    }\n\n    return 0;\n}&lt;\/sys&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2>\u4e8c\u3001\u6587\u4ef6\u7cfb\u7edf<\/h2>\n<h3>\u6587\u4ef6\u7684\u521b\u5efa\u548c\u5199\u5165<\/h3>\n<p>Linux\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7cfb\u7edf\u8c03\u7528\u6765\u8fdb\u884c\u6587\u4ef6\u7684\u521b\u5efa\u548c\u5199\u5165\u64cd\u4f5c\uff0c\u6bd4\u5982open()\u3001write()\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u5199\u5165\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    int fd = open(\"test.txt\", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);\n    \n    if (fd &lt;h3&gt;\u6587\u4ef6\u7684\u8bfb\u53d6\u548c\u5173\u95ed&lt;\/h3&gt;\n&lt;p&gt;\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528read()\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4f7f\u7528close()\u6765\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u793a\u4f8b\uff1a&lt;\/p&gt;\n&lt;pre class=\"brush:c;toolbar:false;\"&gt;#include &lt;stdio.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    int fd = open(\"test.txt\", O_RDONLY);\n    \n    if (fd &lt;h2&gt;\u4e09\u3001\u7f51\u7edc\u901a\u4fe1&lt;\/h2&gt;\n&lt;h3&gt;Socket\u7f16\u7a0b&lt;\/h3&gt;\n&lt;p&gt;Linux\u5185\u6838\u652f\u6301Socket\u7f16\u7a0b\uff0c\u901a\u8fc7Socket\u53ef\u4ee5\u8fdb\u884c\u7f51\u7edc\u901a\u4fe1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684TCP\u5ba2\u6237\u7aef\u793a\u4f8b\uff1a&lt;\/p&gt;\n&lt;pre class=\"brush:c;toolbar:false;\"&gt;#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;sys&gt;\n#include &lt;netinet&gt;\n#include &lt;string.h&gt;\n\nint main() {\n    int sockfd = socket(AF_INET, SOCK_STREAM, 0);\n\n    struct sockaddr_in server_addr;\n    server_addr.sin_family = AF_INET;\n    server_addr.sin_port = htons(8080);\n    server_addr.sin_addr.s_addr = inet_addr(\"127.0.0.1\");\n\n    connect(sockfd, (struct sockaddr*)&amp;amp;server_addr, sizeof(server_addr));\n\n    const char* message = \"Hello, Server!\";\n    send(sockfd, message, strlen(message), 0);\n\n    close(sockfd);\n\n    return 0;\n}&lt;\/string.h&gt;&lt;\/netinet&gt;&lt;\/sys&gt;&lt;\/stdlib.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2>\u56db\u3001\u8bbe\u5907\u9a71\u52a8<\/h2>\n<p>Linux\u5185\u6838\u4e2d\u7684\u8bbe\u5907\u9a71\u52a8\u662f\u5b9e\u73b0\u786c\u4ef6\u4e0e\u5185\u6838\u4e4b\u95f4\u901a\u4fe1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u5185\u6838\u6a21\u5757\u6765\u52a0\u8f7d\u8bbe\u5907\u9a71\u52a8\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u8bbe\u5907\u9a71\u52a8\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;linux&gt;\n#include &lt;linux&gt;\n#include &lt;linux&gt;\n\nstatic int __init mydriver_init(void) {\n    printk(KERN_INFO \"My driver initialized\n\");\n    return 0;\n}\n\nstatic void __exit mydriver_exit(void) {\n    printk(KERN_INFO \"My driver exited\n\");\n}\n\nmodule_init(mydriver_init);\nmodule_exit(mydriver_exit);\n\nMODULE_LICENSE(\"GPL\");\nMODULE_AUTHOR(\"Your Name\");&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2>\u4e94\u3001\u5185\u5b58\u7ba1\u7406<\/h2>\n<h3>\u5185\u5b58\u5206\u914d\u4e0e\u91ca\u653e<\/h3>\n<p>Linux\u5185\u6838\u63d0\u4f9b\u4e86kmalloc()\u548ckfree()\u51fd\u6570\u6765\u8fdb\u884c\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5185\u5b58\u5206\u914d\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;linux&gt;\n\nvoid* ptr = kmalloc(1024, GFP_KERNEL);\nif (!ptr) {\n    printk(KERN_ERR \"Memory allocation failed\n\");\n}\n\nkfree(ptr);&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u662f\u5bf9Linux\u5185\u6838\u4e2d\u4e94\u5927\u90e8\u5206\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5305\u62ec\u8fdb\u7a0b\u7ba1\u7406\u3001\u6587\u4ef6\u7cfb\u7edf\u3001\u7f51\u7edc\u901a\u4fe1\u3001\u8bbe\u5907\u9a71\u52a8\u548c\u5185\u5b58\u7ba1\u7406\u3002\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u7684\u5c55\u793a\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u66f4\u6df1\u5165\u4e86\u89e3Linux\u5185\u6838\u7684\u529f\u80fd\u548c\u5b9e\u73b0\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u63a2\u79d8Linux\u5185\u6838\u529f\u80fd\uff1a\u4e94\u5927\u90e8\u5206\u7684\u8be6\u7ec6\u4ecb\u7ecd\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\u5185\u6838\u4f5c\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u627f\u62c5\u7740\u7ba1\u7406\u786c\u4ef6\u8d44\u6e90\u3001\u63d0\u4f9b\u7cfb\u7edf\u8c03\u7528\u7b49\u91cd\u8981\u529f\u80fd\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8Linux\u5185\u6838\u7684\u4e94\u5927\u90e8\u5206\uff0c\u5305\u62ec\u8fdb\u7a0b\u7ba1\u7406\u3001\u6587\u4ef6\u7cfb\u7edf\u3001\u7f51\u7edc\u901a\u4fe1\u3001\u8bbe\u5907\u9a71\u52a8\u548c\u5185\u5b58\u7ba1\u7406\uff0c\u5e76\u63d0\u4f9b\u8be6\u7ec6\u7684\u4ecb\u7ecd\u548c\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u8fdb\u7a0b\u7ba1\u7406 \u8fdb\u7a0b\u7684\u521b\u5efa \u5728Linux\u5185\u6838\u4e2d\uff0c\u8fdb\u7a0b\u7684\u521b\u5efa\u901a\u8fc7fork()\u7cfb\u7edf\u8c03\u7528\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; int main() { pid_t pid = fork(); if (pid &lt;h3&gt;\u8fdb\u7a0b\u7684\u8c03\u5ea6&lt;\/h3&gt; &lt;p&gt;Linux\u5185\u6838\u4f7f\u7528\u8c03\u5ea6\u5668\u6765\u51b3\u5b9a\u8fdb\u7a0b\u7684\u8fd0\u884c\u987a\u5e8f\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\u6765\u5f71\u54cd\u8c03\u5ea6\u884c\u4e3a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4fee\u6539\u8fdb\u7a0b\u4f18\u5148\u7ea7\u7684\u793a\u4f8b\u4ee3\u7801\uff1a&lt;\/p&gt; &lt;pre class=&#8221;brush:c;toolbar:false;&#8221;&gt;#include &lt;stdio.h&gt; #include &lt;sys&gt; int main() { int ret; const int priority = 10; ret = setpriority(PRIO_PROCESS, 0, priority); if (ret == 0) { printf(&#8220;Set priority successfully &#8220;); } else { perror(&#8220;setpriority failed&#8221;); } return [&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-17815","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17815","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=17815"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17815\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}