内核current指针

通过current指针获取当前进程信息

current其实一个struct task_struct指针,指向当前进程
struct task_struct *task又是struct thread_info的一个成员变量。
thread_info可以从sp寄存器得到

//linux-imx/include/asm-generic/current.h
#define get_current() (current_thread_info()->task)
#define current get_current()

//linux-imx/arch/arm/include/asm/thread_info.h
register unsigned long current_stack_pointer asm ("sp");
static inline struct thread_info *current_thread_info(void)
{
        return (struct thread_info *)(current_stack_pointer & ~(THREAD_SIZE - 1));
}

使用方法
current 指针指向当前在运行的进程,内核代码可以通过使用 current 来使用进程特定的信息。指向 task_struct 的current指针在内核堆栈内,是一个全局项。
printk("current process comm:"%s", pid :%i\n", current->comm, current->pid);
comm:当前进程执行的程序文件名,
pid:当前进程的pid号。

current宏
是一个全局指针,指向当前进程的struct task_struct结构体,即表示当前进程。
  例如current->pid就能得到当前进程的pid,current->comm就能得到当前进程的名称。
  每个进程会有两个栈,一个用户栈,存在于用户空间,一个内核栈,存在于内核空间。
  当进程在用户空间运行时,cpu堆栈指针寄存器里面的内容是用户堆栈地址,使用用户栈;
  当进程在内核空间时,cpu堆栈指针寄存器里面的内容是内核栈空间地址,使用内核栈。
  在陷入内核后,系统调用中也是存在函数调用和自动变量,这些都需要栈支持。、
  当进程因为中断或者系统调用而陷入内核态时,进程所使用的堆栈也要从用户栈转到内核栈。


通过init_task获取内核中所有struct task_struct信息

linux-imx/init/init_task.c
struct task_struct init_task = INIT_TASK(init_task);
union thread_union init_thread_union __init_task_data =  { INIT_THREAD_INFO(init_task) };

//_init_task_data会被保存到一个特殊的段中
linux-imx/include/linux/init_task.h
#define __init_task_data __attribute__((__section__(".data..init_task")))

参考代码

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/list.h>

static __init int printPid(void) //安装模块函数
{
    struct task_struct *task,*p;
    struct list_head *ps;
    int count=0;
    printk("begin.\n");
    task=&init_task;
    list_for_each(ps,&task->tasks)
    {
        p=list_entry(ps,struct task_struct,tasks);
        count++;
        printk("%d\t%s\n",p->pid,p->comm);
    }
    printk("Process counts:%d\n",count);
    return 0;
}
static __exit void exitPid(void)  //卸载函数
{
    printk("exit!\n");
}
module_init(printPid); //实现的函数必须放入其中
module_exit(exitPid);
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luckywang1103

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值