find_get_pid()

内核代码中函数定义的格式:

extern struct pid *find_get_pid(int nr);

struct pid *find_get_pid(pid_t nr)
{
	struct pid *pid;
	
	rcu_read_lock();
	pid = get_pid(find_vpid(nr));
	rcu_read_unlock();
	
	return pid;
}
EXPORT_SYMBOL_GPL(find_get_pid);

struct pid
{
	atomic_t count;   //当前使用过此进程的任务数量
	unsigned int level;  //number[]数组下表,一般取址为0
	/* lists of tasks that use this pid */
	struct hlist_head tasks[PIDTYPE_MAX];  //当前使用此进程的任务列表
	struct rcu_head rcu;
	struct upid numbers[1];  //struct upid类型的数组
};

typedef struct {
	int counter;
} atomic_t;

/*
 * struct upid is used to get the id of the struct pid, as it is
 * seen in particular namespace. Later the struct pid is found with
 * find_pid_ns() using the int nr and struct pid_namespace *ns.
 */

struct upid {
	/* Try to keep pid_chain in the same cacheline as nr for find_vpid */
	int nr;
	struct pid_namespace *ns;
	struct hlist_node pid_chain;
};
//find_get_pid.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pid.h>

static int __init find_get_pid_init(void)
{
  struct pid *kpid;

  printk("the funciton is %s\n",__func__);
  kpid = find_get_pid(2);
  printk("the count of pid is :%d\n",kpid->count);
  printk("the level of pid is :%d\n",kpid->level);

  printk("the pid of find_get_pid is :%d\n",kpid->numbers[kpid->level].nr);
  return 0;

}

static void __init find_get_pid_exit(void)
{
  printk("Bye bye find_get_pid\n");
}

module_init(find_get_pid_init);
module_exit(find_get_pid_exit); 
MODULE_LICENSE("GPL");

Makefile:

obj-m += find_get_pid.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

实现效果:

[76035.130956] the funciton is find_get_pid_init
[76035.130958] the count of pid is :11
[76035.130959] the level of pid is :0
[76035.130959] the pid of find_get_pid is :2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值