struct task_struct *pid_task(struct pid *pid, enum pid_type type) 用于根据pid和其类型找到对应的task_struct
其中pid的类型如下:
enum pid_type
{
PIDTYPE_PID,
PIDTYPE_PGID,
PIDTYPE_SID,
PIDTYPE_MAX
};
其使用的例子如下:
int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
{
int error = -ESRCH;
struct task_struct *p;
for (;;) {
rcu_read_lock();
//通过pid和其对应的类型PIDTYPE_PID 找到对应的task_struct,使用pid_task来查询pid的时候,需要通过
//rcu_read_lock/rcu_read_unlock 保护起来.
p = pid_task(pid, PIDTYPE_PID);
if (p)
error = group_send_sig_info(sig, info, p);
rcu_read_unlock();
if (likely(!p || error != -ESRCH))
return error;
}
}
其源码分析如下:
struct task_struct *pid_task(struct pid *pid, enum pid_type type)
{
struct task_struct *result = NULL;
#首先判断pid不能为null,pid为null的话,pid_task 返回的值也就是null
if (pid) {
struct hlist_node *first;
#通过形参pid->tasks[type] ,就可以找到hlist_node *first。
first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
lockdep_tasklist_lock_is_held());
#这里的#define hlist_entry(ptr, type, member) container_of(ptr,type,member)。所以这里等于让container_of根据hlist_node
#找到在struct task_struct中的offset pids[(type)].node 找到task_struct
if (first)
result = hlist_entry(first, struct task_struct, pids[(type)].node);
}
return result;
}
进程管理API之pid_task
最新推荐文章于 2024-04-26 13:51:04 发布