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

实验要求概述:

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,

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("xxx拜拜了, 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("xxx拜拜了, modul被卸载了\n");
}
 
module_init(print_pid);		
module_exit(lkp_cleanup);	
MODULE_LICENSE("GPL");		

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值