1. 程式人生 > >作業系統程序遍歷(進入linux核心實現)

作業系統程序遍歷(進入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("楊遠林拜拜了, 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");