Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


p->real_parent = current;
p->parent = p->real_parent;

Regular processes that are not threadscan trigger the same behavior by settingCLONE_PARENT.Another
correction is required for threads: The thread group leader of a regular process is the process itself. For a
thread, the group leader is the group leader of the current process:

kernel/fork.c
p->group_leader = p;

if (clone_flags & CLONE_THREAD) {
p->group_leader = current->group_leader;
list_add_tail_rcu(&p->thread_group, &p->group_leader->thread_group);
...
}

The new process must then be linked with its parent process by means of thechildrenlist. This is
handled by the auxiliary macroadd_parent. Besides, the new process must be included in the ID data
structure network as described in Section 2.3.3.

kernel/fork.c
add_parent(p);

if (thread_group_leader(p)) {
if (clone_flags & CLONE_NEWPID)
p->nsproxy->pid_ns->child_reaper = p;

set_task_pgrp(p, task_pgrp_nr(current));
set_task_session(p, task_session_nr(current));
attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
attach_pid(p, PIDTYPE_SID, task_session(current));
}

attach_pid(p, PIDTYPE_PID, pid);
...
return p;
}

thread_group_leaderchecks only whetherpidandtgidof the new process are identical. If so, the
process is the leader of a thread group. In this case, some more work is necessary:

❑ Recall that processes in a process namespace that is not the global namespace have their own
inittask. If a new PID namespace was opened by settingCLONE_NEWPID,thisrolemustbe
assumed by the task that calledclone.
❑ The new process must be added to the current task group and session. This allows for bringing
some of the functions discussed above to good use.

Finally, the PID itself is added to the ID network.This concludes the creation of a new process!

SpecialPoints When GeneratingThreads


Userspace thread libraries use theclonesystem call to generate new threads. This call supports flags
(other than those discussed above) that produce certain special effects in thecopy_process(and in the
Free download pdf