106 Chronologically structured chapter that builds foundational Linux concepts step-by-step
Here's a chronologically structured chapter that builds foundational Linux concepts step-by-step and prepares you for FAANG-level interviews with tools, commands, and real-time scenarios.
๐ง Chapter: Mastering Linux Internals for Interviews
1. The Big Picture: How Linux Works
-
Overview: Role of the kernel, user space vs kernel space
-
Key Concept: Linux as a multitasking, multiuser, monolithic kernel system
2. CPU: The Core Executor
-
What: Executes instructions, context switching, scheduling
-
Terms: User time, system time, idle time
-
Command:
top,htop,mpstat,uptime -
Interview Insight: Explain CPU-bound vs I/O-bound processes
3. Memory: RAM and Virtual Memory
-
Concepts: Virtual memory, paging, swapping, buffers, cache
-
Commands:
free -h,vmstat,/proc/meminfo,top(RES/VIRT/SHR) -
Interview Tip: What causes high swap usage?
4. Processes and Threads
-
Process: An independent executing program (PID, PPID)
-
Thread: Lightweight process sharing the same address space
-
Commands:
ps -ef,pstree,top,htop -
System Calls:
fork(),exec(),exit(),wait() -
Key Difference:
fork()duplicates,exec()replaces,exit()terminates
5. Kernel: The Brain
-
Components: Process scheduler, memory manager, I/O manager
-
System Calls Interface: Bridge between user and kernel space
-
Command:
uname -a,dmesg -
Real World: Debugging kernel logs with
dmesg
6. I/O and Disk Subsystems
-
Concepts: Block vs character devices, buffered I/O, async I/O
-
Commands:
iostat,iotop,df -h,du -sh,lsblk,mount -
Use Case: Identify I/O bottlenecks with
iotop,pidstat -d
7. System Calls Deep Dive
-
What: Interface to kernel services (e.g., file ops, process control)
-
Examples:
read(),write(),open(),close(),kill() -
Tool:
strace— trace system calls and signals -
Interview: How
exec()works under the hood? Usestraceto show.
8. Process States and Lifecycle
-
States: Running, Sleeping, Zombie, Stopped, Orphan
-
Monitoring Tools:
-
top,htop– for real-time process view -
watch -n 1 'ps aux | grep <pid>' -
pidstat– CPU, memory, I/O usage over time -
lsof– list open files by a process
-
-
Real-World Debug: A zombie process scenario
9. Signals in Depth
-
Types: TERM, KILL, STOP, CONT, HUP, INT, etc.
-
Commands:
-
kill -SIGTERM <pid>– graceful shutdown -
kill -9 <pid>– force kill -
trapin shell scripts
-
-
Tools:
strace -p <pid>to inspect signal handling -
Real-Time Example: Releasing stuck processes via SIGKILL
10. Background & Detached Execution
-
Commands:
-
nohup command &– runs even after logout -
disown %job_id– remove from job table
-
-
Use Case: Run long jobs on remote systems safely
11. Advanced Performance Debugging
-
renice– change process priority -
pidstat– profile specific PIDs -
strace– syscall tracing -
lsof– open file/socket tracking -
dmesg– kernel ring buffer
12. Process Monitoring in Production
-
Monitor All States:
-
ps -eo pid,state,cmd -
top -H– thread view -
watch -n 1 'ps aux | grep <app>'
-
-
Real-World Case: Memory leak or CPU spike in production
-
top→strace→lsof→killorrenice
-
Let's break down and deeply explain the concepts of CPU time and the Linux scheduler using real-world analogies, command-line examples, and system internals. This will help you understand it at an interview level, especially for FAANG or senior DevOps/System Engineer roles.
๐ง Part 1: What is CPU Time?
✅ Definition:
CPU Time refers to the amount of time a CPU spends executing a specific process's instructions, excluding any time the process is idle or waiting for I/O (disk/network) operations.
๐ Breakdown:
There are typically two types of CPU time:
-
User CPU Time: Time spent executing user-space code (your application).
-
System CPU Time: Time spent in the kernel (system calls, managing files, sockets, memory).
๐ก Analogy:
Imagine the CPU as a chef in a kitchen.
-
Each process is a customer placing an order (program to run).
-
CPU time is the time the chef (CPU) actually spends cooking the dish (executing instructions) — not waiting for ingredients (I/O).
๐ Real Example:
$ time ls -l
Output:
real 0.003s
user 0.001s
sys 0.002s
-
real: Total elapsed wall-clock time (you watching). -
user: Time spent executing in user space (0.001s). -
sys: Time spent in kernel space (0.002s).
So CPU time = user + sys = 0.003s.
⚙️ Part 2: Linux Scheduler (How CPU Time is Shared)
✅ What is the Scheduler?
The Linux scheduler is a kernel component responsible for deciding which process/thread runs on the CPU and for how long.
It manages CPU time sharing to ensure:
-
Efficiency
-
Fairness (all get CPU)
-
Responsiveness (interactive processes run fast)
-
Throughput (keep CPUs busy)
๐ฆ Types of Scheduling Policies in Linux:
| Policy | Description |
|---|---|
| CFS (default) | Completely Fair Scheduler – balances CPU time fairly across processes |
SCHED_FIFO |
Real-time: first-in, first-out. No time slice, runs until it yields. |
SCHED_RR |
Real-time: Round-robin. Time slice-based rotation. |
SCHED_DEADLINE |
Guarantees deadlines for real-time tasks. |
๐ง How the CFS Scheduler Works (Deep Dive)
CFS (Completely Fair Scheduler) is the default scheduler used by modern Linux kernels.
๐ Key Concept:
Each process is assigned a virtual runtime (vruntime). The process with the lowest vruntime gets the CPU.
๐ Idea:
-
Track how long a process has used the CPU.
-
If a process has had less CPU time than others, it is prioritized next.
-
Ensures fair CPU time proportionate to process weight (nice value).
๐ ️ Tools:
You can view scheduling details using:
ps -eo pid,comm,ni,pri,cls,stat --sort=pid
-
ni– Nice value (lower = higher priority) -
pri– Kernel priority -
cls– Scheduling class (TS= CFS,FF= FIFO) -
stat– Process state
๐งฎ Example of Scheduler in Action
Imagine three processes:
-
Process A (Interactive shell)
-
Process B (Background database)
-
Process C (CPU-intensive encoding)
What scheduler does:
-
Process A: gets quick CPU bursts so shell is responsive.
-
Process B: gets occasional CPU as it's mostly waiting for I/O.
-
Process C: gets fair chunk, but not all CPU, to keep system responsive.
๐ Demo: Viewing CPU Time & Scheduling
๐งช Check CPU time per process:
ps -eo pid,etime,time,comm --sort=-time | head
| etime | Elapsed real time since the process started
| time | Total CPU time (user + system) consumed
๐จ๐ป Strace Example:
To see how system calls contribute to CPU/system time:
strace -T -p <pid>
-
-Tshows how much time each system call takes.
๐ง Interview-Level Questions
| Question | Answer |
|---|---|
| What is CPU time? | Time CPU spends executing user + system code of a process. |
| Difference between wall-clock and CPU time? | Wall-clock is total elapsed; CPU time is time the CPU actually executed your process. |
| What does the Linux scheduler do? | Decides which process/thread to run next on the CPU. |
| What is vruntime in CFS? | A measure of how much CPU time a process has had; lower values are run first. |
| What is the default scheduler in Linux? | CFS (Completely Fair Scheduler). |
๐ Summary (In a Nutshell)
| Concept | Description |
|---|---|
| CPU Time | Actual processing time used by the CPU for a process. |
| User Time | Time in user space (application code). |
| System Time | Time in kernel space (system calls). |
| Linux Scheduler | Kernel component that selects which process gets CPU time. |
| CFS | Ensures fair CPU sharing using vruntime. |
| Tools | top, ps, strace, htop, time, pidstat |
No comments :
Post a Comment