线程是?
线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务
A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.
线程是执行的上下文,是CPU执行命令流所需要的所有信息
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. Many tasks can share a CPU.
CPU给你的错觉是他同时在运行多个运算,他通过在每次计算上花费一点时间来做到,他可以这样做因为每个计算都有执行的上下文,许多任务可以共享CPU(但不是同时,因为计算快,所以感觉是同时计算)
On a more technical level, an execution context (therefore a thread) consists of the values of the CPU's registers
在技术层面来讲,线程是有CPU寄存器的值组成的
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
线程跟进程不同,线程是一段执行的上下文,而进程是一堆计算机相关资源的整合,一个进程有一个或多个线程
进程是?
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, 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
每一个进程都提供执行这个程序所需的资源,一个进程具有虚拟地址空间,可执行代码,打开系统对象的句柄,一个安全的上下文,唯一的进程标识符(win下可在资源管理器中查看,系统通过标识符来识别进程,并不是通过名字),环境变量,优先级类别,最小最大工作空间(通过大小来分配内存),并至少有一个在执行的线程,通常称作主线程,但可以从任何线程创建其他线程
程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程。程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本;进程是程序的一次执行活动,属于动态概念
他俩啥区别?
这里不是直译,加了一些自己的理解
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
对进程的修改(取消,修改优先等)可能会影响进程下的线程的行为,而修改父进程不会影响子进程
转载请注明出处
python自学技术互助扣扣群:670402334
网友评论