进程和线程的区别

不同点:

      ①进程是执行中的一段程序,进程是资源分配的基本单位,是程序执行的一个实例,是调度运行的基本单位,是系统中并发执行的单位。

          线程是单个进程执行的每一个任务,线程是进程中执行运算的最小单元。

 

      ②一个线程只能属于一个进程,一个进程可以拥有很多线程。多线程就是允许在一个进程中同时执行多个任务。

 

      ③线程是轻量级的进程,线程给操作系统带来创建、维护、管理的代价更小,意味着线程的开销更小。

 

      ④线程没有地址空间,线程包含在进程的地址空间中。线程上下文只包含一个堆栈、一个寄存器、一个优先权,线程文本包含在其进程的文本片段中,进程拥有的所有资源都属于线程。所有的线程共享进程的内存和资源。

         同一进程中的多个线程才能共享代码段(代码和常量)、数据段(全部变量和静态变量)、扩展段(堆),但是每个线程拥有自己的栈、寄存器,用来存放所有的局部变量和临时变量。

 

      ⑤父子进程间使用进程间通信,同一进程的线程通过读取和写入变量来通信。

 

      ⑥进程内的任何线程都被看成是同位体,且处于相同级别。不管是哪个线程创建了哪个线程,进程内的任何线程都可以销毁、挂起、恢复、更改其他线程的优先权。线程也要对进程加以控制,进程中的任何线程都可以通过销毁主线程来销毁进程,销毁主线程将导致该进程的销毁,对主线程的修改可能影响所有其他线程。

 

      ⑦子进程不可以对任何其他的子进程施加控制,进程的线程可以对同一进程的其他线程施加控制。

         子进程不能对父进程施加控制,进程中的所有线程都可以对主线程施加控制。

 

相同点:   

      进程和线程都有ID/寄存器组、状态和优先权、信息块,创建后都可以更改自己的属性,都可以与父进程共享资源,都不直接访问其他无关进程或线程的资源。

 

 

 

From    《Unix网络编程》

  • 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 sp·ace 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

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值