inline struct pid *get_pid(struct pid *pid) 用于将struct pid结构体中的count字段加1 后,返回struct pid
其使用的例子如下:
static inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
struct ocfs2_lock_holder *oh)
{
INIT_LIST_HEAD(&oh->oh_list);
oh->oh_owner_pid = get_pid(task_pid(current));
}
其源码分析如下:
static inline struct pid *get_pid(struct pid *pid)
{
if (pid)
atomic_inc(&pid->count);
return pid;
}
可见如果pid 不为null,就增加pid->count后,返回pid
与get_pid对应的是put_pid
void put_pid(struct pid *pid)
{
struct pid_namespace *ns;
if (!pid)
return;
ns = pid->numbers[pid->level].ns;
if ((atomic_read(&pid->count) == 1) ||
atomic_dec_and_test(&pid->count)) {
kmem_cache_free(ns->pid_cachep, pid);
put_pid_ns(ns);
}
}
与get_pid 相比,除了判断pid不能为null,还会判断当前的pid->count是否等于1,如果等于1的话,再减去1的话,就是0.
说明后面没有人用这个pid了,因此要把ns中对pid的缓存去掉,并通过put_pid_ns将ns的ns->kref 一次减1
void put_pid_ns(struct pid_namespace *ns)
{
struct pid_namespace *parent;
while (ns != &init_pid_ns) {
parent = ns->parent;
if (!kref_put(&ns->kref, free_pid_ns))
break;
ns = parent;
}
}
可见如果当前ns的kref->refcount 等于0,就调用free_pid_ns释放当前的ns
static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
{
if (refcount_dec_and_test(&kref->refcount)) {
release(kref);
return 1;
}
return 0;
}
进程管理API之get_pid/ put_pid
最新推荐文章于 2024-05-03 00:34:10 发布