分析Linux内核创建一个新进程的过程
进程描述
- 进程描述符(task_struct)
用来描述进程的数据结构,可以理解为进程的属性。比如进程的状态、进程的标识(PID)等,都被封装在了进程描述符这个数据结构中,该数据结构被定义为task_struct
- 进程控制块(PCB)
是操作系统核心中一种数据结构,主要表示进程状态。
- 进程状态

- fork()
fork()在父、子进程各返回一次。在父进程中返回子进程的 pid,在子进程中返回0。
-
fork一个子进程的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 1
2
3#include <stdio.h>
4
5
6#include <stdlib.h>
7
8
9#include <unistd.h>
10
11int main(int argc, char * argv[])
12{
13int pid;
14/* fork another process */
15
16pid = fork();
17if (pid < 0)
18{
19 /* error occurred */
20 fprintf(stderr,"Fork Failed!");
21 exit(-1);
22}
23else if (pid == 0)
24{
25 /* child process */
26 printf("This is Child Process!\n");
27}
28else
29{
30 /* parent process */
31 printf("This is Parent Process!\n");
32 /* parent will wait for the child to complete*/
33 wait(NULL);
34 printf("Child Complete!\n");
35}
36}
37
38
进程创建
大致流程
fork 通过0x80中断(系统调用)来陷入内核,由系统提供的相应系统调用来完成进程的创建。
-
fork.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 1//fork
2#ifdef __ARCH_WANT_SYS_FORK
3SYSCALL_DEFINE0(fork)
4{
5#ifdef CONFIG_MMU
6 return do_fork(SIGCHLD, 0, 0, NULL, NULL);
7#else
8 /* can not support in nommu mode */
9 return -EINVAL;
10#endif
11}
12#endif
13
14//vfork
15#ifdef __ARCH_WANT_SYS_VFORK
16SYSCALL_DEFINE0(vfork)
17{
18 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0,
19 0, NULL, NULL);
20}
21#endif
22
23//clone
24#ifdef __ARCH_WANT_SYS_CLONE
25#ifdef CONFIG_CLONE_BACKWARDS
26SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
27 int __user *, parent_tidptr,
28 int, tls_val,
29 int __user *, child_tidptr)
30#elif defined(CONFIG_CLONE_BACKWARDS2)
31SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
32 int __user *, parent_tidptr,
33 int __user *, child_tidptr,
34 int, tls_val)
35#elif defined(CONFIG_CLONE_BACKWARDS3)
36SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
37 int, stack_size,
38 int __user *, parent_tidptr,
39 int __user *, child_tidptr,
40 int, tls_val)
41#else
42SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
43 int __user *, parent_tidptr,
44 int __user *, child_tidptr,
45 int, tls_val)
46#endif
47{
48 return do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr);
49}
50#endif
51
52
通过看上边的代码,我们可以清楚的看到,不论是使用 fork 还是 vfork 来创建进程,最终都是通过 do_fork() 方法来实现的。接下来我们可以追踪到 do_fork()的代码(部分代码,经过笔者的精简):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 1
2long do_fork(unsigned long clone_flags,
3 unsigned long stack_start,
4 unsigned long stack_size,
5 int __user *parent_tidptr,
6 int __user *child_tidptr)
7{
8 //创建进程描述符指针
9 struct task_struct *p;
10
11 //……
12
13 //复制进程描述符,copy_process()的返回值是一个 task_struct 指针。
14 p = copy_process(clone_flags, stack_start, stack_size,
15 child_tidptr, NULL, trace);
16
17 if (!IS_ERR(p)) {
18 struct completion vfork;
19 struct pid *pid;
20
21 trace_sched_process_fork(current, p);
22
23 //得到新创建的进程描述符中的pid
24 pid = get_task_pid(p, PIDTYPE_PID);
25 nr = pid_vnr(pid);
26
27 if (clone_flags & CLONE_PARENT_SETTID)
28 put_user(nr, parent_tidptr);
29
30 //如果调用的 vfork()方法,初始化 vfork 完成处理信息。
31 if (clone_flags & CLONE_VFORK) {
32 p->vfork_done = &vfork;
33 init_completion(&vfork);
34 get_task_struct(p);
35 }
36
37 //将子进程加入到调度器中,为其分配 CPU,准备执行
38 wake_up_new_task(p);
39
40 //fork 完成,子进程即将开始运行
41 if (unlikely(trace))
42 ptrace_event_pid(trace, pid);
43
44 //如果是 vfork,将父进程加入至等待队列,等待子进程完成
45 if (clone_flags & CLONE_VFORK) {
46 if (!wait_for_vfork_done(p, &vfork))
47 ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
48 }
49
50 put_pid(pid);
51 } else {
52 nr = PTR_ERR(p);
53 }
54 return nr;
55}
56
57
58
59
do_fork 流程
- 调用 copy_process 为子进程复制出一份进程信息
- 如果是 vfork 初始化完成处理信息
- 调用 wake_up_new_task 将子进程加入调度器,为之分配 CPU
- 如果是 vfork,父进程等待子进程完成 exec 替换自己的地址空间
copy_process 流程
-
追踪copy_process 代码(部分)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 1static struct task_struct *copy_process(unsigned long clone_flags,
2 unsigned long stack_start,
3 unsigned long stack_size,
4 int __user *child_tidptr,
5 struct pid *pid,
6 int trace)
7{
8 int retval;
9
10 //创建进程描述符指针
11 struct task_struct *p;
12
13 //……
14
15 //复制当前的 task_struct
16 p = dup_task_struct(current);
17
18 //……
19
20 //初始化互斥变量
21 rt_mutex_init_task(p);
22
23 //检查进程数是否超过限制,由操作系统定义
24 if (atomic_read(&p->real_cred->user->processes) >=
25 task_rlimit(p, RLIMIT_NPROC)) {
26 if (p->real_cred->user != INIT_USER &&
27 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
28 goto bad_fork_free;
29 }
30
31 //……
32
33 //检查进程数是否超过 max_threads 由内存大小决定
34 if (nr_threads >= max_threads)
35 goto bad_fork_cleanup_count;
36
37 //……
38
39 //初始化自旋锁
40 spin_lock_init(&p->alloc_lock);
41 //初始化挂起信号
42 init_sigpending(&p->pending);
43 //初始化 CPU 定时器
44 posix_cpu_timers_init(p);
45
46
47 //……
48
49 //初始化进程数据结构,并把进程状态设置为 TASK_RUNNING
50 retval = sched_fork(clone_flags, p);
51
52 //复制所有进程信息,包括文件系统、信号处理函数、信号、内存管理等
53 if (retval)
54 goto bad_fork_cleanup_policy;
55
56 retval = perf_event_init_task(p);
57 if (retval)
58 goto bad_fork_cleanup_policy;
59 retval = audit_alloc(p);
60 if (retval)
61 goto bad_fork_cleanup_perf;
62 /* copy all the process information */
63 shm_init_task(p);
64 retval = copy_semundo(clone_flags, p);
65 if (retval)
66 goto bad_fork_cleanup_audit;
67 retval = copy_files(clone_flags, p);
68 if (retval)
69 goto bad_fork_cleanup_semundo;
70 retval = copy_fs(clone_flags, p);
71 if (retval)
72 goto bad_fork_cleanup_files;
73 retval = copy_sighand(clone_flags, p);
74 if (retval)
75 goto bad_fork_cleanup_fs;
76 retval = copy_signal(clone_flags, p);
77 if (retval)
78 goto bad_fork_cleanup_sighand;
79 retval = copy_mm(clone_flags, p);
80 if (retval)
81 goto bad_fork_cleanup_signal;
82 retval = copy_namespaces(clone_flags, p);
83 if (retval)
84 goto bad_fork_cleanup_mm;
85 retval = copy_io(clone_flags, p);
86
87 //初始化子进程内核栈
88 retval = copy_thread(clone_flags, stack_start, stack_size, p);
89
90 //为新进程分配新的 pid
91 if (pid != &init_struct_pid) {
92 retval = -ENOMEM;
93 pid = alloc_pid(p->nsproxy->pid_ns_for_children);
94 if (!pid)
95 goto bad_fork_cleanup_io;
96 }
97
98 //设置子进程 pid
99 p->pid = pid_nr(pid);
100
101
102 //……
103
104
105 //返回结构体 p
106 return p;
107
108
- 调用 dup_task_struct 复制当前的 task_struct
- 检查进程数是否超过限制
- 初始化自旋锁、挂起信号、CPU 定时器等
- 调用 sched_fork 初始化进程数据结构,并把进程状态设置为 TASK_RUNNING
- 复制所有进程信息,包括文件系统、信号处理函数、信号、内存管理等
- 调用 copy_thread 初始化子进程内核栈
- 为新进程分配并设置新的 pid
dup_task_struct 流程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 1
2static struct task_struct *dup_task_struct(struct task_struct *orig)
3{
4 struct task_struct *tsk;
5 struct thread_info *ti;
6 int node = tsk_fork_get_node(orig);
7 int err;
8
9 //分配一个 task_struct 节点
10 tsk = alloc_task_struct_node(node);
11 if (!tsk)
12 return NULL;
13
14 //分配一个 thread_info 节点,包含进程的内核栈,ti 为栈底
15 ti = alloc_thread_info_node(tsk, node);
16 if (!ti)
17 goto free_tsk;
18
19 //将栈底的值赋给新节点的栈
20 tsk->stack = ti;
21
22 //……
23
24 return tsk;
25
26}
27
28
-
调用alloc_task_struct_node分配一个 task_struct 节点
-
调用alloc_thread_info_node分配一个 thread_info 节点,其实是分配了一个thread_union联合体,将栈底返回给 ti
1
2
3
4
5 1union thread_union {
2 struct thread_info thread_info;
3 unsigned long stack[THREAD_SIZE/sizeof(long)];
4};
5
- 最后将栈底的值 ti 赋值给新节点的栈
最终执行完dup_task_struct之后,子进程除了tsk->stack指针不同之外,全部都一样!
sched_fork 流程
-
core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1int sched_fork(unsigned long clone_flags, struct task_struct *p)
2{
3 unsigned long flags;
4 int cpu = get_cpu();
5
6 __sched_fork(clone_flags, p);
7
8 //将子进程状态设置为 TASK_RUNNING
9 p->state = TASK_RUNNING;
10
11 //……
12
13 //为子进程分配 CPU
14 set_task_cpu(p, cpu);
15
16 put_cpu();
17 return 0;
18}
19
20
- 我们可以看到sched_fork大致完成了两项重要工作,一是将子进程状态设置为 TASK_RUNNING,二是为其分配 CPU
copy_thread 流程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 1int copy_thread(unsigned long clone_flags, unsigned long sp,
2 unsigned long arg, struct task_struct *p)
3{
4 //获取寄存器信息
5 struct pt_regs *childregs = task_pt_regs(p);
6 struct task_struct *tsk;
7 int err;
8
9 p->thread.sp = (unsigned long) childregs;
10 p->thread.sp0 = (unsigned long) (childregs+1);
11 memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
12
13 if (unlikely(p->flags & PF_KTHREAD)) {
14 //内核线程
15 memset(childregs, 0, sizeof(struct pt_regs));
16 p->thread.ip = (unsigned long) ret_from_kernel_thread;
17 task_user_gs(p) = __KERNEL_STACK_CANARY;
18 childregs->ds = __USER_DS;
19 childregs->es = __USER_DS;
20 childregs->fs = __KERNEL_PERCPU;
21 childregs->bx = sp; /* function */
22 childregs->bp = arg;
23 childregs->orig_ax = -1;
24 childregs->cs = __KERNEL_CS | get_kernel_rpl();
25 childregs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED;
26 p->thread.io_bitmap_ptr = NULL;
27 return 0;
28 }
29
30 //将当前寄存器信息复制给子进程
31 *childregs = *current_pt_regs();
32
33 //子进程 eax 置 0,因此fork 在子进程返回0
34 childregs->ax = 0;
35 if (sp)
36 childregs->sp = sp;
37
38 //子进程ip 设置为ret_from_fork,因此子进程从ret_from_fork开始执行
39 p->thread.ip = (unsigned long) ret_from_fork;
40
41 //……
42
43 return err;
44}
45
46
copy_thread 这段代码为我们解释了两个相当重要的问题!
- 一是,为什么 fork 在子进程中返回0,原因是childregs->ax = 0;这段代码将子进程的 eax 赋值为0
- 二是,p->thread.ip = (unsigned long) ret_from_fork;将子进程的 ip 设置为 ret_form_fork 的首地址,因此子进程是从 ret_from_fork 开始执行的
总结
新进程的执行源于以下前提:
- dup_task_struct中为其分配了新的堆栈
- 调用了sched_fork,将其置为TASK_RUNNING
- copy_thread中将父进程的寄存器上下文复制给子进程,保证了父子进程的堆栈信息是一致的
- 将ret_from_fork的地址设置为eip寄存器的值
最终子进程从ret_from_fork开始执行