Skip to content

What is the difference between Threads and Processes

Threads and Processes are two fundamental concepts in operating systems and computer science. They are used to execute multiple tasks concurrently.

Process

A process is an instance of a program that is being executed. It is an independent entity that runs in its own memory space. Each process has its own address space, file descriptors, and other resources.

Characteristics of a Process

Example

const { fork } = require('child_process');

const child = fork('./child.js');
child.on('message', (message) => {
    console.log(`Received message: ${message}`);
});

Thread

A thread is a lightweight process that shares the same memory space as the parent process. Threads are used to perform multiple tasks concurrently within the same process.

Characteristics of a Thread

Example

const { Worker } = require('worker_threads');

const worker = new Worker('./worker.js');
worker.on('message', (message) => {
    console.log(`Received message: ${message}`);
});

Difference between Threads and Processes

Summary

References