Linux内核实践之序列文件

发表于:2012-4-01 10:45

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:bullbat(CSDNblog)    来源:51Testing软件测试网采编

分享:

  程序文件(list_seq.c):

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

#define N 10

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mike Feng");

/*对内核链表操作需要加锁*/
static struct mutex lock;

static struct list_head head;

struct my_data
{
 struct list_head list;
 int value;
};

/*链表的插入元素*/
struct list_head* insert_list(struct list_head *head,int value)
{
 struct my_data *md=NULL;
 mutex_lock(&lock);
 md=(struct my_data*)kmalloc(sizeof(struct my_data),GFP_KERNEL);
 if(md)
 {
  md->value=value;
  list_add(&md->list,head);
  
 }
 mutex_unlock(&lock);

 return head;
}
/*打印,传入参数v为open函数返回的,链表需要操作的节点*/
static int list_seq_show(struct seq_file *file,void *v)
{
 struct list_head *list=(struct list_head*)v;

 struct my_data *md=list_entry(list,struct my_data,list);

 seq_printf(file,"The value of my data is:%d\n",md->value);

 return 0;
}
static void *list_seq_start(struct seq_file *file,loff_t *pos)
{
 /*加锁*/
 mutex_lock(&lock);
 return seq_list_start(&head,*pos);
}

static void *list_seq_next(struct seq_file *file,void *v,loff_t *pos)
{
 return seq_list_next(v,&head,pos);
}
static void list_seq_stop(struct seq_file *file,void *v)
{
 /*解锁*/
 mutex_unlock(&lock);
}
static struct seq_operations list_seq_ops=
{
 .start =list_seq_start,
 .next =list_seq_next,
 .stop =list_seq_stop,
 .show =list_seq_show,
};

static int list_seq_open(struct inode *inode,struct file *file)
{
 return seq_open(file,&list_seq_ops);
}

static struct file_operations my_file_ops=
{
 .open =list_seq_open,
 .read =seq_read,
 .write =seq_write,
 .llseek =seq_lseek,
 .release=seq_release,
 .owner =THIS_MODULE,
};

static __init int list_seq_init(void)
{
 struct proc_dir_entry *entry;
 int i;
 mutex_init(&lock);
 INIT_LIST_HEAD(&head);

 for(i=0;i<N;i++)
  head=*(insert_list(&head,i));

 entry=create_proc_entry("list_seq",0,NULL);
 if(entry)
  entry->proc_fops=&my_file_ops;
 return 0;
}

static void list_seq_exit(void)
{
 struct my_data *md=NULL;
 remove_proc_entry("list_seq",NULL);

 while(!list_empty(&head))
 {
  md=list_entry((&head)->next,struct my_data,list);
  list_del(&md->list);
  kfree(md);
 }
 
}

module_init(list_seq_init);
module_exit(list_seq_exit);

  测试试验结果:

  由于内核函数list_add为前插,所以打出的数据为倒序的。

  序列文件的实现基于proc文件系统,下一步将对其进行分析学习。

44/4<1234
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号