• Linux Kernel Development——列出系统中所有的进程


    1. 在内核模块中列出所有的进程:

    从init_task开始遍历内核链表,输出所有进程

    #include <linux/module.h>
    #include <linux/list.h>
    #include <linux/init.h>
    #include <linux/sched.h>
    
    MODULE_LICENSE("Dual BSD/GPL");
    
    static int test_init(void)
    {
            struct task_struct *task, *p;
            struct list_head *pos;
            int count=0;
    
            printk(KERN_ALERT "test module init\n");
    
            task=&init_task;
            list_for_each(pos, &task->tasks)
            {
                    p=list_entry(pos, struct task_struct, tasks);
                    count++;
                    printk(KERN_ALERT "%s[%d]\n", p->comm, p->pid);
            }
            printk(KERN_ALERT "Total %d tasks\n", count);
    
            return 0;
    }
    
    static void test_exit(void)
    {
            printk(KERN_ALERT "test module exit!\n");
    }
    
    module_init(test_init);
    module_exit(test_exit);

    Makefile

    ifneq ($(KERNELRELEASE),)
            obj-m := test.o
    else
            KDIR := /lib/modules/$(shell uname -r)/build
    
    PWD := $(shell pwd)
    
    default:
        $(MAKE) -C $(KDIR) M=$(PWD) modules
    endif

    2. 使用Systemtap输出所有进程:

    //process_list.stp
    
    %{  
    #include <linux/list.h>
    #include <linux/sched.h>
    %}
    
    function process_list ()
    %{  
        struct task_struct *p;
        struct list_head *_p,*_n;
    
        for_each_process(p){
            _stp_printf("%-15s (%-5d)\n",p->comm,p->pid);
        }
    %}
    
    probe begin
    {   
        process_list();
        exit()
    }

    运行方法

    # stap -g process_list.stp 
    init            (1    )
    kthreadd        (2    )
    migration/0     (3    )
    ksoftirqd/0     (4    )
    migration/0     (5    )
    watchdog/0      (6    )
    migration/1     (7    )
    migration/1     (8    )
    ksoftirqd/1     (9    )
    watchdog/1      (10   )
    events/0        (11   )
    events/1        (12   )
    cpuset          (13   )
    khelper         (14   )
    netns           (15   )
    ....

    3. 使用Systemtap打印进程uts命名空间信息

     //namespace_uts.stp
     %{
         #include<linux/list.h>
    
         #include<linux/sched.h>
    
         #include <linux/nsproxy.h>
    
         #include<linux/utsname.h>
     %}
    
    function process_list ()
     %{
        struct task_struct *p;
        struct list_head *_p,*_n;
        struct uts_namespace *uts;
        struct new_utsname *utsname;
    
        for_each_process(p){
            uts=p->nsproxy->uts_ns;
            utsname=&(uts->name);
            _stp_printf("%-15s(%-5d) %-24s %-16s\n", p->comm,p->pid,utsname->release, utsname->sysname);
         }
     %}
    
    probe begin
     {
        process_list();
        exit()
     }
    # stap -g namespace_uts.stp 
    init           (1    ) 2.6.32-220.el6.x86_64    Linux           
    kthreadd       (2    ) 2.6.32-220.el6.x86_64    Linux           
    migration/0    (3    ) 2.6.32-220.el6.x86_64    Linux           
    ksoftirqd/0    (4    ) 2.6.32-220.el6.x86_64    Linux           
    migration/0    (5    ) 2.6.32-220.el6.x86_64    Linux           
    watchdog/0     (6    ) 2.6.32-220.el6.x86_64    Linux           
    migration/1    (7    ) 2.6.32-220.el6.x86_64    Linux           
    migration/1    (8    ) 2.6.32-220.el6.x86_64    Linux 
    ....


    本文参考:

    http://blog.csdn.net/lzuzhp06/article/details/6933525

    《Linux Kernel Development》

  • 相关阅读:
    java.util.date java.sql.date java.sql.timestamp
    javadoc生成文档时,编码 GBK 的不可映射字符
    java关于ServletConfig FilterConfig什么用
    在项目中用run as java aplication调试类
    replace和replaceAll
    在easyui中的datagrid中使用行内编辑时textarea的换行保存到mysql数据库为\n
    [转] 设计模式另类版
    [转] 不错的俄罗斯方块程序代码(VC++版)
    [转] 30道模拟经典题(JDK1.4)(附解答)
    [转] C#排序算法
  • 原文地址:https://www.cnblogs.com/feisky/p/2943517.html
Copyright © 2020-2023  润新知