2021-10-18
生活随笔
收集整理的這篇文章主要介紹了
2021-10-18
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
linux實驗二proc
Makefile:
ifneq ($(KERNELRELEASE),) obj-m := tasklist.o else KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers endiftasklist.c:
//------------------------------------------------------------------- // tasklist.c: 本內(nèi)核文件創(chuàng)建一個proc偽文件,'/proc/tasklist' // 通過如下命令可以顯示系統(tǒng)中所有進程的部分信息 // 注意:Mkefile文件必須正確放置在當(dāng)前目錄下。 // 編譯命令: make // 內(nèi)核模塊添加:$sudo insmod tasklist.ko // 添加內(nèi)核模塊后讀取并信息tasklist內(nèi)核信息: $ cat /proc/tasklist // 內(nèi)核模塊刪除:$sudo rmmod tasklist // NOTE: Written and tested with Linux kernel version 3.13.0 and.3.2.0 // 用戶也可以自己完成相關(guān)的讀寫程序,就像課后練習(xí)1要求的。我們尤其鼓勵同學(xué)自己寫程序代替cat程序。 // strace函數(shù)可用于追蹤系統(tǒng)調(diào)用,命令格式如下所示: // $ strace cat /proc/tasklist //-------------------------------------------------------------------#include <linux/module.h> // for init_module() #include <linux/proc_fs.h> // for create_proc_info_entry() #include <linux/sched.h> // for init_task #include <linux/seq_file.h> // for sequence files #include <linux/slab.h> // for kzalloc, kfree char modname[] = "tasklist"; struct task_struct *task; int taskcounts=0; // 'global' so value will be retainedstatic void * my_seq_start(struct seq_file *m, loff_t *pos) {///printk(KERN_INFO"Invoke start\n"); //可以輸出調(diào)試信息if ( *pos == 0 ) // 表示遍歷開始{task = &init_task; //遍歷開始的記錄地址return &task; //返回一個非零值表示開始遍歷}else //遍歷過程中{ if (task == &init_task ) //重新回到初始地址,退出return NULL;return (void*)pos ;//否則返回一個非零值} } static int my_seq_show(struct seq_file *m, void *v) {//獲取進程的相關(guān)信息//printk(KERN_INFO"Invoke show\n"); seq_printf( m, "#%-3d ", taskcounts++ ); seq_printf( m, "%5d ", task->pid );seq_printf( m, "%lu ", task->state );seq_printf( m, "%-15s ", task->comm );seq_puts( m, "\n" ); return 0; }static void * my_seq_next(struct seq_file *m, void *v, loff_t *pos) {//printk(KERN_INFO"Invoke next\n"); (*pos)++; task= next_task(task); //指向下一個進程return NULL;} static void my_seq_stop(struct seq_file *m, void *v) {//printk(KERN_INFO"Invoke stop\n"); // do nothing }static struct seq_operations my_seq_fops = {//序列文件記錄操作函數(shù)集合.start = my_seq_start,.next = my_seq_next,.stop = my_seq_stop,.show = my_seq_show };static int my_open(struct inode *inode, struct file *file) { return seq_open(file, &my_seq_fops); //打開序列文件并關(guān)聯(lián)my_seq_fops } static const struct file_operations my_proc = { //proc文件操作函數(shù)集合.owner = THIS_MODULE, .open = my_open,.read = seq_read, .llseek = seq_lseek,.release = seq_release }; int __init my_init( void ) {struct proc_dir_entry* my_proc_entry;printk( "<1>\nInstalling \'%s\' module\n", modname );my_proc_entry = proc_create(modname, 0x644, NULL, &my_proc);//生成proc文件if (NULL == my_proc_entry){return -ENOMEM;}return 0; //SUCCESS }void __exit my_exit( void ) {remove_proc_entry( modname, NULL );//刪除proc文件printk( "<1>Removing \'%s\' module\n", modname ); }module_init(my_init); module_exit(my_exit); MODULE_LICENSE("GPL");tasklist.c.bak:
//------------------------------------------------------------------- // tasklist.c: 本內(nèi)核文件創(chuàng)建一個proc偽文件,'/proc/tasklist' // 通過如下命令可以顯示系統(tǒng)中所有進程的部分信息 // 注意:Mkefile文件必須正確放置在當(dāng)前目錄下。 // 編譯命令: make // 內(nèi)核模塊添加:$sudo insmod tasklist.ko // 添加內(nèi)核模塊后讀取并信息tasklist內(nèi)核信息: $ cat /proc/tasklist // 內(nèi)核模塊刪除:$sudo rmmod tasklist // NOTE: Written and tested with Linux kernel version 3.13.0 and.3.2.0 // 用戶也可以自己完成相關(guān)的讀寫程序,就像課后練習(xí)1要求的。我們尤其鼓勵同學(xué)自己寫程序代替cat程序。 // strace函數(shù)可用于追蹤系統(tǒng)調(diào)用 // $ strace cat /proc/tasklist //-------------------------------------------------------------------#include <linux/module.h> // for init_module() #include <linux/proc_fs.h> // for create_proc_info_entry() #include <linux/sched.h> // for init_task #include <linux/seq_file.h> // for sequence files #include <linux/slab.h> // for kzalloc, kfree char modname[] = "tasklist"; struct task_struct *task; int taskcounts=0; // 'global' so value will be retainedstatic void * my_seq_start(struct seq_file *m, loff_t *pos) {///printk(KERN_INFO"Invoke start\n"); //可以輸出調(diào)試信息if ( *pos == 0 ) // 表示遍歷開始{task = &init_task; //遍歷開始的記錄地址return &task; //返回一個非零值表示開始遍歷}else //遍歷過程中{ if (task == &init_task ) //重新回到初始地址,退出return NULL;return (void*)pos ;//否則返回一個非零值} } static int my_seq_show(struct seq_file *m, void *v) {//獲取進程的相關(guān)信息//printk(KERN_INFO"Invoke show\n"); seq_printf( m, "#%-3d ", taskcounts++ ); seq_printf( m, "%5d ", task->pid );seq_printf( m, "%lu ", task->state );seq_printf( m, "%-15s ", task->comm );seq_puts( m, "\n" ); return 0; }static void * my_seq_next(struct seq_file *m, void *v, loff_t *pos) {//printk(KERN_INFO"Invoke next\n"); (*pos)++; task= next_task(task); //指向下一個進程return NULL;} static void my_seq_stop(struct seq_file *m, void *v) {//printk(KERN_INFO"Invoke stop\n"); // do nothing }static struct seq_operations my_seq_fops = {//序列文件記錄操作函數(shù)集合.start = my_seq_start,.next = my_seq_next,.stop = my_seq_stop,.show = my_seq_show };static int my_open(struct inode *inode, struct file *file) { return seq_open(file, &my_seq_fops); //打開序列文件并關(guān)聯(lián)my_seq_fops } static const struct file_operations my_proc = { //proc文件操作函數(shù)集合.owner = THIS_MODULE, .open = my_open,.read = seq_read, .llseek = seq_lseek,.release = seq_release }; int __init my_init( void ) {struct proc_dir_entry* my_proc_entry;printk( "<1>\nInstalling \'%s\' module\n", modname );my_proc_entry = proc_create(modname, 0x644, NULL, &my_proc);//生成proc文件if (NULL == my_proc_entry){return -ENOMEM;}return 0; //SUCCESS }void __exit my_exit( void ) {remove_proc_entry( modname, NULL );//刪除proc文件printk( "<1>Removing \'%s\' module\n", modname ); }module_init(my_init); module_exit(my_exit); MODULE_LICENSE("GPL");總結(jié)
以上是生活随笔為你收集整理的2021-10-18的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Thinking in java第一章对
- 下一篇: 我们“老实”么?