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

简介: 读书笔记系列 - 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:
  2. Running: In the running state, a process is running on a processor. This means it is executing instructions.
  3. 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.
  4. 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.
  5. Process: State Transitions
  6. 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.


目录
相关文章
|
3月前
|
存储 网络协议 Linux
Overview of Concepts
Overview of Concepts
44 1
|
安全 Unix Shell
读书笔记系列 - Operating Systems: Three Easy Pieces - Virtualization - Chapter 5: Process API
读书笔记系列 - Operating Systems: Three Easy Pieces - Virtualization - Chapter 5: Process API
92 0
|
安全 内存技术
读书笔记系列 - Operating Systems: Three Easy Pieces - Intro
读书笔记系列 - Operating Systems: Three Easy Pieces - Intro
123 0
|
存储 缓存 安全
《optimizing software in c++》读书笔记(二)
《optimizing software in c++》读书笔记(二)
203 0
|
SQL 编译器 API
Efficiently Compiling Efficient Query Plans for Modern Hardware 论文解读
这应该是SQL查询编译的一篇经典文章了,作者是著名的Thomas Neumann,主要讲解了TUM的HyPer数据库中对于CodeGen的应用。 在morsel-driven那篇paper 中,介绍了HyPer的整个执行框架,会以task为单位处理一个morsel的数据,而执行的处理逻辑(一个pipeline job)就被编译为一个函数。这篇paper则具体讲如何实现动态编译。
442 0
Efficiently Compiling Efficient Query Plans for Modern Hardware 论文解读