Linux Kernel Development——列出系统中所有的进程
生活随笔
收集整理的這篇文章主要介紹了
Linux Kernel Development——列出系统中所有的进程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 在內(nèi)核模塊中列出所有的進程:
從init_task開始遍歷內(nèi)核鏈表,輸出所有進程
#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 elseKDIR := /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default:$(MAKE) -C $(KDIR) M=$(PWD) modules endif2. 使用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 ....
本文轉(zhuǎn)自feisky博客園博客,原文鏈接:http://www.cnblogs.com/feisky/archive/2013/03/04/2943517.html,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的Linux Kernel Development——列出系统中所有的进程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微服务改造中解决跨库问题的思路
- 下一篇: Linux基本命令之文件查找、检索