当前位置: 首页 > news >正文

Linux内核进程管理子系统有什么第六十八回 —— 进程主结构详解(64)

接前一篇文章:Linux内核进程管理子系统有什么第六十七回 —— 进程主结构详解(63)

本文内容参考:

Linux内核进程管理专题报告_linux rseq-CSDN博客

《趣谈Linux操作系统 核心原理篇:第三部分 进程管理》—— 刘超

《图解Linux内核 基于6.x》 —— 姜亚华 机械工业出版社

https://blog.csdn.net/weixin_45030965/article/details/132734258

特此致谢!

进程管理核心结构 —— task_struct

12. current

前几回讲了current在x86以及ARM上的实现,本回来看本部分剩余的一个知识点:通过task_struct定位内核栈stack和通过内核栈定位task_struct。在其中还会涉及到一个重要的结构体 —— struct pt_regs。

  • 通过内核栈stack找task_struct

上一回讲解对于那些struct thread_info中没有struct task_struct的体系结构,该如何通过内核栈stack找到task_struct。

在新方案中用到了Per CPU 机制。本回对于该机制进行知识补强。

知识补强 —— Per CPU

Per CPU变量是Linux内核中的一种重要的同步机制。多核情况下,多个CPU是同时运行的,它们共同使用同一硬件资源的时候,需要解决多个CPU之间的同步问题。Per CPU变量就是为了解决这个问题应运而生的。顾名思义,Per CPU就是为每个CPU构造一个变量的副本,这样各个CPU就能够操作自己的副本而互不干涉。比如,当前进程的变量current_task就被声明为Per CPU变量。

要使用Per CPU变量,首先要声明这个变量,在arch/x86/include/asm/current.h中有:

#ifndef __ASSEMBLY__
struct task_struct;DECLARE_PER_CPU(struct task_struct *, current_task);static __always_inline struct task_struct *get_current(void)
{return this_cpu_read_stable(current_task);
}#define current get_current()#endif /* __ASSEMBLY__ */

然后定义(初始化)这个变量。在arch/x86/kernel/cpu/common.c中有:

DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
EXPORT_PER_CPU_SYMBOL(current_task);

也就是说,系统刚刚初始化的时候,current_task都指向init_task。

后面当某个CPU上的进程进行切换的时候,current_task被修改为将要切换到的目标进程。例如,后文书要讲到的__switch_to任务切换函数就会改变current_task。以x86为例,参见arch/x86/kernel/process_32.c:

当要获取当前运行中的task_struct的时候,就需要调用this_cpu_read_stable()进行读取。而这正是current及get_current()所做的事情。

this_cpu_read_stable是一个宏,其定义在arch/x86/include/asm/percpu.h中,如下:

/** this_cpu_read() makes gcc load the percpu variable every time it is* accessed while this_cpu_read_stable() allows the value to be cached.* this_cpu_read_stable() is more efficient and can be used if its value* is guaranteed to be valid across cpus.  The current users include* get_current() and get_thread_info() both of which are actually* per-thread variables implemented as per-cpu variables and thus* stable for the duration of the respective task.*/
#define this_cpu_read_stable_1(pcp)	percpu_stable_op(1, "mov", pcp)
#define this_cpu_read_stable_2(pcp)	percpu_stable_op(2, "mov", pcp)
#define this_cpu_read_stable_4(pcp)	percpu_stable_op(4, "mov", pcp)
#define this_cpu_read_stable_8(pcp)	percpu_stable_op(8, "mov", pcp)
#define this_cpu_read_stable(pcp)	__pcpu_size_call_return(this_cpu_read_stable_, pcp)

至此,通过task_struct寻找thread_info和通过thread_info查找task_struct的细节就讲完了。下一回讲解一个关键结构体 —— struct pt_regs。

欲知后事如何,且看下回分解。

http://www.dtcms.com/a/529221.html

相关文章:

  • 做网站需要商标注册吗阿里巴巴怎么做企业网站
  • 做视频网站需要哪些技术指标wordpress可以放视频吗
  • 动态库的使用-openssl
  • Maven 项目和 Maven Web 项目的异同点
  • Maven整理
  • 关于OpenAI CLIP的综合技术报告:架构、对比预训练与多模态影响
  • 网上服装商城网站代码软件开发 网站建设
  • 保洁网站模板闲置物品交易网站怎么做
  • 11月更新|流程节点新增数据变更+发起流程
  • 【Swift】LeetCode 189. 轮转数组
  • 聊城网站托管网络舆情处置方案
  • C#的operator运算符定义
  • 南通网站建设论文网站上传用什么软件做视频格式
  • ftp备份网站wordpress进管理员密码
  • 【移动语义】C++ 移动语义的秘传心法
  • 网站营销的优势电商app系统开发公司
  • 电影wordpress福州搜索优化行业
  • 中国建设银行网站软件下载工厂招聘信息
  • 能耗在线监测系统助企业实时监测管理能耗,提升能源利用率
  • 怎么根据别人的网站做自己的网站片头制作网站
  • Python3 标准库概览
  • 从 Transformer 理论到文本分类:BERT 微调实战总结
  • 基于Python利用正则表达式将英文双引号 “ 替换为中文双引号 “”
  • rwqsd
  • 个人网站 建站前端网站优化
  • 【Linux】深入浅出 Linux 自动化构建:make 与 Makefile 的实用指南
  • 六安市城乡建设网站沧州百姓网免费发布信息网
  • 俱乐部网站php源码网站构建的工作
  • 【AI论文】机器人学习:教程
  • 普宁网站建设django做网站和js做网站