列出所有进程
1 #include2 #include 3 #include 4 #include 5 #include 6 7 static __init int print_pid(void) 8 { 9 struct task_struct *task,*p;10 struct list_head *pos;11 int count=0;12 printk("Hello,let begin\n");13 task = &init_task;14 list_for_each(pos,&task->tasks)15 {16 p = list_entry(pos, struct task_struct, tasks);17 count++;18 printk("%d---->%s-->%X\n",p->pid,p->comm, p->state);19 }20 printk("the number of process is:%d\n",count);21 return 0;22 }23 24 static __exit void print_exit(void)25 {26 printk("<0>end!\n");27 }28 module_init(print_pid);29 module_exit(print_exit);
获得虚拟地址的物理内存
1 /* 2 3 *伪代码,示例 4 5 *32位地址,三级映射(没有pud_t),页面大小4KB 6 7 */ 8 9 unsigned long addr = 0x12345678;//要找的虚拟地址,用户空间所访问的地址10 11 unsigned long real_addr = 0x00;//要输出的地址12 13 struct task_struct *cur_task = get_current();//获取当前进程控制块14 15 struct mm_struct *mm = cur_task -> mm;//进程虚拟空间16 17 pgd_t *pgd;//描述页全局目录项18 19 pmd_t *pmd;//描述页中间项20 21 pte_t *pte;//页表项22 23 24 25 pgd = pgd_offset(mm, addr);//找出所在目录26 27 if (pgd_none(*pgd)){28 29 goto out;30 31 }32 33 pmd = pmd_offset(pgd, addr);//找出所在中间项34 35 36 37 if (pmd_none(*pmd)){38 39 goto out;40 41 }42 43 pte = pte_offset(pmd, addr);//找出所在页面44 45 46 47 48 49 if (pte_none(*pte)) {50 51 goto out;52 53 }54 55 56 57 //假设每页4KB58 59 real_addr = addr & 0x00003fff; //取出页面偏移量60 61 real_addr += pte;//内核空间访问的地址62 63 real_addr -= PAGE_OFFSET;//真正物理地址()64 65 printk("物理地址是 %x\n",real_addr);66 67 return;68 69 70 71 out:72 73 printk("没有内存映射",real_addr);