zoukankan      html  css  js  c++  java
  • 网络编程基础【day10】:进程与线程介绍(一 )

    本节内容

    1、概述

    2、什么是进程?

    3、什么是线程?

    4、什么是携程?

    5、存在的疑问

    6、总结

    一、概述

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

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

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

    二、什么是进程(Process)?

    2.1、英文解释

    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.2、进程概念

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

    在多道编程中,我们允许多个程序同时加载到内存中,在操作系统的调度下,可以实现并发地执行。这是这样的设计,大大提高了CPU的利用率。进程的出现让每个用户感觉到自己独享CPU,因此,进程就是为了在CPU上实现多道编程而提出的。

    2.3、有了进程为什么还要线程?

    进程有很多优点,它提供了多道编程,让我们感觉我们每个人都拥有自己的CPU和其他资源,可以提高计算机的利用率。很多人就不理解了,既然进程这么优秀,为什么还要线程呢?其实,仔细观察就会发现进程还是有很多缺陷的,主要体现在两点上:

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

    • 进程在执行的过程中如果阻塞,例如等待输入,整个进程就会挂起,即使进程中有些工作不依赖于输入的数据,也将无法执行。

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

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

    三、什么是线程(Thead) ?

    3.1、线程概念

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

    3.2、英文解释

    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.1、英文解释

    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.2、中文解释

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

    五、存在的疑问

    5.1、进程和线程那个运行快?

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

    5.2、进程和线程那个启动快?

    答案是:线程快。因为进程相当于在修一个屋子。线程只是一下把一个来过来就行了。进程是一堆资源的集合。它要去内存里面申请空间,它要各种各样的东西去跟OS去申请。但是启动起来一运行,它俩是一样的,因为进程也是通过线程来运行的。

    六、总结

    1. 线程是操作系统最小的调度单位,是一串指令的集合。
    2. 进程要操作CPU,必须先创建一个线程。
    3. 进程本身是不可以执行的,操作cpu是通过线程实现的,因为它是一堆执行,而进程是不具备执行概念的。就像一个屋子,屋子就是进程,但是屋子里面的每一个人就是线程,屋子就是内存空间。
    4. 单核CPU只能同时干一件事,但是为什么给我们的感觉是在干了很多件事?因为上线的切换,刚才也说了跟读书那个例子一样。因为CPU太快了,可以有N多次切换,其实它都是在排着队呐。
    5. 寄存器是存上下文关系的。
    6. 进程是通过PID来区分的,并不是通过进程名来区分的。进程里面的第一个线程就是主线程,父线程和子线程是相互独立的,只是父线程创建了子线程,父线程down了,子线程不会受到影响的。
    7. 主线程修改影响其他线程的行文,因为它们是共享数据的。
  • 相关阅读:
    初赛—算法复杂度
    2-SAT
    最小生成树
    18-短信验证码接口
    17-腾讯云短信开发
    16-手机号是否存在验证接口
    15-多方式登录
    14-登录注册页面
    13-Git
    12-跨域请求详解
  • 原文地址:https://www.cnblogs.com/luoahong/p/9897013.html
Copyright © 2011-2022 走看看