网络编程基础【day10】:进程与线程介绍(一 )

本节内容

一、概述编程

二、什么是进程?安全

三、什么是线程?网络

四、什么是携程?并发

五、存在的疑问socket

六、总结ide

1、概述

  咱们知道,全部的指令的操做都是有CPU来负责的,cpu是来负责运算的。OS(操做系统) 调度cpu的最小单位就是线程。程序启动后,从内存中分一块空间,把数据临时存在内存中,由于内存比较快,内存比磁盘快,而CPU又比内存还要快不少。进程以前的的内存是不能访问的,默认是要隔离的。每个程序的内存是独立的,互相是不能访问的。ui

  进程:是以一个总体的形式暴露给操做系统管理,里面包含对各类资源的调用,内存的管理,网络接口的调用等等。。。。对各类资源管理的集合,就能够成为进程。spa

  线程:是操做系统的最小的调度单位,是遗传指令的集合。操作系统

2、什么是进程(Process)?

2.一、英文解释

An executing instance of a program is called a process.线程

每一个程序执行的实例被称为进程。

Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier,

每一个进程提供提供此程序的所须要的资源。一个进程有一个虚拟地址空间,执行代码,操做系统的系统接口,一个安全的上线文,一个惟一的进程标识符(PID),

environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can

环境变量,一个优先级类,设置最大和最小的工做空间大小,和至少一个线程在运行。每一个进程开始一单个线程,一般陈为主线程,可是能建立多个子线程。

create additional threads from any of its threads.

2.二、进程概念

程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程。程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本;进程是程序的一次执行活动,属于动态概念。

在多道编程中,咱们容许多个程序同时加载到内存中,在操做系统的调度下,能够实现并发地执行。这是这样的设计,大大提升了CPU的利用率。进程的出现让每一个用户感受到本身独享CPU,所以,进程就是为了在CPU上实现多道编程而提出的。

2.三、有了进程为何还要线程?

进程有不少优势,它提供了多道编程,让咱们感受咱们每一个人都拥有本身的CPU和其余资源,能够提升计算机的利用率。不少人就不理解了,既然进程这么优秀,为何还要线程呢?其实,仔细观察就会发现进程仍是有不少缺陷的,主要体如今两点上:

  • 进程只能在一个时间干一件事,若是想同时干两件事或多件事,进程就无能为力了。

  • 进程在执行的过程当中若是阻塞,例如等待输入,整个进程就会挂起,即便进程中有些工做不依赖于输入的数据,也将没法执行。

例如,咱们在使用qq聊天, qq作为一个独立进程若是同一时间只能干一件事,那他如何实如今同一时刻 即能监听键盘输入、又能监听其它人给你发的消息、同时还能把别人发的消息显示在屏幕上呢?你会说,操做系统不是有分时么?但个人亲,分时是指在不一样进程间的分时呀, 即操做系统处理一会你的qq任务,又切换到word文档任务上了,每一个cpu时间片分给你的qq程序时,你的qq仍是只能同时干一件事呀。

再直白一点, 一个操做系统就像是一个工厂,工厂里面有不少个生产车间,不一样的车间生产不一样的产品,每一个车间就至关于一个进程,且你的工厂又穷,供电不足,同一时间只能给一个车间供电,为了能让全部车间都能同时生产,你的工厂的电工只能给不一样的车间分时供电,可是轮到你的qq车间时,发现只有一个干活的工人,结果生产效率极低,为了解决这个问题,应该怎么办呢?。。。。没错,你确定想到了,就是多加几个工人,让几我的工人并行工做,这每一个工人,就是线程!

3、什么是线程(Thead) ?

3.一、线程概念

线程是操做系统可以进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运做单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中能够并发多个线程,每条线程并行执行不一样的任务。

3.二、英文解释

A thread(线程) is an execution(执行) context(上下文), which is all the information a CPU needs to execute a stream of instructions.(线程就是cpu执行时所须要的上下文信息指令)

Suppose(假设) you're reading a book, and you want to take a break right now, but you want to be able to come back and resume(恢复) reading from the exact point where you stopped. One way to achieve that is by jotting down(记录下来) the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.

If you have a roommate(室友), and she's using the same technique, she can take the book while you're not using it, and resume reading from where she stopped. Then you can take it back, and resume it from where you were.

Threads work in the same way. A CPU is giving you the illusion(幻觉) that it's doing multiple(多) computations(运算) at the same time. It does that by spending(花费) a bit of time on each computation. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.

On a more technical level, an execution context (therefore a thread)(一个上线文的切换就是一个线程)  consists(组合) of the values of the CPU's registers(寄存器).

Last: threads are different from processes(线程不一样于进程). A thread is a context of execution(线程是一段上下文的执行指令), while a process is a bunch(一簇) of resources(资源) associated(相关的) with a computation. A process can have one or many threads.

Clarification(澄清一下): the resources associated with a process include memory pages (all the threads in a process have the same view of the memory(全部在同一个进程里的线程是共享同一块内存空间)), file descriptors (e.g., open sockets), and security credentials (e.g., the ID of the user who started the process).

4、进程与线程的区别?

4.一、英文解释

  1. Threads share the address space of the process that created it; processes have their own address space.
  2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  4. New threads are easily created; new processes require duplication(克隆、复制) of the parent process.
  5. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
  6. Changes to the main thread (cancellation(取消), priority change(优先级的改变), etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.

4.二、中文解释

  1. 线程是共享内存空间的;进程的内存是独立的。
  2. 线程能够直接访问此进程中的数据部分;进程有他们独立拷贝本身父进程的数据部分,每一个进程是独立的
  3. 同一进程的线程之间直接交流(直接交流涉及到数据共享,信息传递);两个进程想通讯,必须经过一个中间代理来实现。
  4. 建立一个新的线程很容易;建立新的进程须要对其父进程进行一次克隆。
  5. 一个线程能够控制和操做同一进程里的其余线程;可是进程只能操做子进程。
  6. 对主线程的修改,可能会影响到进程中其余线程的修改;对于一个父进程的修改不会影响其余子进程(只要不删除父进程便可)

5、存在的疑问

5.一、进程和线程那个运行快?

注意了,它俩是没有可比性的,线程是寄生在进程中的,你问它俩谁快。说白了,就是问在问两个线程谁快。由于进程只是资源的集合,进程也是要起一个线程的,它俩没有可比性。

5.二、进程和线程那个启动快?

答案是:线程快。由于进程至关于在修一个屋子。线程只是一下把一个来过来就好了。进程是一堆资源的集合。它要去内存里面申请空间,它要各类各样的东西去跟OS去申请。可是启动起来一运行,它俩是同样的,由于进程也是经过线程来运行的。

6、总结

  1. 线程是操做系统最小的调度单位,是一串指令的集合。
  2. 进程要操做CPU,必须先建立一个线程。
  3. 进程自己是不能够执行的,操做cpu是经过线程实现的,由于它是一堆执行,而进程是不具有执行概念的。就像一个屋子,屋子就是进程,可是屋子里面的每个人就是线程,屋子就是内存空间。
  4. 单核CPU只能同时干一件事,可是为何给咱们的感受是在干了不少件事?由于上线的切换,刚才也说了跟读书那个例子同样。由于CPU太快了,能够有N屡次切换,其实它都是在排着队呐。
  5. 寄存器是存上下文关系的。
  6. 进程是经过PID来区分的,并非经过进程名来区分的。进程里面的第一个线程就是主线程,父线程和子线程是相互独立的,只是父线程建立了子线程,父线程down了,子线程不会受到影响的。
  7. 主线程修改影响其余线程的行文,由于它们是共享数据的。
相关文章
相关标签/搜索