欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

find_get_pid()

程序员文章站 2022-06-03 22:59:59
...

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

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

推荐阅读