进程管理API之pid_task

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值