Linux内核实验报告 - 实验4(2)

2019-04-09 08:17

if (example_dir == 0){ printk (\ return -1; } data_file = (struct proc_dir_entry *)create_proc_entry(\ if (data_file == 0){ remove_proc_entry(\ printk(\ return -ENOMEM; } strcpy(mydata.value,\ data_file->data = &mydata; data_file->read_proc = &proc_read; data_file->write_proc=&proc_write; //data_file->owner=THIS_MODULE; 在我的内核中该结构已经没有owner成员了 return 0; }

//卸载模块

void cleanup_module(void) { remove_proc_entry(\ remove_proc_entry(\ printk(\}

MODULE_LICENSE(\

MODULE_DESCRIPTION(\MODULE_AUTHOR(\

问题A用户测试程序: #include #include #include #include #include #include #define path \#define prefix \#define prelen 6 #define ITERNUM 10 //默认循环次数 int main(int argc,char * argv[]) {

}

int n;

if (argc >1 ){ n = atoi(argv[1]); }else { n = ITERNUM; }

FILE * fp = 0; char buf[16]; struct timeval tv; for ( ; n > 0 ; --n){ fp = fopen(path,\ printf(\ fclose(fp); gettimeofday(&tv,NULL); printf(\}

问题B:

#include #include #include #include #include #include

#include

#include //task_struct等 /*#include */

#define MODULE_NAME \#define MYDATA_LEN 16 #define PMDIR \#define PMFILE \//放用户空间传入的数据 struct my_proc_data{ char value[MYDATA_LEN]; };

struct my_proc_data mydata; //proc结构变量

static struct proc_dir_entry *example_dir; static struct proc_dir_entry *data_file; static int param;

module_param(param,int,0644); //读文件myfile的读驱动函数

static int proc_read(char *page,char **start,off_t off,int count,int *eof,void *data)

{ int len = 0; struct task_struct *task = current; struct my_proc_data * mydatap = (struct my_proc_data*)data; len += sprintf(page + len,\ len += sprintf(page+len,\遍历父进程\\npid\\tppid\\tcomm\\n\ while (task != &init_task){ len += sprintf(page len,\ task = task->parent; } /* task now points to init*/ len += sprintf(page len,\ /*len += sprintf(page len,\ len += sprintf(page + len,\遍历任务队列\\ncomm\\tpid\\n\ for_each_process(task){ /* this poinlessly prints the name and pid of each task*/ /*printk(\ len += sprintf(page + len,\ } return len; }

//写文件myfile的写驱动函数

static int proc_write(struct file *file,const char* buffer,unsigned long count,void *data) { int len; struct my_proc_data *mydatap = (struct my_proc_data *) data; if (count > MYDATA_LEN) len = MYDATA_LEN; else len = count; if (copy_from_user(mydatap->value,buffer,len)){//buffer 在user mode return -EFAULT;//?Bad address,why minus? } mydatap->value[len-1]='\\0'; return len; }

//装入模块

int init_module(void) {

//创建proc/myfile目录

example_dir = (struct proc_dir_enry *)proc_mkdir(PMDIR,NULL);

+

+ +

if (example_dir == 0){ printk (\ return -1; }

data_file = (struct proc_dir_entry *)create_proc_entry(PMFILE,0666,example_dir); if (data_file == 0){ remove_proc_entry(\ printk(\ return -ENOMEM; }

strcpy(mydata.value,\data_file->data = &mydata;

data_file->read_proc = &proc_read; data_file->write_proc=&proc_write;

//data_file->owner=THIS_MODULE; 在我的内核中该结构已经没有owner成员了 return 0; }

//卸载模块

void cleanup_module(void) { remove_proc_entry(PMFILE,example_dir); remove_proc_entry(PMDIR,NULL); printk(\}

MODULE_LICENSE(\

MODULE_DESCRIPTION(\MODULE_AUTHOR(\

问题C: #include #include #include #include #include #include #include #include //task_struct等 #include #include #include #include #define LOAD_INT(x) ((x) >> FSHIFT)

#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100) #define MODULE_NAME \#define MYDATA_LEN 16 #define PMFILE \#define PMDIR \//放用户空间传入的数据 struct my_proc_data{ char value[MYDATA_LEN]; };

struct my_proc_data mydata;

//proc结构变量

static struct proc_dir_entry *example_dir; static struct proc_dir_entry *data_file; static int param;

module_param(param,int,0644);

//读文件myfile的读驱动函数

static int proc_read(char *page,char **start,off_t off,int count,int *eof,void *data) { int len = 0; struct my_proc_data * mydatap = (struct my_proc_data*)data; if (param == 0){ //获取loadavg len += sprintf(page + len, \ LOAD_INT(avenrun[0]), LOAD_FRAC(avenrun[0]), LOAD_INT(avenrun[1]), LOAD_FRAC(avenrun[1]), LOAD_INT(avenrun[2]), LOAD_FRAC(avenrun[2]) /*,nr_running(), nr_threads,*/ /*task_active_pid_ns(current)->last_pid*/); }else if (param == 1){ len += sprintf(page + len,\ len += sprintf(page + len,\ffy/(5000/HZ)0); } return len; }

//写文件myfile的写驱动函数

static int proc_write(struct file *file,const char* buffer,unsigned long count,void *data) { int len; char buf[16]; if (count > MYDATA_LEN) len = MYDATA_LEN; else

len = count; copy_from_user(buf,buffer,len); loops_per_jiffy = simple_strtol(buf,NULL,10); return len; }

//装入模块

int init_module(void) { //创建proc/myfile目录 example_dir = (struct proc_dir_enry *)proc_mkdir(PMDIR,NULL); if (example_dir == 0){ printk (\ return -1; } data_file = (struct proc_dir_entry *)create_proc_entry(PMFILE,0666,example_dir); if (data_file == 0){ remove_proc_entry(PMFILE,0); printk(\ return -ENOMEM; } strcpy(mydata.value,\ data_file->data = &mydata; data_file->read_proc = &proc_read; data_file->write_proc=&proc_write; //data_file->owner=THIS_MODULE; 在我的内核中该结构已经没有owner成员了 return 0; }

//卸载模块

void cleanup_module(void) { remove_proc_entry(PMFILE,example_dir); remove_proc_entry(PMDIR,NULL); printk(\}

MODULE_LICENSE(\

MODULE_DESCRIPTION(\MODULE_AUTHOR(\

参考材料 lke2012.pdf


Linux内核实验报告 - 实验4(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:基础会计自编教材习题集及答案(1-6章)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: