> For the complete documentation index, see [llms.txt](https://wtifs.gitbook.io/diva-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wtifs.gitbook.io/diva-notes/os/jin-cheng-vs-xian-cheng.md).

# 进程VS线程

## 相同

本质上是同一个东西，都是是程序的环境、上下文（Contenxt of Excution, COE）。如 CPU状态（寄存器等），内存状态（页表等），通信状态（打开的文件描述符 `fd`、信号处理器等）。

在 `Linux` 的实现上，进程和线程都是用同一个结构体 `task_struct` 来描述的。 进程的 `fork`、`vfork`，线程的 `pthread_create`，底层也都是调用的 `clone`，只是传入的参数不同。

## 不同

最大的不同是粒度。进程是 **资源分配** 的最小单位，线程是 **CPU调度** 的最小单位。

可以把这个设计理解为一个分层结构：进程是上层，线程是下层，一对多的关系。

做个简单的比喻：进程=火车，线程=车厢

* 一个进程可以包含多个线程；而线程无法独立运行，必须在进程下进行
  * 一辆火车可以有多个车厢
  * 单纯的车厢无法运行
* 不同进程间数据很难共享，需要通过管道等进程间通信的方式；同一进程下不同线程间共享内存，数据比较容易共享
  * 一辆火车上的乘客很难换到另外一辆火车，比如站点换乘
  * A车厢换到B车厢很容易
* 进程要比线程消耗更多的计算机资源
  * 采用多列火车相比多个车厢更耗资源
* 进程间不会相互影响，一个线程挂掉可能导致整个进程挂掉
  * 操作系统发现线程错误可能直接kill掉进程；一个挂掉的线程写入的错误数据也可能导致其他线程挂掉
  * 一列火车不会影响到另外一列火车，但是如果一列火车上中间的一节车厢着火了，将影响到所有车厢
* 进程可以拓展到多机，进程最多适合多核
  * 不同火车可以开在多个轨道上，同一火车的车厢不能在行进的不同的轨道上

## 线程 VS 协程

协程轻量，栈空间只需2KB

协程在用户态调度，无需进入内核态，调度开销更小

## 问题

**为什么 nginx 用多进程而不是多线程架构？**

* 多线程，那不就成了 `Apache` 了么，用 `Apache` 就可以了，不需要再研发 `Nginx`
* 即便并发量很高，也只有固定数量进程处理，资源占用少。
* 高可用，`master` 进程会监控各 `slave` 进程状态，在 `slave` 异常时对其进行重启

#### 参考

> [biaodianfu - 线程和进程的区别是什么？](https://www.zhihu.com/question/25532384/answer/411179772)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wtifs.gitbook.io/diva-notes/os/jin-cheng-vs-xian-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
