Process 与 Thread

这是一篇关于Modern Operating System (4th Edition)的读书笔记。它摘录和总结了作者关于Process起源和其与Thread关系的一些思考。

1. 为了整合资源

一开始,CPU只有执行完一份完整的任务代码后才能执行下一份。后来通过把CPU时间分片,人们可以让多个任务看似同时执行起来。

人们为了更好地区分这些“同时”执行的任务以及整合各自资源,提出了process这个概念。

A process is basically a program in execution… It is fundamentally a container that holds all the information needed to run a program.

每个process都有独立的:

  • address space:a list of memory locations from 0 to some maximum, which the process can read and write.
  • resource:commonly including registers (including the program counter and stack pointer), a list of related processes, and all the other information needed to run the program.

process和process之间要通过IPC (inter-process communication) 来沟通。

2. 为了提高效率

原始的process只有一个thread of control来执行任务。后来人们发现如果一个process中能够有multiple threads of control,并且让它们共享process资源并相互协作,将会大大提高效率。据此,人们提出了thread这个概念。每个thread都拥有自己stack,用来记录执行历史。

如前面提到,为了提高效率,threads之间共享process的address space和resource。由于address space共享,thread A可以几乎毫无障碍地修改thread B stack上的数据。

  1. 为什么不设置一定的保护呢?

Unlike different processes, which may be from different users and which may be hostile to one another, a process is always owned by a single user, who has presumably created multiple threads so that they can cooperate, not fight.

  1. 为什么不用multi-processes来协作呢?

…they are lighter weight than processes, they are easier (i.e., faster) to create and destroy than processes. In many systems, creating a thread goes 10-100 times faster than creating a process.

而且,process之间的资源共享和信息传递(IPC)不如thread高效(共享address space 和 resource)。

3. 总结

Process 这个模型体系由两个独立的概念组成:

  • resource grouping
  • execution

关于resource grouping:

One way of looking at a process is that it is a way to group related resources together. A process has an address space containing program text and data, as well as other resource. These resources may include open files, child processes, pending alarms, signal handlers, accounting information, and more. By putting them together in the form of a process, they can be managed more easily.

关于execution:

The other concept a process has is a thread of control, usually shortened to just thread. The thread has a program counter that keeps track of which instruction to execute next. It has registers, which hold its current working variables. It has a stack, which contains the execution history, with one frame for each procedure called but not yet returned from.

process 和 thread 虽然联系紧密,但从概念上区分的话,可以这么认为:

Processes are used to group resources together; threads are the entities scheduled for execution on the CPU.

4. Acknowledgment

上文引用皆出自于Modern Operating System (4th Edition).