学计算机的那个

不是我觉到、悟到,你给不了我,给了也拿不住;只有我觉到、悟到,才有可能做到,能做到的才是我的.

0%

进程和线程

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

Why

为了整合资源

一开始,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 之间要的通信要通过 IPC(inter-process communication)来实现。

为了提高效率

原始的 process 只有一个“thread of control”来执行任务,后来人们发现如果一个 process 中能够有“multiple threads of control”,让它们共享 process 资源并相互协作,将会大大提高效率。由此,人们提出了 thread 这个概念。

每个 thread 都拥有自己 stack,用来记录执行历史。

正如前面所说,为了提高效率,threads 之间共享 processaddress spaceresource。由于 address space 共享,thread A 可以几乎毫无障碍地修改 thread B 上的数据。

为什么不在 thread 之间设置一定的保护(其实在多进程场景设计时,需要考虑进程同步与死锁的问题,这里的保护指的是其他的保护措施)呢?

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.

  • 为什么不用多进程(multi-processes)而是使用多线程(multi-threads)来协作呢?

…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)

总结

process 模型体系由两个独立的概念(resource groupingexecution)组成,解释如下:

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.

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

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

线程

同一个 process 可以同时存在多个 thread
同一个 process 下的 Thread 共享 process 的资源,如内存、变量等,而不同的 process 则不能如此;

在多线程(Multi threading)中,两个线程如果同时存取或改变全局变量(Golbal Variable),则可能发生同步(Synchronization)的问题。如果线程之间互抢资源,则可能产生死锁(Dead Lock)。在编写多线程程序时,需要特别注意这两种情况。

区别与联系

进程:进程基本上是那些从就绪状态调遣出来的程序,它们被安排在CPU中执行。PCB(进程控制块)包含进程的概念。进程可以创建其他进程,这些进程称为子进程。该进程需要更多的时间来终止,它是孤立的意味着它不与任何其他进程共享内存。

线程:线程是进程的一部分,这意味着一个进程可以有多个线程,这些线程包含在一个进程中。

与进程相比,线程终止所需的时间更短,但与进程不同的是,线程不会隔离。

Process: Processes are basically the programs that are dispatched from the ready state and are scheduled in the CPU for execution. PCB(Process Control Block) holds the concept of process. A process can create other processes which are known as Child Processes. The process takes more time to terminate and it is isolated means it does not share the memory with any other process.

The process can have the following states new, ready, running, waiting, terminated, and suspended.

Thread: Thread is the segment of a process which means a process can have multiple threads and these multiple threads are contained within a process. A thread has three states: Running, Ready, and Blocked.

The thread takes less time to terminate as compared to the process but unlike the process, threads do not isolate.

总结

  • 进程:进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位.

  • 线程:线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.

线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源.

参考

Difference between Process and Thread

进程(process)和线程(thread)介绍

States of a Process in Operating Systems