深入解析 Chrome 浏览器的多进程架构:标签页是进程还是线程?(中英双语)

news/2025/2/5 0:15:21/

深入解析 Chrome 浏览器的多进程架构:标签页是进程还是线程?

1. 引言

Google Chrome 作为全球最流行的浏览器之一,以其稳定性、安全性和多任务处理能力而闻名。而其高效的表现,很大程度上归功于其独特的多进程架构(Multi-Process Architecture)

许多用户好奇:

  • 每打开一个新的标签页,Chrome 是创建了一个新的进程还是线程?
  • 为什么 Chrome 采用多进程架构,而不是单进程或多线程架构
  • 底层的进程管理机制是如何实现的?

本文将深入解析 Chrome 浏览器的多进程架构,并探讨其底层技术实现。


2. Chrome 的多进程架构

Chrome 采用的是 “多进程 + 多线程” 混合架构,即:

  • 不同标签页(Tab)通常运行在不同的进程中(但并不是每个标签页都一定是独立进程)。
  • 每个进程内部仍然可以有多个线程,如:
    • JavaScript 运行的 渲染线程
    • 处理网络请求的 网络线程
    • 处理 UI 的 主线程
    • 执行 WebAssembly 的 WebWorker 线程

2.1 Chrome 的核心进程

Chrome 主要有以下几种进程:

进程类型作用
浏览器进程(Browser Process)负责整个浏览器的管理,如 UI、标签页管理、网络请求、用户输入等。
渲染进程(Renderer Process)负责渲染 HTML、CSS、JavaScript,执行 JavaScript 代码,每个标签页通常有一个独立的渲染进程。
GPU 进程(GPU Process)负责 GPU 硬件加速,减少 CPU 计算压力。
插件进程(Plugin Process)运行 Flash、PDF 等插件,防止插件崩溃影响整个浏览器。
扩展进程(Extension Process)运行 Chrome 插件(Extensions),保证扩展的独立性。

3. Chrome 采用多进程架构的原因

为什么 Chrome 选择多进程架构,而不是传统的单进程或多线程架构?主要有以下几个原因:

3.1 可靠性(Stability)

  • 在传统单进程浏览器(如早期的 Internet Explorer)中,如果一个标签页崩溃,整个浏览器都会崩溃
  • 多进程架构使得一个标签页崩溃不会影响其他标签页,提高了稳定性。

3.2 安全性(Security)

  • Chrome 采用 沙盒机制(Sandboxing),限制渲染进程的权限,使其无法访问操作系统的关键资源,防止恶意网站利用浏览器漏洞攻击系统。
  • 如果 JavaScript 触发了恶意代码,它只能影响当前的渲染进程,而不会影响整个浏览器

3.3 性能优化(Performance)

  • 多进程架构可以更好地利用多核 CPU,提高并行处理能力。
  • 独立的 GPU 进程 负责图像渲染,提高了页面的绘制速度,减少了 CPU 负担。

3.4 资源隔离(Resource Isolation)

  • 每个渲染进程都有独立的内存空间,防止一个页面的内存泄漏影响其他页面。
  • 传统单进程浏览器中,如果某个网站占用大量内存,整个浏览器可能会卡顿甚至崩溃,而 Chrome 采用多进程可以防止这种情况。

4. Chrome 是如何管理进程的?

Chrome 的进程管理由 Site Isolation(站点隔离)策略 以及 进程模型 控制。

4.1 进程管理策略

Chrome 采用了多种进程管理策略,主要包括:

  1. 每个站点(Site)一个进程(Site Isolation)
    • 不同的站点运行在不同的渲染进程中,防止跨站点攻击。
    • 例如:
      打开 https://example.com → 运行在一个渲染进程
      打开 https://google.com → 运行在另一个渲染进程
      
  2. 同一站点的多个标签页可以共享进程
    • 例如,用户打开多个 https://example.com 的页面时,它们可能共享同一个渲染进程,减少内存占用。
  3. 后台标签页可能会被“冻结”
    • Chrome 可能会暂停不活跃的标签页进程,以减少 CPU 和内存消耗。

5. clone() 在 Chrome 进程管理中的应用

Chrome 采用 Linux clone() 系统调用 来创建新进程,而不是传统的 fork()

可以参考笔者的另一篇博文:深入解析 clone():高效的进程与线程创建方法(中英双语)

5.1 为什么 Chrome 使用 clone() 代替 fork()

  • clone() 允许 创建共享资源的进程,而 fork() 复制整个地址空间,开销较大。
  • clone() 允许 灵活地选择共享哪些资源(如内存、文件描述符、信号处理等)。

5.2 Chrome 创建进程的底层实现

当 Chrome 需要创建一个新的渲染进程时,它会调用 clone()

#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#define STACK_SIZE 1024 * 1024  // 1MB stackint child_func(void *arg) {printf("Chrome Renderer Process: PID = %d\n", getpid());return 0;
}int main() {char *stack = malloc(STACK_SIZE);if (!stack) {perror("malloc");exit(EXIT_FAILURE);}pid_t pid = clone(child_func, stack + STACK_SIZE, CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD, NULL);if (pid == -1) {perror("clone");exit(EXIT_FAILURE);}printf("Chrome Main Process: PID = %d, Renderer PID = %d\n", getpid(), pid);wait(NULL);free(stack);return 0;
}

解释:

  • CLONE_VM 让子进程共享内存(避免 fork() 复制数据)。
  • CLONE_FS 共享文件系统信息(减少 I/O 资源)。
  • CLONE_FILES 共享文件描述符(如 sockets)。
  • CLONE_SIGHAND 共享信号处理机制

6. 结论

🚀 Chrome 采用"多进程 + 多线程"架构,每个标签页通常是一个独立的渲染进程。
🚀 多进程架构提高了浏览器的稳定性、安全性和性能。
🚀 Chrome 使用 clone() 来创建渲染进程,而不是 fork(),以减少进程创建的开销。
🚀 理解 Chrome 的进程管理,有助于深入掌握 Linux 进程模型和现代软件架构

How Chrome Manages Processes: Does Each Tab Create a New Process or Thread?

1. Introduction

Google Chrome, the world’s most popular web browser, is known for its stability, security, and efficient multitasking capabilities. A key reason behind its performance is its multi-process architecture.

Many users wonder:

  • Does opening a new tab in Chrome create a new process or a thread?
  • Why does Chrome use a multi-process architecture instead of a single-process or multi-threaded approach?
  • How does Chrome manage its processes at a low level?

In this article, we will explore Chrome’s multi-process architecture and its underlying technical implementation.


2. Chrome’s Multi-Process Architecture

Chrome uses a hybrid model that combines multiple processes and multiple threads:

  • Different tabs (webpages) usually run in separate processes, but not always.
  • Each process contains multiple threads, such as:
    • Rendering thread for JavaScript execution
    • Network thread for handling network requests
    • UI thread for managing user interactions
    • WebWorker threads for parallel execution of JavaScript tasks

2.1 Core Process Types in Chrome

Process TypeFunction
Browser ProcessManages the UI, network, tab lifecycle, and user interactions.
Renderer ProcessRenders HTML, CSS, executes JavaScript, and handles page rendering.
GPU ProcessHandles GPU acceleration to improve rendering performance.
Plugin ProcessRuns browser plugins like Flash or PDF viewers, ensuring stability.
Extension ProcessRuns Chrome extensions in a separate process to isolate them from the browser core.

3. Why Chrome Uses a Multi-Process Architecture

Why did Chrome adopt a multi-process model instead of the traditional single-process or multi-threaded model? There are several key reasons:

3.1 Stability

  • In single-process browsers (like early Internet Explorer versions), a crash in one tab could crash the entire browser.
  • Multi-process architecture ensures that a crash in one tab doesn’t affect the rest of the browser, making Chrome more stable.

3.2 Security (Sandboxing)

  • Chrome uses a sandbox model, which restricts renderer processes from accessing critical system resources.
  • If a website contains malicious JavaScript, it only affects the isolated renderer process and cannot compromise the whole system.

3.3 Performance Optimization

  • Multi-process architecture utilizes multi-core CPUs more effectively, enabling parallel execution of tasks.
  • A dedicated GPU process handles rendering, reducing the CPU’s workload and improving page rendering speed.

3.4 Resource Isolation

  • Each renderer process has its own memory space, preventing memory leaks in one tab from affecting others.
  • In a single-process browser, if one webpage consumes excessive memory, it can cause the entire browser to slow down or crash. Chrome’s multi-process model prevents this issue.

4. How Chrome Manages Processes

Chrome’s process management is controlled by Site Isolation policies and process models.

4.1 Process Management Strategies

Chrome uses various strategies for managing processes:

  1. “One Site, One Process” (Site Isolation)

    • Different sites (origins) run in separate processes to prevent cross-site attacks.
    • Example:
      Open https://example.com → runs in one renderer process
      Open https://google.com → runs in a separate renderer process
      
  2. Multiple Tabs of the Same Site May Share a Process

    • If you open multiple pages from https://example.com, they may share the same renderer process to reduce memory usage.
  3. Background Tabs May Be “Frozen”

    • Chrome can suspend inactive tabs to save CPU and memory.

5. How Chrome Uses clone() Instead of fork()

Chrome does not use fork() to create new processes. Instead, it relies on Linux’s clone() system call, which allows greater control over shared resources.

5.1 Why Chrome Uses clone() Instead of fork()

  • clone() allows processes to share resources, whereas fork() duplicates the entire address space, causing overhead.
  • clone() enables fine-grained control over memory, file descriptors, and signal handling.

5.2 Low-Level Implementation of Chrome Process Creation

When Chrome needs to create a new renderer process, it calls clone() instead of fork():

#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#define STACK_SIZE 1024 * 1024  // 1MB stackint child_func(void *arg) {printf("Chrome Renderer Process: PID = %d\n", getpid());return 0;
}int main() {char *stack = malloc(STACK_SIZE);if (!stack) {perror("malloc");exit(EXIT_FAILURE);}pid_t pid = clone(child_func, stack + STACK_SIZE, CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD, NULL);if (pid == -1) {perror("clone");exit(EXIT_FAILURE);}printf("Chrome Main Process: PID = %d, Renderer PID = %d\n", getpid(), pid);wait(NULL);free(stack);return 0;
}

Explanation:

  • CLONE_VM: Shares memory between parent and child (avoiding memory duplication).
  • CLONE_FS: Shares file system information (reducing I/O overhead).
  • CLONE_FILES: Shares file descriptors (e.g., sockets).
  • CLONE_SIGHAND: Shares signal handling.

6. Conclusion

🚀 Chrome uses a “multi-process + multi-thread” architecture, where each tab is usually a separate renderer process.
🚀 The multi-process model improves stability, security, and performance.
🚀 Chrome uses clone() instead of fork() to efficiently create new processes, reducing overhead.
🚀 Understanding Chrome’s process management provides deeper insights into Linux process models and modern software architectures!

后记

2025年2月3日于山东日照。在GPT4o大模型辅助下完成。


http://www.ppmy.cn/news/1569367.html

相关文章

Java 9模块开发:IntelliJ IDEA实战指南

在Java 9中&#xff0c;模块化是一个重要的特性&#xff0c;它可以帮助我们更好地组织和管理代码。而IntelliJ IDEA作为一个强大的集成开发环境&#xff0c;为Java 9模块的开发提供了全面的支持。本文将通过一个实际的项目示例&#xff0c;详细讲解如何在IntelliJ IDEA中开发和…

TypeScript (TS) 和 JavaScript (JS)

TypeScript (TS) 和 JavaScript (JS) 的区别主要在于 TypeScript 是 JavaScript 的一个超集&#xff0c;它在 JavaScript 基础上增加了类型系统和一些高级功能。让我们详细看看两者的区别和关系&#xff1a; 类型系统&#xff1a; TypeScript 最大的特点是 静态类型。在 TypeSc…

Deep Sleep 96小时:一场没有硝烟的科技保卫战

2025年1月28日凌晨3点&#xff0c;当大多数人还沉浸在梦乡时&#xff0c;一场没有硝烟的战争悄然打响。代号“Deep Sleep”的服务器突遭海量数据洪流冲击&#xff0c;警报声响彻机房&#xff0c;一场针对中国关键信息基础设施的网络攻击来势汹汹&#xff01; 面对美国发起的这场…

2025年2月2日(tcp3次握手4次挥手)

TCP&#xff08;三次握手和四次挥手&#xff09;是建立和关闭网络连接的标准过程&#xff0c;确保数据在传输过程中可靠无误。下面是详细解释&#xff1a; 1. 三次握手&#xff08;TCP连接建立过程&#xff09; 三次握手是为了在客户端和服务器之间建立一个可靠的连接&#x…

《从0到1:用朴素贝叶斯算法搭建垃圾邮件检测系统》

在数字信息爆炸的时代&#xff0c;电子邮箱成为我们工作和生活中不可或缺的沟通工具。但随之而来的是大量垃圾邮件的困扰&#xff0c;它们不仅占用存储空间&#xff0c;还可能隐藏着诈骗信息&#xff0c;浪费我们的时间和精力。今天&#xff0c;就让我们一起探索如何利用朴素贝…

99.20 金融难点通俗解释:中药配方比喻马科维茨资产组合模型(MPT)

目录 0. 承前1. 核心知识点拆解2. 中药搭配比喻方案分析2.1 比喻的合理性 3. 通俗易懂的解释3.1 以中药房为例3.2 配方原理 4. 实际应用举例4.1 基础配方示例4.2 效果说明 5. 注意事项5.1 个性化配置5.2 定期调整 6. 总结7. 代码实现 0. 承前 本文主旨&#xff1a; 本文通过中…

使用C# 如何获取本机连接的WIFI名称[C# ---1]

前言 楼主最近在写一个WLAN上位机&#xff0c;遇到了使用C#查询SSID 的问题。CSDN上很多文章都比较老了&#xff0c;而且代码过于复杂。楼主自己想了一个使用CMD来获得SSID的方法 C#本身是没有获得WINDOWS网路信息的能力&#xff0c;必须要用系统API&#xff0c;WMI什么的&…

IP服务模型

1. IP数据报 IP数据报中除了包含需要传输的数据外&#xff0c;还包括目标终端的IP地址和发送终端的IP地址。 数据报通过网络从一台路由器跳到另一台路由器&#xff0c;一路从IP源地址传递到IP目标地址。每个路由器都包含一个转发表&#xff0c;该表告诉它在匹配到特定目标地址…