linux 读取proc文件之seq

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  static void my_stop (struct seq_file *m, void *v)

  {

  read_unlock_bh(tmp_lock);//释放自旋锁

  seq_printf(m,”\n\n————-data information end—————————“);

  }

  static void * my_next(struct seq_file *m, void *v, loff_t *pos)

  {

  ++*pos;

  return (SEQ_START_TOKEN==v)?list_head:((struct node*)v)->next;

  }

  static int my_show(struct seq_file *m, void *v)

  {

  struct node* tmp_entry;

  static int line_count= 0;

  if(SEQ_START_TOKEN==v){

  seq_printf(m,”\n\n————-data informations start—————————\n\n”);

  }else{

  tmp_entry = (struct node*)v;

  seq_file(m,” %d “,tmp_entry->num);

  if(++line_count==10){//一行输出十个数据

  seq_printf(m,”\n”);

  line_count=0;

  }

  }

  return 0;

  }

  然后,定义一个struct seq_operations,如下:

  static const struct seq_operations my_seq_ops={

  .start = my_start,

  .next = my_next,

  .stop = my_stop,

  .show = my_show,

  };

  到这,我们还要定义一个函数把my_seq_ops的地址出给内核,如下,

  static int proc_my_test_open((struct inode *inode, struct file *file)

  {

  return seq_open(file,&my_seq_ops);

  }

  最后再定义一个struct file_operations结构体,我们的proc_my_test_open传给内核,并调用proc_create,创建一个proc目录就大功告成啦。代码如下:

  static const struct file_operations my_file_ops={

  .owner = THIS_MODULE,

  .open = proc_my_test_open,

  .read = seq_read,

  .llseek = seq_lseek,

  .release = seq_release,

  };

  my_file_opsproc_create(“my_test”, 0, NULL, &my_file_ops);

  其实对于简单的输出,可以简单定义一个show函数然后通过single_open而非我们这里的seq_open,这个方法比较简单,具体办法可以自行google。

[1][2]

可你仍然感谢天地和人世所带来的这些变化和发生。

linux 读取proc文件之seq

相关文章:

你感兴趣的文章:

标签云: