进程:资源分配(cpu time and memory)的基本的单位.
线程:程序执行的基本单位。
进程: 拥有独立的内存单元,多进程之间要用IPC通信
线程:同一个进程的多个线程共享内存,多于一些要求同时进行并且又要求共享某些变量的并发操作,最好用线程。
进程:切换耗费资源大,效率差
线程:切换所需时间远小于进程
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。