What is the relationship between task_struct and pid_namespace?

I am studying some kernel code and trying to understand how data structures are related to each other. I know the basic idea of ​​how the scheduler works and what PID is. However, I have no idea what a namespace is in this context, and I cannot understand how they all work together.

I read some explanations (including the O'Reilly parts of Understanding the Linux Kernel) and understand that it is possible that the same PID got into two processes because one ended and the identifier was redistributed. But I can’t understand how all this is done.

So:

  1. What is a namespace in this context?
  2. What is the relationship between task_structand pid_namespace? (I already understood what this is connected with pid_t, but I don’t know how)

Some links:

+5
source share
1 answer

Perhaps these links may help:

After going through the second link, it becomes clear that namespaces are a great way to isolate resources. And in any OS, including Linux, processes are one of the most important resources. In your own words

, , PID "1" . "chroot" . , 4- ...

, , / . , PID , "". , , root. , , .

, , . , , .

LWN article PID. :

PID, , struct pid. , , ID, node, - . . PID: (PID), (PGID) (SID). PGID SID , , , , . PID . , PID , . PID 1024 256 . , struct pid . struct pid PID:

struct pid {
 atomic_t count;                          /* reference counter */
 int nr;                                  /* the pid value */
 struct hlist_node pid_chain;             /* hash chain */
 struct hlist_head tasks[PIDTYPE_MAX];    /* lists of tasks */
 struct rcu_head rcu;                     /* RCU helper */
};

:

struct upid {
   int nr;                            /* moved from struct pid */
   struct pid_namespace *ns;          /* the namespace this value
                                       * is visible in */
   struct hlist_node pid_chain;       /* moved from struct pid */
};

struct pid {
   atomic_t count;
   struct hlist_head tasks[PIDTYPE_MAX];
   struct rcu_head rcu;
   int level;                     /* the number of upids */
   struct upid numbers[0];
};

, struct upid PID - PID. struct pid PID , , task_pid_nr(), pid_nr_ns(), find_task_by_vpid() ..

, , . , . struct nsproxy. , . PID, . PID task_active_pid_ns.

struct task_struct - , nsproxy, struct nsproxy. , , task_struct, struct nsproxy struct pid.

Linux , execve ( exec). , do_fork copy_process.

:

  • task_struct dup_task_struct.
  • copy_namespaces. nsproxy , nsproxy .
  • INIT ( PID, , ), PID alloc_pid, PID fork ed . :

    nr = alloc_pidmap(tmp);
    if(nr<0)
       goto out_free;
    pid->numbers[i].nr = nr;
    pid->numbers[i].ns = tmp;
    

upid, PID, , .

, copy process, PID task_struct pid_nr, ( PID nr, INIT) PID task_struct.

copy_process task_struct PID pid_link task_struct attach_pid.

Theres , , .

. ( ) , : 3.17.2.

+8

All Articles