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 
....
复制代码


本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2013/03/04/2943517.html,如需转载请自行联系原作者

相关文章
|
1月前
|
Ubuntu Linux
计算机基础知识:linux系统怎么安装?
在虚拟机软件中创建一个新的虚拟机,并选择相应操作系统类型和硬盘空间大小等参数。将下载的 ISO 镜像文件加载到虚拟机中。启动虚拟机,进入安装界面,并按照步骤进行安装。安装完成后,可以在虚拟机中使用 Linux 系统。
|
1月前
|
存储 Ubuntu Linux
「正点原子Linux连载」第二章Ubuntu系统入门
在图2.8.2.4中,我们使用命令umount卸载了U盘,卸载以后当我们再去访问文件夹/mnt/tmp的时候发现里面没有任何文件了,说明我们卸载成功了。
|
1月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
552 10
|
1月前
|
安全 Linux 数据安全/隐私保护
为Linux系统的普通账户授予sudo访问权限的过程
完成上述步骤后,你提升的用户就能够使用 `sudo`命令来执行管理员级别的操作,而无需切换到root用户。这是一种更加安全和便捷的权限管理方式,因为它能够留下完整的权限使用记录,并以最小权限的方式工作。需要注意的是,随意授予sudo权限可能会使系统暴露在风险之中,尤其是在用户不了解其所执行命令可能带来的后果的情况下。所以在配置sudo权限时,必须谨慎行事。
265 0
|
1月前
|
Ubuntu Linux 开发者
国产 Linux 发行版再添新成员,CutefishOS 系统简单体验
当然,系统生态构建过程并不简单,不过为了帮助国产操作系统优化生态圈,部分企业也开始用国产操作系统替代 Windows,我们相信肯定会有越来越多的精品软件登录 Linux 平台。
91 0
|
1月前
|
Ubuntu 安全 Linux
Linux系统入门指南:从零开始学习Linux
Shell脚本是一种强大的自动化工具,可以帮助您简化重复的任务或创建复杂的脚本程序。了解Shell脚本的基本语法和常用命令,以及编写和运行Shell脚本的步骤,将使您更高效地处理日常任务。
151 0
|
1月前
|
Ubuntu Linux 图形学
Linux学习之Linux桌面系统有哪些?
Cinnamon:与MATE类似,Cinnamon 拥有 GNOME 和 Unity 等其它桌面环境所没有的种种功能,是高度可定制的桌面环境,不需要任何外部插件、窗口组件和调整工具来定制桌面。
99 0
|
1月前
|
Ubuntu 安全 Linux
十款常用Linux系统介绍
本文不是什么大盘点。市面上有好几百款发行版,每款发行版在某个方面都与众不同。不可能在此全部罗列,本文只罗列了十款最常见的Linux发行版(世界上只有两种人,一种是懂二进制的,另一种是不懂二进制的)。请宣传Linux的魅力或威力。
|
1月前
|
Ubuntu 安全 Linux
linux系统|Ubuntu 18.10 如期正式发布,新面孔新技术都来了
微软公司也终于沉不住气要在linux开源系统开疆扩土了。mscode这样的工具的确好用,虽然差第一名那么一点儿,但是最老版的公司出版的软件的确很是让人动心!
|
1月前
|
Ubuntu Linux 数据安全/隐私保护
Win10安装Linux子系统教程!如何在Win10系统中安装Ubuntu!
登录系统后,输入cd /返回上一级,然后再输入“ls”查看一下系统文件目录,看看对不对!