欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文學(xué)會(huì)使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息

 更新時(shí)間:2023年05月14日 11:05:01   作者:chnmagnus  
這篇文章主要介紹了使用Linux內(nèi)核模塊&proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息詳解,

實(shí)例要求

編寫(xiě)一個(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í)還需要編寫(xiě)一個(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 License

archlinux 下的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)核模塊&amp;proc實(shí)例統(tǒng)計(jì)所有進(jìn)程信息的詳細(xì)內(nèi)容,更多關(guān)于Linux內(nèi)核模塊&amp;proc的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 深入理解Linux中的grep命令

    深入理解Linux中的grep命令

    大家都知道grep是一種強(qiáng)大的文本搜索工具,它能使用正則表達(dá)式搜索文本,并把匹配的行打印出來(lái)。這篇文章給大家詳細(xì)的介紹了Linux中的grep命令,相信對(duì)大家的學(xué)習(xí)和理解很有幫助,有需要的朋友們可以參考借鑒,感興趣下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • 獲取同一網(wǎng)段下所有機(jī)器MAC地址的shell腳本

    獲取同一網(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腳本分享

    這篇文章主要介紹了Ubuntu服務(wù)器配置apache2.4的限速功能shell腳本分享,本文直接給出腳本實(shí)現(xiàn)代碼,代碼相關(guān)原理都寫(xiě)在了注釋里,需要的朋友可以參考下
    2015-03-03
  • Shell腳本遍歷一個(gè)日期范圍實(shí)例

    Shell腳本遍歷一個(gè)日期范圍實(shí)例

    這篇文章主要介紹了Shell腳本遍歷一個(gè)日期范圍實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-03-03
  • 最快捷登陸ssh 服務(wù)器的方法

    最快捷登陸ssh 服務(wù)器的方法

    這篇文章主要介紹了如果利用shell 腳本登陸,實(shí)現(xiàn)最快速登陸,需要的朋友可以參考下
    2013-11-11
  • 用來(lái)查看linux內(nèi)存使用情況的shell腳本

    用來(lái)查看linux內(nèi)存使用情況的shell腳本

    本文為大家提供的這個(gè)腳本,通過(guò)查看status文件中VmRSS的字段來(lái)查看系統(tǒng)或進(jìn)程使用的內(nèi)存情況,有需要的朋友不妨參考下
    2013-02-02
  • shell進(jìn)階awk命令編排字段使用詳解

    shell進(jìn)階awk命令編排字段使用詳解

    這篇文章主要為大家介紹了shell進(jìn)階awk命令編排字段使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Linux下常用壓縮格式的壓縮與解壓方法詳解

    Linux下常用壓縮格式的壓縮與解壓方法詳解

    這篇文章主要介紹了Linux下常用壓縮格式的壓縮與解壓方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Linux logrotate日志切割安裝配置說(shuō)明

    Linux logrotate日志切割安裝配置說(shuō)明

    這篇文章主要為大家介紹了Linux logrotate日志切割的安裝配置說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • shell對(duì)比文件內(nèi)容腳本分享

    shell對(duì)比文件內(nèi)容腳本分享

    這篇文章主要介紹了shell 對(duì)比文件內(nèi)容腳本,找出兩個(gè)文件不同的數(shù)據(jù),需要的朋友可以參考下
    2014-03-03

最新評(píng)論