进程原理基本概念

本文介绍了Linux进程的基本概念,包括进程的定义、四要素以及与线程的区别。详细讲解了进程的生命周期,从init进程开始,阐述了不同状态的转换。讨论了task_struct结构体在进程管理中的作用,以及进程优先级的分类。最后,提到了与进程相关的系统调用,如fork、vfork和clone,以及它们在创建和管理进程时的不同之处。
摘要由CSDN通过智能技术生成

概念介绍

程序:指指令,数据以及组织形式描述
进程:不是运行单位,计算机已经运行程序,而是线程的容器。
进程的四要素:
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;
	
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丰恒谷

你的鼓励是我最大创作动力!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值