一文學(xué)會(huì)使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息
實(shí)例要求
編寫一個(gè)Linux的內(nèi)核模塊,其功能是遍歷操作系統(tǒng)所有進(jìn)程。該內(nèi)核模塊輸出系統(tǒng)中:每個(gè)進(jìn)程的名字、進(jìn)程pid、進(jìn)程的狀態(tài)、父進(jìn)程的名字;以及統(tǒng)計(jì)系統(tǒng)中進(jìn)程個(gè)數(shù),包括統(tǒng)計(jì)系統(tǒng)中TASK_RUNNING、TASK_INTERRUPTIBLE、TASK_UNINTERRUPTIBLE、TASK_ZOMBIE、TASK_STOPPED等(還有其他狀態(tài))狀態(tài)進(jìn)程的個(gè)數(shù)。同時(shí)還需要編寫一個(gè)用戶態(tài)下執(zhí)行的程序,顯示內(nèi)核模塊輸出的內(nèi)容。
學(xué)習(xí)資料:
《邊干邊學(xué)—Linux內(nèi)核指導(dǎo)》 ArchLinux Wiki
解決方案
#include <linux/module.h> //needed by all modules
#include <linux/kernel.h> //needed for KERN_INFO
#include <linux/sched.h> //defined struct task_struct
#include <linux/proc_fs.h> //create proc file
#include <linux/seq_file.h> //use seq_file
#define FILENAME "osexp" //defile proc file name
int myshow(struct seq_file *,void *);
int myopen(struct inode *,struct file *);
/* custom the operations to the seq_file */
static const struct file_operations myops = {
.owner = THIS_MODULE,
.open = myopen,
.read = seq_read,
.release = single_release
};
/* do when open the seq file */
int myopen(struct inode *inode,struct file *file){
single_open(file,myshow,NULL);//bind seq_file with myshow function
return 0;
}
/* create proc file */
int init_proc(void){
struct proc_dir_entry * myfile;
myfile = proc_create(FILENAME,0444,NULL,&myops);//create proc file
if(myfile == NULL) //deal with error
return -ENOMEM;
return 0;
}
/* remove proc file */
void remove_proc(void){
remove_proc_entry(FILENAME,NULL);//remove proc file
printk(KERN_INFO "[m] proc file:%s removed\n",FILENAME);//print debug message
}
/*description: output process's info to log */
int myshow(struct seq_file *file,void *v){
int num_running = 0; //the number of process whose status is running
int num_interruptible = 0; //the number of process whose status is interruptible
int num_uninterruptible = 0; //the ... status is uninterruptible
int num_zombie = 0; //the process exited with status zombie
int num_stopped = 0; //the ... status is stopped
int num_traced = 0; //the ... status is traced
int num_dead = 0; //the process has deaded;
int num_unknown = 0; //the process whose status is unknown
int num_total = 0; //the total number of process
int t_exit_state; //temp var to store task_struct.exit_state
int t_state; //temp var to store task_struct.state
struct task_struct *p; //pointer to task_struct
//printk("[m] All processes' info:\n");//print boot info
seq_printf(file,"[m] All processes' info:\n");
for(p=&init_task;(p=next_task(p))!=&init_task;){ //go througn the linklist
//printk(KERN_INFO "[m] Name:%s Pid:%d State:%ld ParName:%s\n",p->comm,p->pid,p->state,p->real_parent->comm); //print the process's info to log
seq_printf(file,"[m] Name:%s Pid:%d State:%ld ParName:%s\n",p->comm,p->pid,p->state,p->real_parent->comm);
num_total++; //total number of process plus one
t_state = p->state; //put p->state to variable t_state
t_exit_state = p->exit_state;//similar to above
if(t_exit_state!=0){ //if the process has exited
switch(t_exit_state){
case EXIT_ZOMBIE://if the exit state is zombie
num_zombie++;//variable plus one
break; //break switch
case EXIT_DEAD://if the exit state is dead
num_dead++;//variable plus one
break;//break switch
default: //other case
break;//break switch
}
}else{ // if the proess hasn't exited
switch(t_state){
case TASK_RUNNING://if the state is running
num_running++;//variable plus one
break;//break switch
case TASK_INTERRUPTIBLE://state is interruptible
num_interruptible++;//variable plus one
break;//break switch
case TASK_UNINTERRUPTIBLE://state is uninterruptible
num_uninterruptible++;//var + 1
break;//break switch
case TASK_STOPPED://state is stopped
num_stopped++;//var +1
break;//break switch
case TASK_TRACED://state is traced
num_traced++;//var +1
break;//break switch
default://other case
num_unknown++;
break;
}
}
}
//below instruction is to print the statistics result in above code
// printk(KERN_INFO "[m] total tasks: %10d\n",num_total);
// printk(KERN_INFO "[m] TASK_RUNNING: %10d\n",num_running);
// printk(KERN_INFO "[m] TASK_INTERRUPTIBLE: %10d\n",num_interruptible);
// printk(KERN_INFO "[m] TASK_UNINTERRUPTIBLE: %10d\n",num_uninterruptible);
// printk(KERN_INFO "[m] TASK_TRACED: %10d\n",num_stopped);
// printk(KERN_INFO "[m] TASK_TRACED: %10d\n",num_stopped);
// printk(KERN_INFO "[m] EXIT_ZOMBIE: %10d\n",num_zombie);
// printk(KERN_INFO "[m] EXIT_DEAD: %10d\n",num_dead);
// printk(KERN_INFO "[m] UNKNOWN: %10d\n",num_unknown);
seq_printf(file,"[m] total tasks: %10d\n",num_total);
seq_printf(file,"[m] TASK_RUNNING: %10d\n",num_running);
seq_printf(file,"[m] TASK_INTERRUPTIBLE: %10d\n",num_interruptible);
seq_printf(file,"[m] TASK_UNINTERRUPTIBLE: %10d\n",num_uninterruptible);
seq_printf(file,"[m] TASK_TRACED: %10d\n",num_stopped);
seq_printf(file,"[m] TASK_TRACED: %10d\n",num_stopped);
seq_printf(file,"[m] EXIT_ZOMBIE: %10d\n",num_zombie);
seq_printf(file,"[m] EXIT_DEAD: %10d\n",num_dead);
seq_printf(file,"[m] UNKNOWN: %10d\n",num_unknown);
return 0;
}
int init_module(void){// init the kernel module
printk(KERN_INFO "[m] exp_process started\n");//print start message
return init_proc();//create proc file
}
void cleanup_module(void){ //clean up the resources when module finished
remove_proc();//remove proc file
printk(KERN_INFO "[m] exp_process finished\n");//print finish message
}
MODULE_LICENSE("GPL");//show that this code follow GUN General Public Licensearchlinux 下的makefile
TARGET = os_1_2
KDIR = /lib/modules/$(shell uname -r)/build
PWD = $(shell pwd)
obj-m += $(TARGET).o
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean以上就是一文學(xué)會(huì)使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息的詳細(xì)內(nèi)容,更多關(guān)于Linux內(nèi)核模塊&proc的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
獲取同一網(wǎng)段下所有機(jī)器MAC地址的shell腳本
有時(shí)候需要在當(dāng)前同一網(wǎng)段下所有機(jī)器MAC地址,需要的朋友可以參考下2013-01-01
Ubuntu服務(wù)器配置apache2.4的限速功能shell腳本分享
這篇文章主要介紹了Ubuntu服務(wù)器配置apache2.4的限速功能shell腳本分享,本文直接給出腳本實(shí)現(xiàn)代碼,代碼相關(guān)原理都寫在了注釋里,需要的朋友可以參考下2015-03-03

