概念介绍
程序:指指令,数据以及组织形式描述
进程:不是运行单位,计算机已经运行程序,而是线程的容器。
进程的四要素:
1 有一段程序供其执行
2 有进程专用的系统堆栈空间
3 在内核有task_struct描述
4 进程有独立的存储空间,拥有专有的用户空间
有1,2,3缺少4,称为线程;
若完全没有用户空间,称为内核线程;
若共享用户空间,称为用户线程
生命周期
当linux启动的时候会启动0号进程init,然后其它的进程都是通过系统调用fork/clone来分离产生其它的所有进程。
进程的状态:运行态,就绪等待态,睡眠态(轻中重度),终止态,僵尸状态
在进程描述结构体中有一个voliate int state来标识进程是什么状态。无论何种状态的进程都会存在进程表中,内核中专门有进程表来进行管理所有的进程。
task_struct
内核结构体详细描述:
Linux 5.16.18 include/linux/sched.h文件中
struct task_struct {
#ifdef CONFIG_THREAD_INFO_IN_TASK
// 每种体系结构下都实现的不同,task_struct是描述通用的,此描述不同的体系结构
//与stack指针共同描述
struct thread_info thread_info;
#endif
// 进程状态
unsigned int __state;
#ifdef CONFIG_PREEMPT_RT
unsigned int saved_state;
#endif
randomized_struct_fields_start
// 指向内核栈指针
void *stack;
// 几个进程使用此结构
refcount_t usage;
// 标识 ptrace系统调用,实现断点调试
unsigned int flags;
unsigned int ptrace;
#ifdef CONFIG_SMP // 条件编译smp处理器
int on_cpu;
struct __call_single_node wake_entry;
unsigned int wakee_flips;
unsigned long wakee_flip_decay_ts;
struct task_struct *last_wakee;
int recent_used_cpu;
int wake_cpu;
#endif
// 运行队列和进程调度相关的
int on_rq;
int prio;
int static_prio;
int normal_prio;
unsigned int rt_priority;
struct sched_entity se;
struct sched_rt_entity rt;
struct sched_dl_entity dl;
const struct sched_class *sched_class;
#ifdef CONFIG_SCHED_CORE
struct rb_node core_node;
unsigned long core_cookie;
unsigned int core_occupation;
#endif
#ifdef CONFIG_CGROUP_SCHED
// 线程组结构体链表
struct task_group *sched_task_group;
#endif
#ifdef CONFIG_UCLAMP_TASK
struct uclamp_se uclamp_req[UCLAMP_CNT];
struct uclamp_se uclamp[UCLAMP_CNT];
#endif
struct sched_statistics stats;
#ifdef CONFIG_PREEMPT_NOTIFIERS
struct hlist_head preempt_notifiers;
#endif
#ifdef CONFIG_BLK_DEV_IO_TRACE
// 块设备io层的跟踪工具
unsigned int btrace_seq;
#endif
//进程调度策略相关的字段
unsigned int policy;
int nr_cpus_allowed;
const cpumask_t *cpus_ptr;
cpumask_t *user_cpus_ptr;
cpumask_t cpus_mask;
void *migration_pending;
#ifdef CONFIG_SMP
unsigned short migration_disabled;
#endif
unsigned short migration_flags;
// RCU同步原语
#ifdef CONFIG_PREEMPT_RCU
int rcu_read_lock_nesting;
union rcu_special rcu_read_unlock_special;
struct list_head rcu_node_entry;
struct rcu_node *rcu_blocked_node;
#endif /* #ifdef CONFIG_PREEMPT_RCU */
#ifdef CONFIG_TASKS_RCU
unsigned long rcu_tasks_nvcsw;
u8 rcu_tasks_holdout;
u8 rcu_tasks_idx;
int rcu_tasks_idle_cpu;
struct list_head rcu_tasks_holdout_list;
#endif /* #ifdef CONFIG_TASKS_RCU */
#ifdef CONFIG_TASKS_TRACE_RCU
int trc_reader_nesting;
int trc_ipi_to_cpu;
union rcu_special trc_reader_special;
bool trc_reader_checked;
struct list_head trc_holdout_list;
#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
struct sched_info sched_info;
// 进程架构链表
struct list_head tasks;
#ifdef CONFIG_SMP
struct plist_node pushable_tasks;
struct rb_node pushable_dl_tasks;
#endif
// 进程管理进程的地址空间,独立4G 32位*86
struct mm_struct *mm;
struct mm_struct *active_mm;
/* Per-thread vma caching: */
struct vmacache vmacache;
#ifdef SPLIT_RSS_COUNTING
struct task_rss_stat rss_stat;
#endif
// 进程状态参数
int exit_state;
int exit_code;