Linux Kernel Development——列出系統中所有的進程

來源:互聯網
上載者:User

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.oelse        KDIR := /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:    $(MAKE) -C $(KDIR) M=$(PWD) modulesendif

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》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.