Course contentsShow
← Node.js
Lesson 5 of 1,9801. Node.js Foundations and Runtime InternalsFree lesson

System Calls and Hardware Interrupts

Examine how Node.js communicates with hardware through low-level system calls.

In this lesson, you will learn how Node.js reaches past its own code to interact with your computer's physical hardware.

The Bridge: System Calls

Node.js is just a program running in user space, which is a "restricted zone." It doesn't have permission to directly touch your hard drive, network card, or keyboard. When your code needs to do something like read a file or send a packet, it must request help from the Operating System (OS).

This request is called a System Call (syscall). Think of it like a waiter in a restaurant: your Node.js code is a customer sitting at a table. The customer cannot walk into the kitchen and start cooking (because that would be dangerous and chaotic). Instead, the customer places an order (the syscall) with the waiter (the OS kernel). The kernel is the only one authorized to go into the kitchen and talk to the "hardware" chefs.

The Wake-Up Call: Hardware Interrupts

When the hardware finishes the job—like reading data from a disk—it needs a way to tell the OS it’s done. It does this via a Hardware Interrupt.

Imagine the kitchen finishes cooking your meal. The chef rings a bell, causing the waiter to stop whatever else they are doing and immediately pay attention. In your computer, when the hardware fires an interrupt signal, the CPU pauses its current task to handle the signal. The OS then captures that data and passes it back to Node.js.

A Quick Illustration

When you use fs.readFile in Node.js, the following happens:

  1. Syscall: Node.js asks the OS to "please fetch this file."
  2. Waiting: The CPU moves on to other tasks while the hardware works.
  3. Interrupt: The disk completes the read and sends a hardware signal.
  4. Completion: The OS notifies Node.js, and your callback function is finally triggered.

This cycle allows Node.js to be incredibly efficient; it doesn't stand around waiting for slow hardware; it just makes a request and waits for the interrupt to tell it when the data is ready.

Key Takeaway: Node.js relies on system calls to request hardware actions from the OS, and hardware interrupts to receive notification that those tasks are complete.