Threads or Processes?

What is a process?

In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.

What is a thread?

In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. It generally results from a fork of a computer program into two or more concurrently running tasks. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process.

In short, a thread is a sub-process.

As an analogy, consider a company with three different processes or tasks.

Process 1 is like an HR head. It spawns a thread of execution (Thread 1.1) in HR clerk and another (Thread 1.2) in HR assistant.

Process 2 is like an Accounting head. It spawns a thread (Thread 2.1) in an Accounting encoder and another (Thread 2.2) in Accounting clerk, then another (Thread 2.3) in Accounting assistant.

Process 3 is like a Payroll head. It spawns a thread (Thread 3.1) in Payroll clerk, and another (Thread 3.2) in Payroll encoder.

Each thread is only associated with its process. That is, a thread from Process 1 does not know anything about Process 3. Threads communicate through shared-memory constructs like mutexes, semaphores and the like. The challenges?

  1. ensuring the correct sequencing of the interactions and
  2. coordinating access to resources

When processes need to communicate with other processes, they do so using IPC (inter-process communication). One scalable approach is a shared-nothing method called message passing.

So, back to our analogy.

Note that each department head is a process. A process can be used interchangeably with a program. A process or a program has its own set of instructions to execute. A department head (or a process in our analogy) has his own assistants and clerks which act like threads. If one department (process) is just a single-thread program, it would have only one person doing all the tasks in a serial fashion!

But in reality, the three departments mentioned above do their own work in parallel at the same time, so let’s fine-tune our example.

Assuming, for illustration’s sake, that each department has only one computer to do their work.

As you can imagine, the single computer is the bottleneck. No matter how efficient your head (process) and assistants (threads), they are going to be in line just to get their turn to the computer.

To do parallel work, you just add one computer per department. That is like one processor (computer) per department (process). To accommodate the clerks and assistants (threads) in parallel, you have to add two single-core computers (spawn two instances of single-thread process) or one dual-core computer (assuming it’s a multi-threaded process).

(Note that multitasking is not parallel computing. In the early days of single CPU, multitasking gives the illusion of parallel task execution through scheduling)

From a process’ point of view, a core is another processor.

With Intel HyperThreading, a process being executed in a core can have 2 threads.  To Windows, a core capable of executing a process with two threads is seen as two processors.

But you don’t have to think in multiple threads.

With multi-cores, you can write your single-thread program, execute multiple instances of it, and it will run on as much cores as you have on a computer. No mutex, no semaphores, nothing of the complexity associated with multi-threaded or concurrent programming!

It’s just a clean single-thread program. Small, independent, self-sufficient, shared-nothing architecture.

That is how you write scalable programs (the very goal of Node.js)

Of  course, that doesn’t preclude you to write multi-threaded programs if you know what you are doing.


One Comment Add yours

  1. Aubrie says:

    I guess you may want to place a facebook button to your site. Just bookmarked this article, but I must complete this by hand. Simply my 2 cents.

    My site:
    DSL Vergleich

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s