/*
* What is struct pid?
*
* A struct pid is the kernel's internal notion of a process identifier.
* It refers to individual tasks, process groups, and sessions. While
* there are processes attached to it the struct pid lives in a hash
* table, so it and then the processes that it refers to can be found
* quickly from the numeric pid value. The attached processes may be
* quickly accessed by following pointers from struct pid.
*
* pid结构是进程id的内核内部概念。它代表单独任务,进程组和会话。因为进程与它紧密相连且pid结构存储在一个哈希表中,
* 所以它代表的进程可以很容易由pid值快速查找到。同时由pid结构的指针可以很快的访问连接的进程。
*
* Storing pid_t values in the kernel and refering to them later has a
* problem. The process originally with that pid may have exited and the
* pid allocator wrapped, and another process could have come along
* and been assigned that pid.
*
* 在内核中储存pid_t值之后访问它们产生一个问题。原先与该pid相关的进程已经推出,且pid分配器已经清除了该pid,
* 其他进程可能紧接着分配了该pid。
*
* Referring to user space processes by holding a reference to struct
* task_struct has a problem. When the user space process exits
* the now useless task_struct is still kept. A task_struct plus a
* stack consumes around 10K of low kernel memory. More precisely
* this is THREAD_SIZE + sizeof(struct task_struct). By comparison
* a struct pid is about 64 bytes.
*
* 谈到用户空间进程持有一个task_struct结构的引用会产生一个问题。当用户空间进程退出,无用的task_struct仍然存在。
* task_struct增加了大约10k的低内核存储的栈消耗。更精确的是THREAD_SIZE + sizeof(struct task_struct)大小。
*
* Holding a reference to struct pid solves both of these problems.
* It is small so holding a reference does not consume a lot of
* resources, and since a new struct pid is allocated when the numeric pid
* value is reused (when pids wrap around) we don't mistakenly refer to new
* processes.
*/
/*
* 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;
};
struct pid
{
atomic_t count;
unsigned int level;
/* lists of tasks that use this pid */
struct hlist_head tasks[PIDTYPE_MAX];
struct rcu_head rcu;
struct upid numbers[1];
};
enum pid_type
{
PIDTYPE_PID,
PIDTYPE_PGID,
PIDTYPE_SID,
PIDTYPE_MAX
};
struct pid_link
{
struct hlist_node node;
struct pid *pid;
};
struct task_struct
{
...................................
pid_t pid;
pid_t tgid;
/* PID/PID hash table linkage. */
struct pid_link pids[PIDTYPE_MAX];
..................................
};
该图片引自《Professional linux kernel architecture》图中的mode应为node