Processes and threads

process

  1. When you start an application, the computer creates one or more processes (open a browser, start a process, open multiple browser tabs, and start multiple processes).
  2. The CPU allocates a portion of memory to the process, which has its own address space and is independent of each other.
  3. All data/state of the application is stored in this block of memory;

Summary: Process is the basic unit of operating system [resource scheduling]

thread

Threads are the basic unit of CPU scheduling and can share all resources with other threads in the same process

contact

  1. A process contains one or more threads, but threads can only exist in one process
  2. Different threads in the same process share space resources (memory space) in the same process.

The difference between

  1. Fundamental difference:
    • Processes are operating systemsresourcesThe basic unit of dispatch
    • The thread is CPUtaskThe basic unit of scheduling execution
  2. Switching cost:
    • Processes have their own separate data Spaces, between programsHigh switching overhead;
    • Threads also have their own run stacks and program counters between threadsLow switching overhead.
  3. Sharing and synchronization:
    • Processes are independent of each other in space and resources, soShared complex, requires IPC (interprocess communication), butSynchronization is simple
    • Threads share the resources of their own processes, soSharing simple, butSynchronization of complex, need to use lock and other measures.

The reason for designing threads

There are two important concepts in operating systems: concurrency and isolation

  • Concurrency: Improves hardware utilization. Context switching of processes is less efficient than context switching of threads, so threads can improve the efficiency of concurrency
  • Isolation: The resources of the computer are shared. When a program crashes, it is necessary to ensure that these resources are recycled. The resources of the process are independent and the crash does not affect the progress of other programs

The process itself can do isolation, but threads are needed to do concurrency (for example, when the browser encounters setTimeout, let the timer thread do it instead of the renderer thread) because the overhead of switching between threads is lower than that of switching between processes

How to resolve conflicts caused by threads sharing resources?

  • The mutex: Prevents multiple threads from reading and writing to a memory area at the same time (this applies if a process contains one thread)

For example: a workshop of one person, the first to lock the door, the last to see the lock, queuing at the door, waiting for the lock to open before entering

  • A semaphore: Used to ensure that multiple threads do not conflict with each other (for a process containing multiple threads)

For example: a workshop of many people, put a bunch of keys at the door, enter the person to take a key, and then hang the key back to the original place. When the last person to arrive finds his keys on the rack, he knows he must wait in line at the door

Browser multi-process

The main processes of the browser and their responsibilities

  • Browser process: Main process of the browser (Responsible for coordination and control)
  1. Responsible for address bar, bookmark bar, forward and back buttons, etc
  2. Handles the browser’s invisible, low-level operations, such as network requests and file access
  3. Responsible for page management, creating and destroying other processes
  • Rendering process: Responsible for a TAB inside aboutPage renderingOf all things,Page rendering.Script execution.Event handling, etc.
  • Plug-in process: be responsible forManaging all plug-ins, such as Flash, each type of plug-in corresponds to a process that is created only when the plug-in is used
  • GPU process: processes GPU-related tasks

In Chrome, for example, each TAB page you open creates a separate browser process (with the possibility of launching other processes to help)

Advantages and disadvantages of multi-process browsers

  • Advantages:

(1) the failure of one rendering process does not affect other processes. (2) It is more secure and limits the permissions of different processes at the system level

  • Disadvantages:

Since memory is not shared between different processes, memory of different processes often needs to contain the same content. To save memory, Chrome limits the maximum number of processes, which is determined by the memory and CPU capacity of the device. When this limit is reached, newly opened tabs will share the previous rendering process of the same site.

Chrome treats the functions of different browser applications as services that can be easily split into different processes or combined into a single process.

Rendering process – multi-threaded

For the front end, the renderer is the most important process in the browser:

  1. Each TAB page corresponds to a rendering process
  2. Each rendering process is responsible for parsing, executing and rendering HTML, CSS, JS and other files, as well as event handling
  3. Each render process consists of multiple threads :(1) GUI threads; (2) JS engine thread; (3) event trigger thread; (4) Timer thread; (5) Asynchronous HTTP network request threads

1. GUI rendering thread

Responsible for page rendering, this is the main task. Not finished page rendering what the user is looking at [laughing and crying]

The specific rendering process is shown in the browser series – rendering principle and process

JavaScript engine threads

Responsible for parsing Javascript scripts and running code.

For example, DOM manipulation of the interaction between the help page and the user is the main task

The following three processes should be handled by the JS engine thread, but because the JS language is single threaded, and the following three processes are time-consuming, so the browser does not want to waste time and block the queue, so the browser “shares” these to other threads to ensure that the JS engine thread runs smoothly

Q: Why are JS engines single threaded?

Because JS itself as a browser scripting language, it operates DOM tree, CSS style tree to deal with user interaction in the page, if it is multi-threaded will be UI operation conflict. Single threads don’t.

Q: What happens when a single thread encounters an asynchronous task

  • The synchronization task is handed to the JS engine thread.
  • Asynchronous tasks are assigned to timer threads, event triggers, and asynchronous HTTP request threads

3. The timing trigger thread

The browser timing counter is not counted by the JavaScript engine because JavaScript engines are single-threaded. If the JavaScript engine is in the blocking thread state, it will affect the timing accuracy, so it is timed and triggered by a separate thread

4. The event triggers the thread

When a DOM event is triggered, the thread adds the event to the end of the queue, waiting for the JavaScript engine to process it

DOM events include input events (including keyboard events), mouse events, click events, and load events. For details, see the JavaScript HTML DOM Event Instance Reference Manual

5. Asynchronous HTTP request threads

The XMLHttpRequest request opens a new thread request in the browser, and when a state change is detected, if a callback function is set, the asynchronous thread generates a state change event and places it in the JavaScript engine’s processing queue

See the JavaScript series — Asynchronous Programming (unfinished)

Also note: different browsers have different kernel implementations, resulting in different interpretation of web syntax (JS engine threads), so the same page may render differently in different browsers with different kernel (GUI rendering threads), which throws up common “browser compatibility issues”.

Refer to the article

  • The difference between thread and process
  • Browsers multithreaded and JS single threaded
  • A simple explanation of processes and threads