
💭 写在前面
本系列博客为复习操作系统导论的笔记,内容主要参考自:
" 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.
一个进程是由代码、数据、栈和堆,以及程序计数器组成的。
CPU虚拟化(CPU virtualizing)!
🔺 总结:操作系统通过虚拟化(virtualizing)CPU 来提供这种假象。
这些 API 适用于现代 CPU:
创建(Create):
销毁(Destroy):
等待(Wait):
其他控制(Miscellaneous Control):
状态(Status):
Step1:将一个程序代码载入到内存中(进入进程的地址空间)
Step2:程序运行时栈被分配
Step3:程序的堆被创建
Step4:OS做一些其他的初始化任务
Step5:启动入口程序 main
在内存中的进程:

一个进程通常有三种状态:
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 (跟踪进程状态)

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 内核结构
- // the information xv6 tracks about each process
- // including its register context and state
- struct proc {
- char *mem; // Start of process memory
- uint sz; // Size of process memory
- char *kstack; // Bottom of kernel stack
- // for this process
- enum proc_state state; // Process state
- int pid; // Process ID
- struct proc *parent; // Parent process
- void *chan; // If non-zero, sleeping on chan
- int killed; // If non-zero, have been killed
- struct file *ofile[NOFILE]; // Open files
- struct inode *cwd; // Current directory
- struct context context; // Switch here to run process
- struct trapframe *tf; // Trap frame for the
- // current interrupt
- };
-
- // the registers xv6 will save and restore
- // to stop and subsequently restart a process
- struct context {
- int eip; // Index pointer register
- int esp; // Stack pointer register
- int ebx; // Called the base register
- int ecx; // Called the counter register
- int edx; // Called the data register
- int esi; // Source index register
- int edi; // Destination index register
- int ebp; // Stack base pointer register
- };
-
-
- // the different states a process can be in
- enum proc_state { UNUSED, EMBRYO, SLEEPING,
- RUNNABLE, RUNNING, ZOMBIE };
Example : Linux task_struct

- 📌 [ 笔者 ] 王亦优
- 📃 [ 更新 ] 2022.10.20
- ❌ [ 勘误 ] /* 暂无 */
- 📜 [ 声明 ] 由于作者水平有限,本文有错误和不准确之处在所难免,
- 本人也很想知道这些错误,恳望读者批评指正!
| 📜 参考资料 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/. |