线程与进程

Posted 写代码的运维妞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程与进程相关的知识,希望对你有一定的参考价值。

什么是线程?

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

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.

每个进程的启动都是从一个线程开始的,这个线程被称为主线程。进程可以从一个线程中创建多个线程。

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.

一个进程有多个线程

 

进程的优点:

  • 多道编程,允许多个程序同时加载到内存,实现多进程的并发执行,用户感觉自己独享CPU

进程的缺点:

  • 在进程内部,只能在一个时间干一件事。

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

 

什么是线程?

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

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.

 

 

线程与进程的区别:

  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.

  对主线程的修改可能影响其所在进程的其他线程的行为。对父进程的修改不能影响子进程

以上是关于线程与进程的主要内容,如果未能解决你的问题,请参考以下文章

Android中线程与线程,进程与进程之间如何通信?

JAVA线程与线程进程与进程间通信

OS——进程与线程

OS——进程与线程

OS——进程与线程

OS——进程与线程