The Internals of Node.js

Part 2. Process and the Threads

Song Cho
5 min readJun 14, 2019

“The Basics of Process and Threads”

Before going straight into the libuv, we’re going to do a very brief detour and discuss something called threads. Once we understand what a thread is we’ll then move on to the next topic event loop.

A process is an instance of a computer program that has been executed. Within a single process, we can have multiple things called threads.

You can picture a thread as a to-do list that has some number of instructions that need to be executed by the CPU of your computer. This thread is given to the CPU and the CPU you will attempt to run every instruction on it one by one. Starting at the top and then going down.

Let me show you, my activity monitor. It lists out all the different processes that are running on my computer. If you look at the bottom right over here, I’ve got about 327 different processes and 1761 different threads running right now. Every one of these threads belongs to one individual process.

“Scheduler decides which treads should be processed”

One of the most important areas of study around threads is understanding something called scheduling. Scheduling is the ability to decide which thread to process at any given instant in time. Let’s figure out what that means by example.

Each one of these threads right here might have some urgent responsibility assigned to it. We might have a thread that is responsible for making sure that whenever the user moves their mouse, the cursor on the screen moves as well. Or we might have another thread whose job is to make sure that keyboard typing shows up inside of text input whenever the user types.

The scheduler has to look at all these different threads that are asking to be processed and figure out how to do some amount of work on each of them while making sure that they don’t have to wait too long to be processed.

Important! We want to make sure that urgent threads don’t have to wait too long to be executed. So for example, if the thread in charge of moving the mouse around the screen has to wait like five seconds before it gets processed, the cursor will not move during that time and the user is going to think that their computer is broken!

“Scheduler’s Strategy”

There are a couple of different strategies that are used to improve the rate at which these threads can be processed.

1. Adding more CPU cores

The easiest way to quickly process threads or process more at one given time is by adding in additional CPU cores to our machine. If we have more than one core inside of our CPU then we can easily process multiple threads at the same time.

One thing I want to mention here is that technically one core can process more than one threaded time through a process called multi-threading or you may have also heard it which is referred to as hyper-threading as well.

2. Optimizing the Scheduler

The next thing we can do is more closely examine the work that is being done by each thread. Let’s assume that we have two threads that are both competing for processing power.

Thread #1 has a set of instructions that want to read a file off the hard drive (HD) and then maybe count the number of characters inside of it and whereas thread #2 wants to multiply 18 by 28.

Any time our computer tries to read data off of an HD, we refer to that as input or output (I/O) operations and it always takes some non zero amount of time. So during this I/O phase, your CPU is waiting on the HD to read some file and return its contents. And during that time this thread #1 has absolutely nothing else to do.

Scheduler has the ability to detect this downtime or this kind of pause between instructions and it can decide to put thread #1 on pause and then execute thread # 2. As soon as thread #2 is complete it can go back over to thread #1 and continue waiting on the hard drive to be read from the person to the file to be read from the hard drive.

OK! quick recap here. In this section we spoke about some of the basics of threads. And now let’s move on to the next section, “event loop”.

Recap

  • Threads are units of instruction that are waiting to be executed by the CPU. Deciding which order to execute these threads is referred to as scheduling.
  • Scheduling is controlled by your operating system.
  • Two ways of improving the rate at which we process threads is to either 1. add more CPU cores to our machine 2. allow our OS scheduler to detect big pauses in processing time due to expensive operations.

Reference

--

--