How web browsers use process & Threads
What are really Process and Thread.
What is a Process.
A Process is an active program that is under execution in computer CPU.
What is a Thread.
Thread is an execution unit that is a part of a process. A process can have multiple threads those are executing at the same time.A thread is light weight and can be managed independently by a scheduler.
There three different ways in threads,
- Kernel-level threads
- User-level threads
- Hybrid threads
Why multiple processes in browsers
In the days when most current browsers were designed, web pages were simple and had little or no active code in them. It made sense for the browser to render all the pages you visited in the same process, to keep resource usage low.
On top of this, the parts of the browser that render HTML, JavaScript, and CSS have become extraordinarily complex over time. These rendering engines frequently have bugs as they continue to evolve, and some of these bugs may cause the rendering engine to occasionally crash.
Consider about Google Chrome,
Chrome has multi process architecture and each process is heavily multi threaded. The main obstacle to overcome when attempting to implement such an architecture is creating an efficient way for all of the processes to communicate. To overcome this problem developers are using named pipes system. Also Google Chrome creates three different types of processes,
- Browser Process
There’s only one browser process, which manages the tabs, windows, and “chrome” of the browser. This process also handles all interactions with the disk, network, user input, and display.
- Renderer Process
The renderer processes contain all the complex logic for handling HTML, JavaScript, CSS, images, and so on. We achieve this using the open source WebKit rendering engine, which is also used by Apple’s Safari web browser. Each renderer process is run in a sandbox, which means it has almost no direct access to your disk, network, or display. All interactions with web apps, including user input events and screen painting, must go through the browser process.
- Plug-in Process
The browser process also creates one process for each type of plug-in that is in use, such as Flash, Quicktime, or Adobe Reader. These processes just contain the plug-ins themselves.
The threading model is, at its implemented level, quite complex. Keeping it simple, what basically happens is that there is one main process handling user interaction, the browser, operating system calls and persistent data; that being the “browser” process. This browser process contains several threads, each with different responsibilities, but the only one we need to focus on at this level is the I/O thread. The I/O thread maintains links to all spawned processes and handles all communications with other modules. Almost every time a new tab is opened in the browser, a new “Render” process is created. Occasionally
As you can see every chrome process has,
- A main thread
- An IO thread
- A few more special thread
- And a pool of general purpose thread
Also Chromium Processes take care of following tasks
The render process takes care of
- HTML parsing
- CSS parsing
- Image decoding
- JavaScript interpreter
- Regular expressions
- Layout
- Document Object Model
- Rendering
- SVG
- XML parsing
- XSLT
The browser process takes care of
- Cookie database
- History database
- Password database
- Window management
- Location bar
- Safe Browsing blacklist
- Network stack
- SSL/TLS
- Disk cache
- Download manager
- Clipboard
Consider about Firefox Browser
In older version of Firefox for Desktop, the entire browser ran within a single Process system. After Mozilla is launching a new initiative, dubbed Project Quantum, to focus on optimizing the browser engine that runs within a content process. Now a days the latest versions of Firefox browser UI and Web content running ass separate process, all browser tabs run within the same process and the browser UI runs in its own individual process. But that technology which is used in Firefox browser it helps to reduce the memory usage and power usage of computer better than Google chrome.