操作系统进程遍历(进入linux内核实现)

版权声明: https://blog.csdn.net/qq_39463175/article/details/83987469

实验要求概述:

Part I—Iterating over Tasks Linearly

As illustrated in Section 3.1, the PCB in Linux is represented by the

structure task_struct, which is found in the <linux/sched.h> include file.

In Linux, the for_each_process() macro easily allows iteration over all

current tasks in the system:

Part II—Iterating over Tasks with a Depth-First Search Tree

The second portion of this project involves iterating over all tasks in the

system using a depth-first search (DFS) tree. (As an example: the DFS

iteration of the processes in Figure 3.8 is 1, 8415, 8416, 9298, 9204, 2, 6,

扫描二维码关注公众号,回复: 4037337 查看本文章

200, 3028, 3610, 4005.)

实验过程:

  1. 1. Open the virtual machine and create a new file.
  2. 2. Write the part 1 code as required, enter the terminal, first use the CD command to the shiyuan3.2 directory, then use the make command, then load the kernel with sudo insmod simple.ko, then use dmesg to view the process information, and finally use sudo Rmmod simple.ko Uninstall the module, you can also use dmesg to view the exit information.
  3. 3. Write the part 2 code as required, (DFS implementation is very clever, I also think about it for a long time, here is recursive implementation) into the terminal, first use the CD command to the shiyuan3.2 directory, then use the make command, then Then use sudo insmod simple.ko to load the kernel, then use dmesg to view the process information, and finally use sudo rmmod simple.ko to uninstall the module, or use dmesg to view the exit information.
  4. End the experiment, take a screenshot, then shut down the virtual machine.
Part 1 code: 

#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/init.h>	
#include <linux/list.h> 
#include <linux/sched.h>
 
 
//初始化函数  print_pid()
static int __init print_pid(void){
	struct task_struct *task, *p;
	struct list_head *pos;
	int count = 0;
 
	printk("内核模块开始运行\n");
 
	task = &init_task;
 
	list_for_each(pos, &task->tasks){
		p = list_entry(pos, struct task_struct, tasks);
		count++;
		printk("state%ld",p->state);//打印进程状态
                              printk("--->%d",p->pid);//打印进程的ID号
                           printk("--->%s\n",p->comm);//打印进程的名字
	}
 
	printk("总的进程数为:%d\n", count);
	
	return 0;
}
 
//退出和清理函数  
static void __exit lkp_cleanup(void){
 
	printk("杨远林拜拜了, modul被卸载了\n");
}
 
module_init(print_pid);		
module_exit(lkp_cleanup);	
MODULE_LICENSE("GPL");		

 

 

Part 2 code: 


 #include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>

//深度优先算法实现遍历
void DFS(struct task_struct *p)
{   
    struct task_struct *child;
    struct list_head *list;

  printk("state%ld",p->state);//打印进程状态
   printk("--->%d",p->pid);//打印进程的ID号
    printk("--->%s\n",p->comm);//打印进程的名字
    list_for_each(list, &p->children) {//开始迭代
        child = list_entry(list, struct task_struct, sibling);
        DFS(child);
    }
}


//初始化函数  print_pid()
static int __init print_pid(void){
  printk("内核模块开始运行\n");
    DFS(&init_task);

    return 0;
}


//退出和清理函数  
static void __exit lkp_cleanup(void){
 
	printk("杨远林拜拜了, modul被卸载了\n");
}
 
module_init(print_pid);		
module_exit(lkp_cleanup);	
MODULE_LICENSE("GPL");		

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/qq_39463175/article/details/83987469
今日推荐