• 【操作系统导论】Abstraction Process | 进程 | 虚拟化 | 进程API


    💭 写在前面

    本系列博客为复习操作系统导论的笔记,内容主要参考自:

    • Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy PiecesA. Silberschatz, P. Galvin, and G. Gagne,
    • Operating System Concepts, 9th Edition, John Wiley & Sons, Inc., 2014, ISBN 978-1-118-09375-7.Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .

    0x00 什么是进程?

    " Process is a running program. "

    何为进程?进程就是运行中的程序。

    A program is a passive entity. (比如存储在磁盘上的可执行文件)

    A process is an active entity with a program counter.

    当一个可执行文件被加载到内存中时,一个程序就成为了一个进程。

    一个程序可以创建多个进程。

    我们几乎可以互换地使用工作、任务和进程。

    A process is comprised of code section, data section, stack and heap and program counter.

    一个进程是由代码、数据、栈和堆,以及程序计数器组成的。

    0x01 系统是如何营造出有许多CPU的错觉的

    CPU虚拟化(CPU virtualizing)!

    • 操作系统可以营造有许多CPU的错觉。
    • 时间共享(Time Sharing):运行一个进程,然后停止它去运行另一个进程(潜在的开销 - 性能的损失)。

    🔺 总结:操作系统通过虚拟化(virtualizing)CPU 来提供这种假象。

    0x02 进程API

    这些 API 适用于现代 CPU:

    创建(Create):

    • Create a new process to run a program(创建一个新的进程,在 shell 中键入命令或双击应用程序图标时,会调用操作系统来创建新进程,运行指定的程序)

    销毁(Destroy):

    • Halt a runaway process (由于存在创建进程的接口,对应的操作系统还提供了一个强制销毁进程的接口。当然,很多进程会在运行完成后自行退出。但是,如果他们不退出,用户可能希望终止他们,因此停止失控进程的接口非常有用。)

    等待(Wait):

    • Wait for a process to stop running(有时候等待进程运行是有用的,因此提供某种等待接口)

    其他控制(Miscellaneous Control):

    • Some kind of method to suspend a process and then resume it(例如:大多数操作系统提供某种方法来暂停进程)

    状态(Status):

    • Get some status info about a proces(通常有一些接口可以获得有关进程的状态信息,例如运行了多长事件,或者处于什么状态)

    0x03 创建进程(Process Creation)

    Step1:将一个程序代码载入到内存中(进入进程的地址空间)

    • 程序最初以可执行格式(executable format)的状态存在磁盘(disk)上
    • 现代操作系统会惰性处理(lazily),像是有拖延症一样去执行加载的过程
      • 在程序需要时才去加载代码和数据段(可以理解为不到DDL不去做,就硬托)
      • "Loading pieces of code or data only as they are needed during program execution."

    Step2:程序运行时栈被分配

    • 将栈用于局部变量(local variables)、函数参数(function parameters)和返回地址(return address)。
    • 用主函数的 argc 和 argv 数组初始化栈。

    Step3:程序的堆被创建

    • 用于动态分配有明确需求的数据
    • 程序通过调用 malloc() 来申请空间,通过调用 free() 来释放空间

    Step4:OS做一些其他的初始化任务

    • Input / Output (I/O)设置
      • Each process by default has three open file descriptors.
      • Standard input (stdin), standard output (stdout) and standard error (stderr)

    Step5:启动入口程序 main

    • OS将CPU控制权转移给新创建的进程。

    在内存中的进程:

    0x04 程序状态和中转(Process States and Transition)

    一个进程通常有三种状态:

    Ready(就绪):进程已经准备好运行,但由于某种原因,操作系统选择不在此时运行。

    A process is ready to run but for some reason the OS has chosen not to run it at this given moment.   在就绪状态下,进程已经准备好运行,但由于某种原因,操作系统选择不在此时运行。

    Running(运行):进程正在处理器上运行

    A process is running on a processor.  在运行状态下,进程正在处理器上运行(这意味着它正在执行指令)。

    Blocked(阻塞):一个进程执行了某个操作(比如I/O),因此其他进程可以使用处理器。

    A process has performed some kinds of operations. (such as IO)

    When a process initates an I/O request to a disk, it becomes blocked and thus some other process can use the processor.

    在阻塞状态下,一个进程执行了某种操作,直到发生其他事时才会准备运行。比如进程向磁盘发起 I/O 请求时,它会被阻塞。因此其他进程可以使用处理器。

    将这些状态映射到图上表示:

    (进程:状态转换)

    Example : Tracing Process State (跟踪进程状态)

    0x05 数据结构(Data Structures)

    PCB (Process Control Block)    - 进程控制块

    用于存储关于进程的信息的个体结构称为进程控制块,是谈论包含每个进程信息的C结构的一种方式。

    A structure that contains information about each process

    Includes register context: a set of registers that define the state of a process.

    Other information associated with each process

    Process List (Queue)   - 进程列表

    Ready processes(进程准备)

    Blocked processes(进程阻塞)

    Current running processes(当前运行进程)

    例子:XV6 内核结构

    1. // the information xv6 tracks about each process
    2. // including its register context and state
    3. struct proc {
    4. char *mem; // Start of process memory
    5. uint sz; // Size of process memory
    6. char *kstack; // Bottom of kernel stack
    7. // for this process
    8. enum proc_state state; // Process state
    9. int pid; // Process ID
    10. struct proc *parent; // Parent process
    11. void *chan; // If non-zero, sleeping on chan
    12. int killed; // If non-zero, have been killed
    13. struct file *ofile[NOFILE]; // Open files
    14. struct inode *cwd; // Current directory
    15. struct context context; // Switch here to run process
    16. struct trapframe *tf; // Trap frame for the
    17. // current interrupt
    18. };
    19. // the registers xv6 will save and restore
    20. // to stop and subsequently restart a process
    21. struct context {
    22. int eip; // Index pointer register
    23. int esp; // Stack pointer register
    24. int ebx; // Called the base register
    25. int ecx; // Called the counter register
    26. int edx; // Called the data register
    27. int esi; // Source index register
    28. int edi; // Destination index register
    29. int ebp; // Stack base pointer register
    30. };
    31. // the different states a process can be in
    32. enum proc_state { UNUSED, EMBRYO, SLEEPING,
    33. RUNNABLE, RUNNING, ZOMBIE };

    Example : Linux task_struct

    1. 📌 [ 笔者 ]   王亦优
    2. 📃 [ 更新 ]   2022.10.20
    3. ❌ [ 勘误 ]   /* 暂无 */
    4. 📜 [ 声明 ]   由于作者水平有限,本文有错误和不准确之处在所难免,
    5. 本人也很想知道这些错误,恳望读者批评指正!

    📜 参考资料 

    Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces

    A. Silberschatz, P. Galvin, and G. Gagne,

    Operating System Concepts, 9th Edition, John Wiley & Sons, Inc., 2014, ISBN 978-1-118-09375-7.

    Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .

    百度百科[EB/OL]. []. https://baike.baidu.com/.

  • 相关阅读:
    1207、mysql主从同步、复制模式
    C++的一些好用的限定修饰符
    IntelliJ IDEA 2022.2发布首个Beta版本,看看有哪些更新
    el-input: 把不符合正则校验的值动态清空,只保留符合的值
    Spring Security 自定义拦截器Filter实现登录认证
    学习java第五十一天
    @ConfigurationProperties配置绑定~
    Git clone时报错: OpenSSL SSL_read: Connection was reset, errno 10054
    postgres 数据库架构介绍--1
    【无标题】
  • 原文地址:https://blog.csdn.net/weixin_50502862/article/details/126762194