×ðÁú¿­Ê±¹ÙÍøµÇ¼

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½Ó¿Ú½¨ÉèµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ