一、概述编程
二、什么是进程?安全
三、什么是线程?网络
四、什么是携程?并发
五、存在的疑问socket
六、总结ide
咱们知道,全部的指令的操做都是有CPU来负责的,cpu是来负责运算的。OS(操做系统) 调度cpu的最小单位就是线程。程序启动后,从内存中分一块空间,把数据临时存在内存中,由于内存比较快,内存比磁盘快,而CPU又比内存还要快不少。进程以前的的内存是不能访问的,默认是要隔离的。每个程序的内存是独立的,互相是不能访问的。ui
进程:是以一个总体的形式暴露给操做系统管理,里面包含对各类资源的调用,内存的管理,网络接口的调用等等。。。。对各类资源管理的集合,就能够成为进程。spa
线程:是操做系统的最小的调度单位,是遗传指令的集合。操作系统
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.
程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程。程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本;进程是程序的一次执行活动,属于动态概念。
在多道编程中,咱们容许多个程序同时加载到内存中,在操做系统的调度下,能够实现并发地执行。这是这样的设计,大大提升了CPU的利用率。进程的出现让每一个用户感受到本身独享CPU,所以,进程就是为了在CPU上实现多道编程而提出的。
进程有不少优势,它提供了多道编程,让咱们感受咱们每一个人都拥有本身的CPU和其余资源,能够提升计算机的利用率。不少人就不理解了,既然进程这么优秀,为何还要线程呢?其实,仔细观察就会发现进程仍是有不少缺陷的,主要体如今两点上:
进程只能在一个时间干一件事,若是想同时干两件事或多件事,进程就无能为力了。
进程在执行的过程当中若是阻塞,例如等待输入,整个进程就会挂起,即便进程中有些工做不依赖于输入的数据,也将没法执行。
例如,咱们在使用qq聊天, qq作为一个独立进程若是同一时间只能干一件事,那他如何实如今同一时刻 即能监听键盘输入、又能监听其它人给你发的消息、同时还能把别人发的消息显示在屏幕上呢?你会说,操做系统不是有分时么?但个人亲,分时是指在不一样进程间的分时呀, 即操做系统处理一会你的qq任务,又切换到word文档任务上了,每一个cpu时间片分给你的qq程序时,你的qq仍是只能同时干一件事呀。
再直白一点, 一个操做系统就像是一个工厂,工厂里面有不少个生产车间,不一样的车间生产不一样的产品,每一个车间就至关于一个进程,且你的工厂又穷,供电不足,同一时间只能给一个车间供电,为了能让全部车间都能同时生产,你的工厂的电工只能给不一样的车间分时供电,可是轮到你的qq车间时,发现只有一个干活的工人,结果生产效率极低,为了解决这个问题,应该怎么办呢?。。。。没错,你确定想到了,就是多加几个工人,让几我的工人并行工做,这每一个工人,就是线程!
线程是操做系统可以进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运做单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中能够并发多个线程,每条线程并行执行不一样的任务。
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).
注意了,它俩是没有可比性的,线程是寄生在进程中的,你问它俩谁快。说白了,就是问在问两个线程谁快。由于进程只是资源的集合,进程也是要起一个线程的,它俩没有可比性。
答案是:线程快。由于进程至关于在修一个屋子。线程只是一下把一个来过来就好了。进程是一堆资源的集合。它要去内存里面申请空间,它要各类各样的东西去跟OS去申请。可是启动起来一运行,它俩是同样的,由于进程也是经过线程来运行的。