目录
基本概念
-
进程与线程
进程是指具有某些独立功能的程序从加载、执行到结束的一次过程,是程序的一次执行。程序运行时系统会创建一个进程,并为它分配资源(CPU、内存等),然后将该进程交给进程就绪队列,直到进程调度器选中它并为它分配CPU时间,程序开始真正执行。进程是资源分配的最小单位。
进程是一个动态的概念,动态创建、执行到动态消亡。进程创建后,有自己的地址空间,包括方法区、数据区以及堆栈区。
进程的执行过程中可能具有三种基本形态:就绪状态、执行状态以及阻塞状态。如图所示
线程是程序执行的最小单位,是系统独立调度和分派的基本单位,有时也被称作轻量级的进程(Lightweight,LWP)。一个进程可以由多个线程组成,线程间共享进程的所有资源,每个线程有自己的堆栈和局部变量。
在单个进程中同时运行多个线程完成不同的工作,就是多线程。
-
进程和线程区别
|
fork is expensive. Memory is copied from the parent to the child, all descriptors are duplicated in the child, and so on. Current implementations use a technique called copy-on-write, which avoids a copy of the parent’s data space to the child until the child needs its own copy. But, regardless of this optimization, fork is expensive. IPC is required to pass information between the parent and child after the fork. Passing information from the parent to the child before the fork is easy, since the child starts with a copy of the parent’s data space and with a copy of all the parent’s descriptors. But, returning information from the child to the parent takes more work. Threads help with both problems. Threads are sometimes called lightweight processes since a thread is “lighter weight” than a process. That is, thread creation can be 10–100 times faster than process creation. All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem |
- 并行与并发
[参考文献]
1.
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



