{"id":16513,"date":"2024-11-18T12:23:57","date_gmt":"2024-11-18T04:23:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16513\/"},"modified":"2024-11-18T12:23:57","modified_gmt":"2024-11-18T04:23:57","slug":"linux%e9%a9%b1%e5%8a%a8-procfs%e6%8e%a5%e5%8f%a3%e5%88%9b%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16513\/","title":{"rendered":"Linux\u9a71\u52a8 | procfs\u63a5\u53e3\u521b\u5efa"},"content":{"rendered":"<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u4e0a\u7bc7\u4ecb\u7ecd\u4e86Linux\u9a71\u52a8\u4e2dsysfs\u63a5\u53e3\u7684\u521b\u5efa\uff0c\u4eca\u5929\u4ecb\u7ecdprocfs\u63a5\u53e3\u7684\u521b\u5efa\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">procfs\uff1a\u53ef\u5b9e\u73b0\u7c7b\u4f3ccat \/proc\/cpuinfo\u7684\u64cd\u4f5c<\/p>\n<h2> <span><\/span><span style=\"padding: 2px 10px 1px;border-top-right-radius: 3px;border-top-left-radius: 3px;margin-right: 3px\">procfs\u63a5\u53e3\u521b\u5efa<\/span><span style=\"vertical-align: bottom;border-bottom: 36px solid #efebe9;border-right: 20px solid transparent\"> <\/span> <\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5b9e\u73b0\u6548\u679c\uff1a<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u4f8b\u5982, \u5728\/proc\u4e0b\u521b\u5efa\u4e00\u4e2aclk\u8282\u70b9\uff0c\u901a\u8fc7cat \/proc\/clk\u53ef\u67e5\u770b\u5185\u5bb9\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/275\/014\/9eb374e712f4fd66e57ead0661ac4bcb-0.png\" class=\"aligncenter\" title=\"Linux\u9a71\u52a8 | procfs\u63a5\u53e3\u521b\u5efa\u63d2\u56fe\" alt=\"Linux\u9a71\u52a8 | procfs\u63a5\u53e3\u521b\u5efa\u63d2\u56fe\" \/>  <\/p>\n<p style=\"max-width:90%\">\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"border-top-width: 1px;text-align: left;min-width: 85px\" width=\"153\">\u7cfb\u7edf<\/th>\n<th style=\"border-top-width: 1px;text-align: left;min-width: 85px\" width=\"147\">\u5185\u6838\u7248\u672c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"min-width: 85px\" width=\"68\">Linux<\/td>\n<td style=\"min-width: 85px\" width=\"147\">4.9.88<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5728\u9a71\u52a8\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n\nstruct&nbsp;proc_dir_entry&nbsp;*my_proc_entry;\n\nstatic&nbsp;int&nbsp;proc_clk_show(struct&nbsp;seq_file&nbsp;*m,&nbsp;void&nbsp;*v)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/cat\u663e\u793a\u7684\u5185\u5bb9\n&nbsp;&nbsp;&nbsp;&nbsp;seq_printf(m,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"pll0:&nbsp;%u&nbsp;Mhz\n\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"pll1:&nbsp;%u&nbsp;Mhz\n\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"pll2:&nbsp;%u&nbsp;Mhz\n\",\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100,&nbsp;200,&nbsp;300);\n&nbsp;&nbsp;&nbsp;return&nbsp;0;\n}\n\nstatic&nbsp;int&nbsp;clk_info_open(struct&nbsp;inode&nbsp;*inode,&nbsp;struct&nbsp;file&nbsp;*filp)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;single_open(filp,&nbsp;proc_clk_show,&nbsp;NULL);\n}\n\nstatic&nbsp;struct&nbsp;file_operations&nbsp;myops&nbsp;=&nbsp;\n{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.owner&nbsp;=&nbsp;THIS_MODULE,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.open&nbsp;=&nbsp;clk_info_open,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.read&nbsp;=&nbsp;seq_read,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.llseek&nbsp;=&nbsp;seq_lseek,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.release&nbsp;=&nbsp;seq_release,\n};\n\nstatic&nbsp;int&nbsp;__init&nbsp;my_module_init(void)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u6ce8\u518cproc\u63a5\u53e3\n&nbsp;&nbsp;&nbsp;my_proc_entry&nbsp;=&nbsp;proc_create(\"clk\",&nbsp;0644,&nbsp;NULL,&nbsp;&amp;amp;myops);\n\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;\n}\n\nstatic&nbsp;void&nbsp;__exit&nbsp;my_module_exit(void)\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u6ce8\u9500proc\u63a5\u53e3\n&nbsp;&nbsp;&nbsp;proc_remove(my_proc_entry);\n}\n\nmodule_init(my_module_init);\nmodule_exit(my_module_exit);\nMODULE_LICENSE(\"GPL\");&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">procfs\u63a5\u53e3\u7684\u521b\u5efa\uff0c\u4e3b\u8981\u662f\u5b9e\u73b0struct file_operations\u7ed3\u6784\u4f53\uff0c\u7136\u540e\u901a\u8fc7proc_create\u51fd\u6570\u8fdb\u884c\u6ce8\u518c\uff0c\u901a\u8fc7proc_remove\u51fd\u6570\u8fdb\u884c\u6ce8\u9500\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">procfs\u901a\u5e38\u662f\u7528\u6765\u83b7\u53d6CPU\u3001\u5185\u5b58\u3001\u8fdb\u7a0b\u7b49\u5404\u79cd\u4fe1\u606f\uff0c\u4f8b\u5982cat \/proc\/cpuinfo\u3001cat \/proc\/meminfo\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u5b9e\u73b0.open\u6210\u5458\u51fd\u6570\u3002\u5f53\u4f7f\u7528cat\u547d\u4ee4\u67e5\u770b\/proc\u4e0b\u7684\u4fe1\u606f\u65f6\uff0c\u4f1a\u8c03\u7528\u5230.open\u5bf9\u5e94\u7684\u5b9e\u73b0\u51fd\u6570\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86seq_file\u63a5\u53e3\uff0c\u9700\u8981\u8bb0\u4f4f\u7684\u662f\uff0c<strong>procfs\u901a\u5e38\u4f1a\u548cseq_file\u63a5\u53e3\u4e00\u8d77\u4f7f\u7528<\/strong>\u3002seq_file\u662f\u4e00\u4e2a\u5e8f\u5217\u6587\u4ef6\u63a5\u53e3\uff0c<strong>\u5f53\u6211\u4eec\u521b\u5efa\u7684proc\u6570\u636e\u5185\u5bb9\u7531\u4e00\u7cfb\u5217\u6570\u636e\u987a\u5e8f\u7ec4\u5408\u800c\u6210\u6216\u8005\u662f\u6bd4\u8f83\u5927\u7684proc\u6587\u4ef6\u7cfb\u7edf\u65f6\uff0c\u90fd\u5efa\u8bae\u4f7f\u7528seq_file\u63a5\u53e3<\/strong>\uff0c\u4f8b\u5982cat \/proc\/meminfo\u5c31\u4f1a\u663e\u793a\u5f88\u591a\u5185\u5bb9\u3002<\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">seq_file\u63a5\u53e3\u4e3b\u8981\u5c31\u662f\u89e3\u51b3proc\u63a5\u53e3\u7f16\u7a0b\u5b58\u5728\u7684\u95ee\u9898\uff0c<strong>\u63a8\u8350\u5728proc\u63a5\u53e3\u7f16\u7a0b\u65f6\u4f7f\u7528seq_file\u63a5\u53e3<\/strong>\uff0c\u53e6\u5916.read\u3001.llseek\u3001.release\u6210\u5458\u51fd\u6570\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7528seq_read\u3001seq_lseek\u548cseq_release\u3002<\/p>\n<h2> <span><\/span><span style=\"padding: 2px 10px 1px;border-top-right-radius: 3px;border-top-left-radius: 3px;margin-right: 3px\">proc\u65b0\u63a5\u53e3<\/span><span style=\"vertical-align: bottom;border-bottom: 36px solid #efebe9;border-right: 20px solid transparent\"> <\/span> <\/h2>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u6ce8\u610f\uff0c\u5728\u8f83\u65b0\u7248\u672c\u7684\u5185\u6838\u4e2d\uff0cprocfs\u7684\u51fd\u6570\u63a5\u53e3\u6709\u6240\u53d8\u5316\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"border-top-width: 1px;text-align: left;min-width: 85px\" width=\"143\">\u7cfb\u7edf<\/th>\n<th style=\"border-top-width: 1px;text-align: left;min-width: 85px\" width=\"152\">\u5185\u6838\u7248\u672c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"min-width: 85px\" width=\"143\">Linux<\/td>\n<td style=\"min-width: 85px\" width=\"152\">5.10.111<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u5728\u9a71\u52a8\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n#include&nbsp;&lt;linux&gt;\n\nstruct&nbsp;proc_dir_entry&nbsp;*my_proc_entry;\n\nstatic&nbsp;int&nbsp;proc_clk_show(struct&nbsp;seq_file&nbsp;*m,&nbsp;void&nbsp;*v)\n{\n&nbsp;seq_printf(m,\n&nbsp;&nbsp;&nbsp;&nbsp;\"pll0:&nbsp;%lu&nbsp;Mhz\n\"\n&nbsp;&nbsp;&nbsp;&nbsp;\"pll1:&nbsp;%lu&nbsp;Mhz\n\"\n&nbsp;&nbsp;&nbsp;&nbsp;\"pll2:&nbsp;%lu&nbsp;Mhz\n\",\n&nbsp;&nbsp;&nbsp;&nbsp;100,&nbsp;200,&nbsp;300);\n&nbsp;return&nbsp;0;\n}\n\nstatic&nbsp;int&nbsp;clk_info_open(struct&nbsp;inode&nbsp;*inode,&nbsp;struct&nbsp;file&nbsp;*filp)\n{\n&nbsp;return&nbsp;single_open(filp,&nbsp;proc_clk_show,&nbsp;NULL);\n}\n\nstatic&nbsp;const&nbsp;struct&nbsp;proc_ops&nbsp;clk_stat_proc_fops&nbsp;=&nbsp;{\n&nbsp;.proc_open&nbsp;=&nbsp;clk_info_open,\n&nbsp;.proc_read&nbsp;=&nbsp;&nbsp;seq_read,\n&nbsp;.proc_lseek&nbsp;=&nbsp;seq_lseek,\n&nbsp;.proc_release&nbsp;=&nbsp;seq_release,\n};\n\nstatic&nbsp;int&nbsp;__init&nbsp;my_module_init(void)\n{\n&nbsp;&nbsp;&nbsp;my_proc_entry&nbsp;=&nbsp;proc_create(\"clk\",&nbsp;0,&nbsp;NULL,&nbsp;&amp;amp;clk_stat_proc_fops);\n\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;\n}\n\nstatic&nbsp;void&nbsp;__exit&nbsp;my_module_exit(void)\n{\n&nbsp;&nbsp;&nbsp;proc_remove(my_proc_entry);\n}\n\nmodule_init(my_module_init);\nmodule_exit(my_module_exit);\nMODULE_LICENSE(\"GPL\");&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;&lt;\/linux&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"padding-top: 8px;padding-bottom: 8px;line-height: 26px\">\u65b0\u7684proc\u63a5\u53e3\u4e2d\uff0c\u5c06\u539f\u6765\u7684struct file_operations\u6362\u6210\u4e86struct proc_ops\uff0c\u5176\u4e2d\u6210\u5458\u51fd\u6570\u4e5f\u6dfb\u52a0\u4e86\u5bf9\u5e94\u7684\u524d\u7f00proc\uff0c\u4f46\u672c\u8d28\u8fd8\u662f\u4e00\u6837\u7684\uff0c\u53ea\u662f\u6362\u4e86\u540d\u5b57\uff0c\u66f4\u52a0\u89c4\u8303\u4e86\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLinux\u9a71\u52a8 | procfs\u63a5\u53e3\u521b\u5efa\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>\u4e0a\u7bc7\u4ecb\u7ecd\u4e86Linux\u9a71\u52a8\u4e2dsysfs\u63a5\u53e3\u7684\u521b\u5efa\uff0c\u4eca\u5929\u4ecb\u7ecdprocfs\u63a5\u53e3\u7684\u521b\u5efa\u3002 procfs\uff1a\u53ef\u5b9e\u73b0\u7c7b\u4f3ccat \/proc\/cpuinfo\u7684\u64cd\u4f5c procfs\u63a5\u53e3\u521b\u5efa \u5b9e\u73b0\u6548\u679c\uff1a \u4f8b\u5982, \u5728\/proc\u4e0b\u521b\u5efa\u4e00\u4e2aclk\u8282\u70b9\uff0c\u901a\u8fc7cat \/proc\/clk\u53ef\u67e5\u770b\u5185\u5bb9\uff1a \u4ee3\u7801\u5b9e\u73b0\uff1a \u7cfb\u7edf \u5185\u6838\u7248\u672c Linux 4.9.88 \u5728\u9a71\u52a8\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a #include&nbsp;&lt;linux&gt; #include&nbsp;&lt;linux&gt; #include&nbsp;&lt;linux&gt; #include&nbsp;&lt;linux&gt; #include&nbsp;&lt;linux&gt; struct&nbsp;proc_dir_entry&nbsp;*my_proc_entry; static&nbsp;int&nbsp;proc_clk_show(struct&nbsp;seq_file&nbsp;*m,&nbsp;void&nbsp;*v) { &nbsp;&nbsp;&nbsp;&nbsp;\/\/cat\u663e\u793a\u7684\u5185\u5bb9 &nbsp;&nbsp;&nbsp;&nbsp;seq_printf(m, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;pll0:&nbsp;%u&nbsp;Mhz &#8221; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;pll1:&nbsp;%u&nbsp;Mhz &#8221; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;pll2:&nbsp;%u&nbsp;Mhz &#8220;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100,&nbsp;200,&nbsp;300); &nbsp;&nbsp;&nbsp;return&nbsp;0; } static&nbsp;int&nbsp;clk_info_open(struct&nbsp;inode&nbsp;*inode,&nbsp;struct&nbsp;file&nbsp;*filp) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;single_open(filp,&nbsp;proc_clk_show,&nbsp;NULL); } static&nbsp;struct&nbsp;file_operations&nbsp;myops&nbsp;=&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.owner&nbsp;=&nbsp;THIS_MODULE, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.open&nbsp;=&nbsp;clk_info_open, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.read&nbsp;=&nbsp;seq_read, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.llseek&nbsp;=&nbsp;seq_lseek, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.release&nbsp;=&nbsp;seq_release, }; static&nbsp;int&nbsp;__init&nbsp;my_module_init(void) { &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u6ce8\u518cproc\u63a5\u53e3 &nbsp;&nbsp;&nbsp;my_proc_entry&nbsp;=&nbsp;proc_create(&#8220;clk&#8221;,&nbsp;0644,&nbsp;NULL,&nbsp;&amp;amp;myops); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; } static&nbsp;void&nbsp;__exit&nbsp;my_module_exit(void) { &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u6ce8\u9500proc\u63a5\u53e3 &nbsp;&nbsp;&nbsp;proc_remove(my_proc_entry); [&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-16513","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16513","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=16513"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16513\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}