LinuxÇý¶¯ | procfs½Ó¿Ú½¨Éè
ÉÏƪÏÈÈÝÁËLinuxÇý¶¯ÖÐsysfs½Ó¿ÚµÄ½¨É裬½ñÌìÏÈÈÝprocfs½Ó¿ÚµÄ½¨Éè¡£
procfs£º¿ÉʵÏÖÀàËÆcat /proc/cpuinfoµÄ²Ù×÷
procfs½Ó¿Ú½¨Éè
ʵÏÖЧ¹û£º
ÀýÈç, ÔÚ/procϽ¨ÉèÒ»¸öclk½Úµã£¬Í¨¹ýcat /proc/clk¿ÉÉó²éÄÚÈÝ£º
´úÂëʵÏÖ£º
ϵͳ | Äں˰汾 |
---|---|
Linux | 4.9.88 |
ÔÚÇý¶¯ÖÐÌí¼ÓÒÔÏ´úÂ룺
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> struct proc_dir_entry *my_proc_entry; static int proc_clk_show(struct seq_file *m, void *v) { //catÏÔʾµÄÄÚÈÝ seq_printf(m, "pll0: %u Mhz\n" "pll1: %u Mhz\n" "pll2: %u Mhz\n", 100, 200, 300); return 0; } static int clk_info_open(struct inode *inode, struct file *filp) { return single_open(filp, proc_clk_show, NULL); } static struct file_operations myops = { .owner = THIS_MODULE, .open = clk_info_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release, }; static int __init my_module_init(void) { //×¢²áproc½Ó¿Ú my_proc_entry = proc_create("clk", 0644, NULL, &myops); return 0; } static void __exit my_module_exit(void) { //×¢Ïúproc½Ó¿Ú proc_remove(my_proc_entry); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL");
µÇ¼ºó¸´ÖÆ
procfs½Ó¿ÚµÄ½¨É裬Ö÷ÒªÊÇʵÏÖstruct file_operations½á¹¹Ì壬Ȼºóͨ¹ýproc_createº¯Êý¾ÙÐÐ×¢²á£¬Í¨¹ýproc_removeº¯Êý¾ÙÐÐ×¢Ïú¡£
procfsͨ³£ÊÇÓÃÀ´»ñÈ¡CPU¡¢ÄÚ´æ¡¢Àú³ÌµÈÖÖÖÖÐÅÏ¢£¬ÀýÈçcat /proc/cpuinfo¡¢cat /proc/meminfo£¬ÒÔÊÇÎÒÃÇÖ»ÐèҪʵÏÖ.open³ÉÔ±º¯Êý¡£µ±Ê¹ÓÃcatÏÂÁîÉó²é/procϵÄÐÅϢʱ£¬»áŲÓõ½.open¶ÔÓ¦µÄʵÏÖº¯Êý¡£
ÕâÀïÎÒÃÇʹÓÃÁËseq_file½Ó¿Ú£¬ÐèÒª¼Ç×ŵÄÊÇ£¬procfsͨ³£»áºÍseq_file½Ó¿ÚÒ»ÆðʹÓá£seq_fileÊÇÒ»¸öÐòÁÐÎļþ½Ó¿Ú£¬µ±ÎÒÃǽ¨ÉèµÄprocÊý¾ÝÄÚÈÝÓÉһϵÁÐÊý¾Ý˳Ðò×éºÏ¶ø³É»òÕßÊǽÏÁ¿´óµÄprocÎļþϵͳʱ£¬¶¼½¨ÒéʹÓÃseq_file½Ó¿Ú£¬ÀýÈçcat /proc/meminfo¾Í»áÏÔʾÐí¶àÄÚÈÝ¡£
seq_file½Ó¿ÚÖ÷Òª¾ÍÊǽâ¾öproc½Ó¿Ú±à³Ì±£´æµÄÎÊÌ⣬ÍƼöÔÚproc½Ó¿Ú±à³ÌʱʹÓÃseq_file½Ó¿Ú£¬ÁíÍâ.read¡¢.llseek¡¢.release³ÉÔ±º¯ÊýÒ²¿ÉÒÔÖ±½ÓÓÃseq_read¡¢seq_lseekºÍseq_release¡£
procнӿÚ
×¢ÖØ£¬ÔÚ½Ïа汾µÄÄÚºËÖУ¬procfsµÄº¯Êý½Ó¿ÚÓÐËùת±ä¡£
ϵͳ | Äں˰汾 |
---|---|
Linux | 5.10.111 |
ÔÚÇý¶¯ÖÐÌí¼ÓÒÔÏ´úÂ룺
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> struct proc_dir_entry *my_proc_entry; static int proc_clk_show(struct seq_file *m, void *v) { seq_printf(m, "pll0: %lu Mhz\n" "pll1: %lu Mhz\n" "pll2: %lu Mhz\n", 100, 200, 300); return 0; } static int clk_info_open(struct inode *inode, struct file *filp) { return single_open(filp, proc_clk_show, NULL); } static const struct proc_ops clk_stat_proc_fops = { .proc_open = clk_info_open, .proc_read = seq_read, .proc_lseek = seq_lseek, .proc_release = seq_release, }; static int __init my_module_init(void) { my_proc_entry = proc_create("clk", 0, NULL, &clk_stat_proc_fops); return 0; } static void __exit my_module_exit(void) { proc_remove(my_proc_entry); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL");
µÇ¼ºó¸´ÖÆ
еÄproc½Ó¿ÚÖУ¬½«ÔÀ´µÄstruct file_operations»»³ÉÁËstruct proc_ops£¬ÆäÖгÉÔ±º¯ÊýÒ²Ìí¼ÓÁ˶ÔÓ¦µÄǰ׺proc£¬µ«ÊµÖÊÕÕ¾ÉÒ»ÑùµÄ£¬Ö»ÊÇ»»ÁËÃû×Ö£¬Ô½·¢¹æ·¶ÁËһЩ¡£
ÒÔÉϾÍÊÇLinuxÇý¶¯ | procfs½Ó¿Ú½¨ÉèµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡