读书笔记系列 - Operating Systems: Three Easy Pieces - Virtualization - Chapter 4: Processes


4. Processes

  1. The definition of a process, informally, is quite simple: it is a running program.
  2. The program itself is a lifeless thing: it just sits there on the disk, a bunch of instructions (and maybe some static data), waiting to spring into action.
  3. This basic technique, known as time sharing of the CPU, allows users to run as many concurrent processes as they would like; the potential cost is performance, as each will run more slowly if the CPU(s) must be shared.
  4. To implement virtualization of the CPU, and to implement it well, the OS will need both some low-level machinery and some high-level intelligence.
  5. We call the low-level machinery mechanisms; mechanisms are low-level methods or protocols that implement a needed piece of functionality.
  6. On top of these mechanisms resides some of the intelligence in the OS, in the form of policies. Policies are algorithms for making some kind of decision within the OS.

4.1 TheAbstraction: A Process

  1. A process is simply a running program; at anyinstant in time, we can summarize a process by taking an inventory of the different pieces of the system it accesses or affects during the course of its execution.
  2. Machine state: what a program can read or update when it is running. At any given time, what parts of the machine are important to the execution of this program?
  3. Memory; Registers.
  4. TIP: SEPARATE POLICY AND MECHANISM在这里插入图片描述

4.2 Process API

  1. Create: An operating system must include some method to create new processes.
  2. Destroy: As there is an interface for process creation, systems also provide an interface to destroy processes forcefully.
  3. Wait: Sometimes it is useful to wait for a process to stop running; thus some kind of waiting interface is often provided.
  4. Miscellaneous Control: Other than killing or waiting for a process, there are sometimes other controls that are possible. For example, most operating systems provide some kind of method to suspend a process (stop it from running for a while) and then resume it (continue it running).
  5. Status: There are usually interfaces to get some status information about a process as well, such as how long it has run for, or what state it is in.

4.3 Process Creation: A Little More Detail

  1. Loading: From Program To Process在这里插入图片描述
  2. The first thing that the OS must do to run a programis to load its code and any static data (e.g., initialized variables) into memory, into the address space of the process.
  3. Once the code and static data are loaded into memory, there are a few other things the OS needs to do before running the process. Some memory must be allocated for the program’s run-time stack (or just stack).
  4. The OS may also allocate some memory for the program’s heap.
  5. The OS will also do some other initialization tasks, particularly as related to input/output (I/O).
  6. By loading the code and static data into memory, by creating and initializing a stack, and by doing other work as related to I/O setup, the OS has now (finally) set the stage for program execution. It thus has one last task: to start the program running at the entry point, namely main(). By jumping to the main() routine (through a specialized mechanism that we will discuss next chapter), the OS transfers control of the CPU to the newly-created process, and thus the program begins its execution.

4.4 Process States

  1. In a simplified view, a process can be in one of three states:
    1. Running: In the running state, a process is running on a processor. This means it is executing instructions.
    2. Ready: In the ready state, a process is ready to run but for some reason the OS has chosen not to run it at this given moment.
    3. Blocked: In the blocked state, a process has performed some kind of operation that makes it not ready to run until some other event takes place. A common example: when a process initiates an I/O request to a disk, it becomes blocked and thus some other process can use the processor.
  2. Process: State Transitions在这里插入图片描述
  3. Being moved from ready to running means the process has been scheduled; being moved from running to ready means the process has been descheduled. Once a process has become blocked (e.g., by initiating an I/O operation), the OS will keep it as such until some event occurs (e.g., I/O completion); at that point, the process moves to the ready state again (and potentially immediately to running again, if the OS so decides).

4.5 Data Structures

  1. The register context will hold, for a stopped process, the contents of its registers. When a process is stopped, its registers will be saved to this memory location; by restoring these registers (i.e., placing their values back into the actual physical registers), the OS can resume running the process. We’ll learn more about this technique known as a context switch in future chapters.
  2. Sometimes a system will have an initial state that the process is in when it is being created. Also, a process could be placed in a final state where it has exited but has not yet been cleaned up. This final state can be useful as it allows other processes (usually the parent that created the process) to examine the return code of the process and see if the just-finished process executed successfully (usually, programs return zero in UNIX-based systems when they have accomplished a task successfully, and non-zero otherwise). When finished, the parent will make one final call (e.g., wait()) to wait for the completion of the child, and to also indicate to the OS that it can clean up any relevant data structures that referred to the now-extinct process.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Kuo-Teng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值