Linux内核的 `init_task` 定义在 `init/init_task.c` 文件中,其功能是作为内核线程的起点,在内核启动时初始化进程0的任务结构体。
`init_task` 的定义如下:
```c
struct task_struct init_task = INIT_TASK(init_task);
```
其中,`INIT_TASK` 宏定义在 `include/linux/sched.h` 文件中,用于初始化 `task_struct` 结构体的各个字段。`init_task` 的完整定义如下:
```c
struct task_struct init_task = {
.state = 0,
.stack = init_stack,
.usage = ATOMIC_INIT(2),
.flags = PF_KTHREAD,
.prio = MAX_PRIO-20,
.static_prio = MAX_PRIO-20,
.normal_prio = MAX_PRIO-20,
#ifdef CONFIG_SCHED_DEADLINE
.dl = {
.dl_runtime = RUNTIME_INF,
.dl_deadline = DEADLINE_INF,
.dl_period = PERIOD_INF,
},
#endif
#ifdef CONFIG_CGROUP_SCHED
.se = {
.group_node = {
.kn = KERN_ID,
},
.parent = &se_root,
.cfs_rq = &init_task.rq->cfs,
.avg = {
.runnable_sum = WMULT_CONST(TASK_MAX, WMULT),
.runnable_avg = WMULT_CONST(TASK_MAX, WMULT),
.decay_count = 0,
},
.delta_exec = 0,
.delta_fair = 0,
.sum_exec_runtime = 0,
.vruntime = 0,
.fair_sleep_stamp = 0,
.last_wakeup = 0,
.sleep_start = 0,
.last_schedule = 0,
.cookies = {
[0] = -1,
[1] = -1,
},
},
#endif
#ifdef CONFIG_RT_MUTEXES
.pi_lock_task = {
.prev = LIST_HEAD_INIT(init_task.pi_lock_task.prev),
.next = LIST_HEAD_INIT(init_task.pi_lock_task.next),
.lock_count = 0,
},
#endif
.tasks = LIST_HEAD_INIT(init_task.tasks),
.ptraced = LIST_HEAD_INIT(init_task.ptraced),
.thread_node = LIST_HEAD_INIT(init_task.thread_node),
.se.on_rq = 0,
.se.exec_start = 0,
.cpu_timers = INIT_CPU_TIMERS(init_task),
.pi_state = { 0 },
.wake_entry = { NULL, NULL },
.blkio = { NULL, },
.io_context = NULL,
.splice_pipe = NULL,
.sighand = &init_sigpending,
.signal = {
.rlim = INIT_RLIMITS,
.siglock = __SPIN_LOCK_UNLOCKED(init_task.signal.siglock),
.shared_pending = LIST_HEAD_INIT(init_task.signal.shared_pending),
.group_exit_code = 0,
},
.blocked = {{0, 0}},
.alloc_lock = __SPIN_LOCK_UNLOCKED(init_task.alloc_lock),
.journal_info = NULL,
.cpu = 0,
.cpus_allowed = CPU_MASK_ALL,
.ptrace = {
.tracehook = NULL,
.tracer = NULL,
},
.set_child_tid = NULL,
.clear_child_tid = NULL,
.utime = 0,
.stime = 0,
.utimescaled = 0,
.stimescaled = 0,
.gtime = 0,
.prev_cputime = 0,
.nvcsw = 0,
.nivcsw = 0,
.start_time = {0},
.real_start_time = {0},
.min_flt = 0,
.maj_flt = 0,
.cputime_expires = 0,
.cpu_timers_nohz = {
[0 ... TIMER_STATS_NSTATS-1] = TIMER_DEF_INITIALIZER,
},
.perf_event_list = LIST_HEAD_INIT(init_task.perf_event_list),
.perf_counter_ctx = NULL,
.pi_lock = &init_task.alloc_lock,
.timer_slack_ns = 50000, /* 50 usec default slack */
.pids = { NULL, },
.thread_group = LIST_HEAD_INIT(init_task.thread_group),
.thread_group_leader = &init_task,
.thread_pid = &init_struct_pid,
.group_leader = &init_task,
.used_math = 0,
#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
.vtime_seq = 0,
.vtime_snap = 0,
#endif
#ifdef CONFIG_ILLEGAL_POINTER_VALUE
.bad_iret = 0x00000000,
#endif
.splice_sched = INIT_LIST_HEAD(&init_task.splice_sched),
#ifdef CONFIG_TASK_XACCT
.acct_rss_mem1 = 0,
.acct_vm_mem1 = 0,
.acct_timexpd = 0,
#endif
#ifdef CONFIG_CPUSETS
.cpuset_mem_spread_rotor = 0,
.cpuset_slab_spread_rotor = 0,
.cpuset_mems_allowed = { { nodemask_all_pages } },
.mems_allowed_seqnr = 0,
#endif
#ifdef CONFIG_NUMA_BALANCING
.numa_group = NULL,
.numa_faults = 0,
#endif
#ifdef CONFIG_CGROUP_FREEZER
.freezer = {
.state = FROZEN,
.pids_frozen = 0,
.mnt_id = 0,
.pid_list = LIST_HEAD_INIT(init_task.freezer.pid_list),
.parent_swait = __WAIT_QUEUE_HEAD_INITIALIZER(init_task.freezer.parent_swait),
},
#endif
#ifdef CONFIG_DEBUG_MUTEXES
.blocked_on = NULL,
.blocked_on_lock = NULL,
#endif
};
```
`init_task` 结构体包含了多个字段,用于描述进程的各种属性和状态。其中比较重要的字段包括:
- `state`:进程状态。
- `stack`:进程的内核栈。
- `flags`:进程标志位。
- `prio`:进程优先级。
- `se`:进程的调度实体。
- `sighand`:进程的信号处理程序。
- `signal`:进程的信号处理队列。
- `blocked`:进程阻塞情况。
- `cpu`:进程正在运行的 CPU。
- `cpus_allowed`:进程可以运行的 CPU 集合。
- `thread_group_leader`:进程组的领导进程。
- `thread_pid`:进程的线程 ID。
- `group_leader`:进程组的领导进程。
这些字段都是内核调度器进行进程调度的必要信息。