Skip to content

Chapter 2: Unix / Linux Basics — History, Kernel, Shell, Distributions

Source slides: L01_Introduction_and_Linux.pdf (slides ≈ 35–80, "Introduction to Unix/Linux"), read.pdf slides 23–41.


1. Chapter Overview

This chapter explains what Linux really is, where it comes from, and how its pieces fit together. It is the theory chapter for the rest of the course: every command, script and cluster login from this point on assumes you understand:

  • The four families: Unix, POSIX, GNU, Linux — and how they differ.
  • The two execution modes of an OS: kernel mode vs user mode.
  • The Linux kernel vs GNU/Linux distribution vs shell.
  • Why so many distributions exist and how to choose one for HPC (Ubuntu, CentOS/RHEL, Debian, Arch, openSUSE).
  • What happens between the moment you press a key and the moment a character appears on screen — through system calls.

Why it matters in HPC/CFD: clusters run a stripped-down Linux distro (often Rocky Linux or SLES); knowing the difference between kernel and user space lets you read error messages and pick the right tool. Understanding POSIX explains why your script that works on Ubuntu breaks on macOS.

What the examiner asks (very common written-exam topics):

  • "Define Unix, POSIX, GNU, Linux. State the relationship."
  • "Explain user mode vs kernel mode. Give an example of a system call."
  • "What is a Linux distribution? Name three."
  • "What is a shell?"

What you must master for top grade:

  • A clean Venn diagram of Unix / POSIX / GNU / Linux.
  • A 3-line definition of each.
  • A 4-step trace of what happens when you type ls.
  • The full names of the four standard parts of a Linux system: kernel, GNU userland, init, shell.

2. Basics from Zero

In 1969, two engineers (Ken Thompson and Dennis Ritchie) at AT&T Bell Labs built a small operating system called Unix to run on a spare DEC PDP-7. Unix was simple, multi-user and portable — the second version was rewritten in C, so it could be compiled on any computer. By 1980 every university had a Unix box.

But Unix was proprietary. Two reactions followed:

  1. POSIX (1988) — IEEE standardised the interface of Unix so that programs would be portable, even between different vendor Unixes. POSIX is a contract, not an implementation.
  2. GNU project (1983, Richard Stallman) — wrote free clones of every Unix tool (gcc, bash, coreutils, make, emacs). They had everything except the kernel.

In 1991 a Finnish student, Linus Torvalds, posted on a newsgroup: "I'm doing a (free) operating system, just a hobby". That was the Linux kernel. People combined it with GNU's userland → GNU/Linux = a complete free OS.

বাংলায়: গল্পটা চারটা নামে মনে রাখুন: Unix (১৯৭০, Bell Labs-এর আসল OS, কিন্তু proprietary), POSIX (১৯৮৮, IEEE-র standard — কোনো software নয়, একটা চুক্তি), GNU (১৯৮৩, Stallman-এর free tool-এর ভাণ্ডার — kernel ছাড়া সব), আর Linux (১৯৯১, Torvalds-এর free kernel)। GNU-র userland + Linux kernel = আজকের সম্পূর্ণ free OS। পরীক্ষায় এই চারটার সম্পর্ক ব্যাখ্যা করা প্রায় নিশ্চিত প্রশ্ন।

Today a Linux distribution = (Linux kernel) + (GNU userland) + (package manager) + (defaults). Different distributions = different defaults / package formats:

  • Debian / Ubuntu: apt, .deb, popular for desktops & cloud.
  • RHEL / CentOS / Rocky / Fedora: yum/dnf, .rpm, common in HPC/enterprise.
  • SUSE / openSUSE / SLES: zypper, .rpm, also enterprise/HPC.
  • Arch / Manjaro: rolling, pacman.
  • Gentoo: source-based.

বাংলায়: Distribution মানে kernel + GNU userland + package manager + default settings-এর একটা প্যাকেজ — মূল জিনিস সব distro-তেই এক, পার্থক্য শুধু মোড়কে। মুখস্থ রাখুন: Debian পরিবার apt/.deb চালায়, RHEL পরিবার (Rocky, CentOS) dnf/.rpm চালায় — আর HPC cluster-এ প্রায় সবসময় RHEL-পরিবারই থাকে কারণ এরা দীর্ঘদিন stable support দেয়।

Real-life analogy. Think of an OS as a restaurant.

  • Unix = the original restaurant chain (Bell Labs, 1970).
  • POSIX = the printed menu format that every chain agreed to use.
  • GNU = a free workshop that builds the kitchen utensils.
  • Linux kernel = the engine of the kitchen (oven + plumbing).
  • GNU/Linux distro = a full restaurant with kitchen + utensils + menu.
  • Shell = the waiter who takes your order.

What if you confuse them? You will write "Linux is an OS" when actually the kernel is just one piece, and the cluster uses GNU userland tools — which is what your scripts depend on.


3. Hard English Made Easy

Hard Term Simple English বাংলা Example
Operating System (OS) The software that controls the computer কম্পিউটার নিয়ন্ত্রণকারী সফটওয়্যার Linux, Windows, macOS
Kernel The core of the OS that talks to the hardware অপারেটিং সিস্টেমের মূল অংশ Linux kernel 6.x
User mode Restricted CPU mode where normal programs run সাধারণ প্রোগ্রাম চালানোর সীমিত মোড Your ls command
Kernel mode Privileged CPU mode for OS code অপারেটিং সিস্টেমের বিশেষ মোড Disk driver
System call A request from a program to the kernel প্রোগ্রাম থেকে কার্নেলকে অনুরোধ read(), write()
POSIX Standard ensuring portability across Unix-like OSes সিস্টেম মানদণ্ড macOS is POSIX-certified
GNU Free-software project providing tools ফ্রি সফটওয়্যার প্রকল্প gcc, bash, make
GPL GNU General Public License জিএনইউ লাইসেন্স bash is GPL'd
Distribution / Distro A complete Linux package for users সম্পূর্ণ লিনাক্স প্যাকেজ Ubuntu 22.04
Shell Program that interprets your commands কমান্ড পরিচালক প্রোগ্রাম bash, zsh
Userland Set of programs run by the user ইউজারের চালু করা সফটওয়্যার ls, cat, grep
Init system First process started after boot বুট হওয়ার পর প্রথম প্রসেস systemd, sysvinit
File system Way of organising files on disk ফাইল সংরক্ষণ পদ্ধতি ext4, xfs
Process A running program চলমান প্রোগ্রাম every bash opens a process
Daemon A background service ব্যাকগ্রাউন্ড সেবা sshd, cron
Tux The Linux mascot (penguin) লিনাক্সের মাসকট পেঙ্গুইন logo of kernel.org
Kernel space Memory area for kernel code কার্নেলের মেমরি এলাকা drivers run here
User space Memory area for user code ইউজারের মেমরি এলাকা your apps
Multi-user Many users at once বহু ব্যবহারকারী Linux supports this since 1971
Multi-tasking Many programs at once অনেক প্রোগ্রাম একসাথে OS schedules them

4. Deep Theory Explanation

4.1 Unix

Proprietary OS from AT&T Bell Labs (1970). Wrote V6 in C in 1973 → portable. Spawned BSD, AIX, HP-UX, Solaris, IRIX, macOS (Darwin) and the original Unix culture: everything is a file, do one thing well, write programs that work together (Unix philosophy).

4.2 POSIX

IEEE standard 1003 (1988). Defines:

  • System interfaces (function names, e.g. fork, exec, open, read, write).
  • Shell utilities with deterministic behaviour (grep, awk, sed, sh).

If two OSes are POSIX-compliant, the same C source can be compiled on both. macOS is POSIX-certified; Linux is mostly POSIX-compliant (not officially certified). Windows offers POSIX through WSL.

Examiner trap: "Is Linux POSIX-compliant?" Mostly. Not certified.

বাংলায়: POSIX কোনো software নয় — এটা IEEE-র লেখা একটা interface-এর চুক্তি: কোন function (fork, open, read) আর কোন tool (grep, sed, sh) কেমন আচরণ করবে। দুটো OS POSIX মানলে একই C code দুটোতেই compile হবে — এটাই portability। ফাঁদ-প্রশ্ন মনে রাখুন: macOS POSIX-certified, কিন্তু Linux mostly compliant, officially certified নয়।

4.3 GNU

"GNU is Not Unix" — recursive joke. Project started by Richard Stallman in 1983, Free Software Foundation 1985. Goal: a free Unix. Tools delivered:

  • bash (Bourne-Again Shell)
  • gcc, g++ (compilers)
  • coreutils (ls, cp, mv, …)
  • make, binutils, glibc, gawk, sed, grep, tar, gzip, nano, emacs.

Released under the GPL ("copyleft") — derived works must also be free.

4.4 The Linux kernel

A monolithic but modular kernel. Tasks: process scheduling, memory management, drivers, networking stack, file systems, security. Released under GPLv2. Versioning: 6.X. Configurable: uname -r shows running kernel.

বাংলায়: Kernel হলো OS-এর সেই অংশ যেটা সরাসরি hardware-এর সাথে কথা বলে — কোন process কখন CPU পাবে (scheduling), কে কতটা memory পাবে, disk/network-এর driver — সব kernel-এর কাজ। Linux kernel monolithic কিন্তু modular: driver-গুলো module হিসেবে load হয়। পরীক্ষায় kernel-এর অন্তত চারটা কাজ লিখতে পারতে হবে।

4.5 GNU/Linux: the complete OS

Linux kernel + GNU userland + init + optional X/Wayland + package manager = a distribution.

4.6 User mode vs Kernel mode (very high exam frequency)

Modern CPUs have at least two privilege rings:

Mode Ring What runs here Can do
User mode 3 Your programs Use libraries, do arithmetic, allocate memory through syscalls
Kernel mode 0 The kernel & drivers Direct hardware access, change page tables, handle interrupts

A system call is the gate between the two. When printf is called:

  1. printf (libc, user mode) formats the string.
  2. It calls write(1, buf, n) — still user mode.
  3. The CPU executes a syscall / int 0x80 / svc instruction → switches to kernel mode.
  4. Kernel writes bytes to terminal driver.
  5. Returns → user mode.

Why it matters: if your code has a bug that touches forbidden memory, it gets a segmentation fault in user mode (recoverable) — but a kernel bug crashes the whole machine ("kernel panic"). Drivers belong in kernel mode; never put your CFD solver there.

The whole layered picture, with the system-call boundary marked:

┌─────────────────────────────────────────────────────────┐
│ USER PROGRAMS     bash · ls · gcc · vim · CFD solver    │  user mode
├─────────────────────────────────────────────────────────┤  (ring 3)
│ LIBRARIES (libc)  printf() · malloc() · fopen()         │
╞══════════ SYSTEM-CALL INTERFACE — the only gate ════════╡ ◄─ syscall /
│ KERNEL            scheduler · memory management · VFS · │    int 0x80
│                   drivers · network stack · security    │  kernel mode
├─────────────────────────────────────────────────────────┤  (ring 0)
│ HARDWARE          CPU · RAM · disk · network card       │
└─────────────────────────────────────────────────────────┘
The shell is just another user program; it asks the kernel
(via fork/exec syscalls) to start the programs you type.

বাংলায়: CPU দুটো মোডে চলে: user mode (ring 3, সীমিত ক্ষমতা — আপনার সব প্রোগ্রাম এখানে) আর kernel mode (ring 0, hardware-এ পূর্ণ অধিকার)। দুই জগতের মাঝখানের একমাত্র দরজা হলো system call — printf শেষমেশ write() syscall দিয়ে kernel-এ ঢোকে। এই বিভাজনের লাভ: আপনার code-এ bug থাকলে শুধু প্রোগ্রামটা মরে (segmentation fault), পুরো মেশিন নয়। এটা এই chapter-এর সবচেয়ে বেশি জিজ্ঞেস করা প্রশ্ন।

4.7 Linux distributions

Family Package Example Common HPC use
Debian apt / .deb Ubuntu, Debian, Mint Workstations, cloud
RHEL dnf / .rpm RHEL, CentOS, Rocky, AlmaLinux, Fedora Most clusters
SUSE zypper / .rpm SLES, openSUSE Some clusters
Arch pacman Arch, Manjaro Devs
Gentoo portage Gentoo Tweakers
Custom n/a Cray Linux Env (CLE) Vendor-specific

4.8 The shell

A shell is a program that reads a command, asks the kernel to start that program, waits for it to finish, and then prompts again.

Common shells: sh, bash, dash, ksh, zsh, fish, tcsh. On Linux the default login shell is /bin/bash (or /bin/sh, often a dash or bash link). HPC clusters typically force bash.

বাংলায়: Shell নিজে kernel-এর অংশ নয় — এটা একটা সাধারণ user প্রোগ্রাম, যার কাজ হলো আপনার লেখা command পড়া, kernel-কে দিয়ে (fork + exec) প্রোগ্রামটা চালানো, শেষ হলে আবার prompt দেখানো। bash মানে Bourne-Again Shell — Stephen Bourne-এর পুরনো sh-এর GNU উত্তরসূরি। Cluster-এ প্রায় সবসময় bash-ই default, তাই এই course-এর সব script bash-এ।

4.9 Diagram from L01 — "Unix → POSIX/GNU/Linux" Venn

        ┌──────────────────────┐
        │        UNIX          │
        │  ┌────────┐          │
        │  │ POSIX  │          │
        │  └────────┘          │
        │                      │
        │  ┌────────┐  ┌──────┐│
        │  │  GNU   │  │ LINUX││
        │  └────────┘  └──────┘│
        └──────────────────────┘
GNU + Linux kernel = a distro that mostly follows POSIX

In a written answer, redraw a small Venn and label: "Unix = the family, POSIX = the standard, GNU = the userland, Linux = the kernel."

4.10 The boot story (extra polish for top grade)

BIOS/UEFI → bootloader (GRUB) → Linux kernel → init (systemd)
        → login → shell (bash) → user programs

4.11 Units math: KiB/MiB/GiB (\(2^{10}\)) vs KB/MB/GB (\(10^3\))

Unix tools (ls -lh, free -h, df -h) mostly report binary units, while disk vendors advertise decimal units. You must be able to convert.

Binary unit Definition Bytes Decimal unit Definition Bytes
1 KiB \(2^{10}\) 1 024 1 KB \(10^{3}\) 1 000
1 MiB \(2^{20}\) 1 048 576 1 MB \(10^{6}\) 1 000 000
1 GiB \(2^{30}\) 1 073 741 824 1 GB \(10^{9}\) \(10^9\)
1 TiB \(2^{40}\) \(1.0995 \times 10^{12}\) 1 TB \(10^{12}\) \(10^{12}\)

Fully worked example — why a "1 TB" disk shows only ~931 GiB.

  1. Vendor's "1 TB" means \(10^{12}\) bytes exactly.
  2. The OS divides by \(2^{30}\) to display GiB: \(\dfrac{10^{12}}{2^{30}} = \dfrac{10^{12}}{1\,073\,741\,824}\).
  3. Result: \(931.32\) GiB. Nothing is missing — only the unit differs.

General conversion factor: \(\dfrac{\text{GiB}}{\text{GB}} = \dfrac{2^{30}}{10^9} \approx 1.0737\), so divide any decimal GB figure by 1.0737 to get GiB. Same idea one level up: a "512 GB" SSD shows \(512 \times 10^9 / 2^{30} \approx 476.8\) GiB.

বাংলায়: KiB/MiB/GiB হলো ২-এর ঘাত (\(2^{10}, 2^{20}, 2^{30}\)), আর KB/MB/GB হলো ১০-এর ঘাত — দুটো এক জিনিস নয়। Disk-বিক্রেতা decimal-এ বিজ্ঞাপন দেয় (\(10^{12}\) byte = "1 TB"), কিন্তু Linux binary-তে দেখায়, তাই 1 TB disk-এ দেখবেন মাত্র ~931 GiB — কোনো জায়গা হারায়নি, শুধু একক আলাদা। পরীক্ষায় এই হিসাবটা ধাপে ধাপে করতে বললে \(2^{30} = 1\,073\,741\,824\) সংখ্যাটা মনে রাখলেই হবে।

4.12 Processes: PID, parent and child

A process is a running program plus its state (memory image, open files, environment). Key facts:

  • Every process has a unique PID (process ID) and remembers its parent's PPID.
  • The shell shows its own PID with echo $$; ps -ef lists PID and PPID columns; pstree draws the tree.
  • New processes are created in two steps: fork() clones the calling process (child = near-copy of parent), then exec() replaces the child's memory image with the new program. The parent wait()s for the child to exit.
  • PID 1 is init (today systemd) — the ancestor of every process on the machine.
PID 1  systemd (init) ─── ancestor of every process
 ├── PID 812  sshd ─────── daemon listening for logins
 │    └── PID 4290 sshd ── your SSH connection
 │         └── PID 4291 bash ── login shell  ◄── echo $$ prints 4291
 │              ├── PID 4310 vim run.sh   (bash fork()s, child exec()s vim)
 │              └── PID 4322 ls           (fork + exec, exits quickly)
 └── PID 901  cron ─────── background daemon (scheduled jobs)

fork(): bash clones itself → a child process appears
exec(): the child replaces its memory image with /usr/bin/ls
wait(): bash sleeps until the child exits, then prints the prompt

বাংলায়: প্রতিটা চলমান প্রোগ্রাম একটা process, যার একটা নিজস্ব PID আর একটা parent (PPID) আছে — সবার আদি পূর্বপুরুষ PID 1 (systemd)। নতুন প্রোগ্রাম চালু হয় দুই ধাপে: fork() নিজের একটা কপি বানায়, তারপর exec() সেই কপির ভেতরের সবকিছু নতুন প্রোগ্রাম দিয়ে বদলে দেয়। Shell-এ নিজের PID দেখতে echo $$ — এই fork/exec গল্পটা পরীক্ষায় trace করতে বলা খুব প্রিয় প্রশ্ন।

4.13 The Unix filesystem tree — where things live

/                        root of everything (there is no C:\ in Linux)
├── bin → usr/bin        essential commands (ls, cp, bash)
├── boot                 kernel image (vmlinuz) + GRUB bootloader files
├── dev                  device files — "everything is a file" (sda, tty)
├── etc                  system-wide configuration (os-release, fstab, ssh/)
├── home                 users' home directories (/home/debwa)
├── lib → usr/lib        shared libraries (glibc, libm)
├── opt                  optional third-party software
├── proc                 virtual FS: kernel & process info (cpuinfo, meminfo)
├── root                 home directory of the superuser (not /home/root)
├── tmp                  scratch space, usually cleared at reboot
├── usr                  the bulk of userland (bin, lib, share, include)
└── var                  variable data: logs (/var/log), spool, caches

On an HPC cluster two more locations matter: a slow but backed-up /home/<user> (quota!) and a fast parallel scratch FS such as /scratch or /work (Lustre) for simulation data.


5. Command / Syntax / Code Breakdown

uname — show kernel/system info

Purpose: prove the OS, kernel version, hostname.
Syntax: uname [options]
Useful flags: -a all, -r kernel release, -s kernel name, -n hostname, -m machine arch.
Example:

$ uname -a
Linux luis-cluster 5.14.0-362.24.1.el9.x86_64 #1 SMP PREEMPT_DYNAMIC ... GNU/Linux
$ uname -r
5.14.0-362.24.1.el9.x86_64

Common mistake: using uname -p and getting unknown — that's normal, it's deprecated.
Exam tip: in a written answer name uname -r to identify a kernel.

cat /etc/os-release — distro identity

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
PRETTY_NAME="Ubuntu 22.04.4 LTS"
ID=ubuntu

echo $SHELL — current shell

$ echo $SHELL
/bin/bash

lsb_release -a (when present)

$ lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy

whoami, id, hostname

$ whoami
debwa
$ id
uid=1000(debwa) gid=1000(debwa) groups=1000(debwa),27(sudo)
$ hostname
laptop-01

which, type, command -v

To inspect what runs when you type a command:

$ which ls         # /usr/bin/ls
$ type cd          # cd is a shell builtin
$ command -v grep  # /usr/bin/grep

strace — trace system calls (advanced)

$ strace -e trace=openat,read,write -c ls > /dev/null
% time     seconds  usecs/call     calls    errors syscall
 ...

This proves how user-space ls calls into kernel space.


6. Mandatory Practical Example

Purpose

Show the user-mode → kernel-mode boundary in action by counting system calls of ls.

Input

A directory with a few files. Tool: strace.

Code / Command

mkdir -p ~/demo && cd ~/demo
touch a.txt b.txt c.txt
strace -c ls > /dev/null 2>strace.log
cat strace.log | head -20

Expected Output

% time     seconds  usecs/call     calls    errors syscall
 ------ ----------- ----------- --------- --------- ----------------
   0.00    0.000000           0         8           read
   0.00    0.000000           0         3         3 write
   0.00    0.000000           0         3           openat
   0.00    0.000000           0         3           close
   ...

Step-by-Step Explanation

  • strace -c collects a summary of each system call.
  • Every read, write, openat, close is a user → kernel transition.
  • Even a tiny ls triggers many syscalls — that is kernel mode doing the actual work.

Real-Life HPC/CFD Meaning

When your CFD code is "stuck", strace -p <pid> may show that it is hanging on a read from a network share — i.e. the cluster's parallel file system is the bottleneck. Same idea, much bigger numbers.

Written Exam Relevance

A favorite question: "Show, with one example, that user-space programs reach kernel features through system calls." Use ls → write here.


7. Real HPC/CFD Workflow

When you log onto a cluster:

ssh me@cluster
uname -a              # → confirm Linux + kernel version
cat /etc/os-release   # → confirm distro (Rocky 9 / SLES 15 / Ubuntu 22)
echo $SHELL           # → bash
module avail          # → which compilers / MPI versions exist (cluster-specific)

Each of those is a user-mode call that asks the kernel through GNU userland (coreutils). You typically never need kernel-mode access yourself.


8. Exercises and Solutions

There is no separate exercise sheet specific to "Unix/Linux Basics" — this material is examined directly in the written exam. Below is a self-made exam-style exercise.

Exercise 2-A. Identify the OS layer.

Item Layer (Kernel / Userland / Shell / Distro)
bash Userland (GNU)
ls Userland (GNU coreutils)
Linux 6.5.0 Kernel
Ubuntu 22.04 Distribution
gcc Userland (GNU)
systemd Userland init
ext4 driver Kernel module
zsh Shell

Exercise 2-B. Trace a syscall.

Write the chain of calls when you cat hello.txt (where the file is 12 bytes long).

Solution:

  1. User runs cat hello.txt.
  2. Bash forks, execs /usr/bin/cat.
  3. cat calls open("hello.txt") → kernel returns FD 3.
  4. cat calls read(3, buf, 4096) → kernel reads 12 bytes from disk via the ext4 driver.
  5. cat calls write(1, buf, 12) → kernel pushes bytes to the terminal.
  6. cat calls close(3), then exit(0).

Marking scheme (5 marks): 1 for fork/exec, 1 for open, 1 for read, 1 for write, 1 for close+exit.

বাংলায়: syscall-trace প্রশ্নের ছাঁচ মুখস্থ রাখো: fork → exec → open → read → write → close/exit। যেকোনো কমান্ডের গল্প এই ছয় ধাপেই বলা যায় — শুধু ফাইল আর byte-সংখ্যা বদলায়। প্রতিটা ধাপে "user mode থেকে kernel mode-এ ঢোকা হচ্ছে" বললে extra নম্বর।

Exercise 2-C. Pick the right OS for an HPC node

Why is RHEL/Rocky preferred on production clusters?

Answer (5 marks): stable long-term ABI, certified by ISVs (Intel, NVIDIA), corporate support, predictable kernel, RPM packaging, SELinux defaults, common across vendors so software stacks are portable.


9. Written Exam Focus

9.1 Short Answers

Q. Define POSIX. A. POSIX (Portable Operating System Interface) is an IEEE standard defining the system-call and shell-utility interface of Unix-like OSes, ensuring software portability.

Q. Difference between Linux and GNU/Linux. A. "Linux" alone is the kernel; "GNU/Linux" denotes the full OS = kernel + GNU userland.

Q. Define a Linux distribution. A. A package combining the Linux kernel, GNU userland, an init system, and a package manager into a usable OS (e.g. Ubuntu, Rocky).

Q. Define the kernel. A. The privileged core of the OS controlling hardware, scheduling processes, managing memory, serving system calls.

Q. Define a shell. A. A program that reads user commands, launches the corresponding programs via the kernel, and returns their output.

9.2 Medium Answers

Q. (8 marks) Explain the relationship between Unix, POSIX, GNU and Linux.

A. Unix is the original 1970 AT&T operating system. POSIX is the IEEE standard capturing Unix's interface for portability. GNU is the free-software project (1983) re-implementing Unix's userland (bash, gcc, coreutils). Linux is the free kernel (1991) by Linus Torvalds. Linux kernel + GNU userland = a distribution, mostly POSIX-compliant. Unix is the family, POSIX the contract, GNU the toolkit, Linux the engine.

Q. (5 marks) Explain user mode vs kernel mode with one example.

A. The CPU runs in privileged kernel mode (ring 0) or unprivileged user mode (ring 3). User programs reach hardware only through system calls that switch into kernel mode. Example: printf("hi") calls libc's write(), which executes the syscall instruction; the kernel writes "hi" to the terminal and returns. This separation stops a buggy program from crashing the system.

9.3 Long Answer (12 marks)

Q. Describe the architecture of a modern Linux system from pressing Enter on ls until characters appear.

A.

  1. The terminal driver passes ls\n to bash (user mode).
  2. Bash word-splits, then fork() → kernel creates a child process.
  3. The child runs execve("/usr/bin/ls", …) → kernel loads the ELF image.
  4. ls opens the directory: openat(AT_FDCWD, ".", O_RDONLY) → FD.
  5. ls calls getdents64(fd, …) → kernel reads entries via VFS → ext4 driver.
  6. ls calls write(1, output, len) → kernel hands bytes to the tty driver.
  7. The terminal renders them.
  8. ls exit(0); bash receives SIGCHLD, prints the prompt.

Connection to HPC/CFD. The same chain runs when your solver writes a 5 GB checkpoint to Lustre — pwrite from many MPI ranks, all mediated by the kernel.

Conclusion. User processes never touch hardware directly; the kernel mediates every step.

9.4 Output Prediction

$ uname -s        →  Linux
$ uname -m        →  x86_64
$ uname -o        →  GNU/Linux

9.5 Comparison Tables

Aspect Unix Linux
Year 1970 1991
Owner AT&T (then various) Torvalds + community
Licence proprietary (mostly) GPLv2 (kernel)
Cost paid free
Variants AIX, HP-UX, Solaris, macOS Ubuntu, RHEL, …
Source closed historically open
POSIX Linux
A standard / specification A kernel
Defines API + shell utilities Implements (mostly) the API
IEEE-maintained Linus + community
Mode Privileges Crash impact Example
User limited program dies bash, gcc, your code
Kernel full system panic drivers, scheduler

9.6 Templates

Definition template: "X is …; it differs from Y in …; it is needed for …"

Trace template: "Step 1: shell parses; Step 2: fork+exec; Step 3: process opens X; Step 4: kernel reads/writes; Step 5: process exits."

9.7 Marking Scheme — "Define Unix/POSIX/GNU/Linux" (8 marks)

  • 1 each for the four definitions.
  • 1 "Linux ≈ kernel".
  • 1 "GNU = userland".
  • 1 "POSIX = standard".
  • 1 example (e.g. macOS is POSIX-certified).

10. Very Hard Questions

Beginner

  1. Who released the Linux kernel? → Linus Torvalds, 1991.
  2. What does GNU stand for? → GNU is Not Unix.
  3. Licence of the Linux kernel? → GPLv2.
  4. Two Unix-derived OSes. → macOS, Solaris.
  5. What is "Tux"? → Linux's penguin mascot.

Intermediate

  1. Why was Unix portable? → Rewritten in C in 1973.
  2. Who maintains POSIX? → IEEE.
  3. Role of init? → First user-space process; starts services.
  4. Why RHEL/Rocky on clusters? → long support, ISV certification.
  5. Why "Bourne-Again Shell"? → successor of Stephen Bourne's sh.

Hard

  1. Two POSIX features. → API definitions (fork); shell utilities (grep).
  2. Why is Linux ≠ Unix legally? → Unix-like but not derived from AT&T code.
  3. Why is systemd controversial? → scope creep; some prefer minimal init.
  4. What is glibc? → GNU C library; user-side implementation of POSIX APIs.
  5. Why does ls work on macOS too? → both follow POSIX.

Very Hard

  1. How does a syscall transition privilege levels (x86_64)? → the syscall instruction jumps to an MSR-defined kernel entry, saving user RIP/RFLAGS.
  2. Why is "everything is a file" central? → generic read/write work on devices, sockets, pipes.
  3. Monolithic (Linux) vs microkernel. → drivers in kernel space vs pushed to user space for safety at IPC cost.

Deep Integration

  1. How does the Unix philosophy map to this course? → small tools + text streams = the pipe-based workflows of Ch 7.
  2. Why is POSIX still relevant? → containers, WSL, embedded all rely on it.

Coding/Command

  1. Print kernel version. → uname -r.
  2. Current shell. → echo $SHELL or ps -p $$.

Debugging

  1. bash: ls: command not found though /bin/ls exists. → PATH cleared.
  2. System still boots the old kernel after update. → GRUB not updated / default entry old.

Long Written

  1. (250 words) "Why do HPC clusters choose Linux distributions over BSD or Windows?"

11. Debugging and Mistake Analysis

Mistake Why wrong Correct version Explanation
"Linux is an OS" strictly the kernel "Linux kernel; OS = GNU/Linux" strict exams care
Confusing GNU and Linux GNU has no finished kernel "GNU is the userland" Hurd never shipped
"POSIX is an OS" it's a standard "IEEE specification" contract, not software
"macOS is Linux" BSD-derived "Unix-like, POSIX-certified" different lineage
"kernel mode = admin" privilege ring, not user role "ring 0" terminology
$BASH vs $SHELL different meanings $SHELL = login shell subtle in scripts

12. Mini Project for Mastery

Goal: Build a one-page "system identity" report an admin would recognise.

{
  echo "===== Host ====="
  hostname; whoami; id
  echo
  echo "===== Kernel ====="
  uname -a
  echo
  echo "===== Distribution ====="
  cat /etc/os-release
  echo
  echo "===== Shell ====="
  echo "$SHELL"; bash --version | head -1
  echo
  echo "===== CPU ====="
  lscpu | head -10
  echo
  echo "===== Memory ====="
  free -h
} > system_report.txt
cat system_report.txt

Connection to exam: each section demonstrates one layer from §4 (kernel, userland, shell, distro). Be ready to explain any line.


13. Final Chapter Cheat Sheet

Topic Memorise
Unix 1970, AT&T, proprietary
POSIX IEEE standard, portability
GNU free userland, Stallman 1983
Linux kernel, Torvalds 1991, GPLv2
Distro kernel + userland + init + package mgr
Cluster distros Rocky / Alma / SLES / Ubuntu HPC
Modes user (ring 3) vs kernel (ring 0)
Syscall syscall instruction
Units KiB = \(2^{10}\) B vs KB = \(10^{3}\) B; 1 TB disk ≈ 931 GiB
Init systemd
Trap "Linux is the OS" → say "kernel"
Top-grade phrase "GNU userland on top of the Linux kernel forms a POSIX-mostly compliant distribution."

14. Mock Exam — Four Levels

Level 1 — Basic (definitions & syntax)

Q1. Name the four layers between hardware and you when typing a command.

Solution: hardware → kernel → shell → user program (the shell launches it); the terminal mediates I/O.

Q2. Which command prints kernel name, release, and architecture in one line?

Solution: uname -a (individually: -s, -r, -m).

Q3. What is a PID and which PID does the init/systemd process have?

Solution: Process IDentifier; init/systemd is PID 1.

Q4. GNU/Linux: which part is GNU, which is Linux?

Solution: GNU = userland tools (bash, gcc, coreutils); Linux = kernel.

Q5. What does POSIX standardise — software or an interface?

Solution: An interface (system calls + shell utilities), not an implementation.

Level 2 — Intuitive (predict / explain why)

Q1. echo $$ prints 5001. Then (echo $$) prints… what and why?

Solution: Also 5001 — $$ expands to the PID of the main shell even in a subshell (use $BASHPID to see the subshell's own PID). The trap: a subshell IS a new process, but $$ deliberately keeps the parent shell's PID.

Q2. Why does a "1 TB" drive show as ~931 GiB? Show the computation.

Solution: \(10^{12} / 2^{30} = 10^{12} / 1.0737\times10^9 \approx 931\) — decimal marketing units vs binary OS units. Nothing is missing.

Q3. Why can't a user program write directly to the disk controller?

Solution: It runs in user mode (ring 3); hardware access requires privileged instructions available only in kernel mode — it must request the kernel via syscalls. Protection and multiplexing are the point.

Q4. You kill a shell; its background job dies too. Yet PID 1 never dies. Explain via the process tree.

Solution: Jobs are children of the shell and receive SIGHUP when the session leader dies (Ch 4: nohup prevents this). PID 1 is the root of the tree, started by the kernel — it adopts orphans and only ends at shutdown.

Q5. cat /etc/os-release shows Ubuntu but uname -r shows a "microsoft" kernel string. What machine is this?

Solution: WSL2 — Ubuntu userland (distribution) running on Microsoft's Linux kernel build inside Windows. Perfect illustration that distro and kernel are separable.

Level 3 — Hard (exam level)

Q1. (8 marks) Trace wc -l data.txt (file = 100 lines, 2 KiB) at the syscall level.

Solution: bash: fork() → child execve("/usr/bin/wc", ["wc","-l","data.txt"], envp)openat(AT_FDCWD,"data.txt",O_RDONLY) = FD 3 → read(3, buf, …) returns 2048 bytes (1 call, file < buffer) → wc counts \n in user space (no syscall needed for counting!) → write(1, "100 data.txt\n", 13)close(3), exit(0) → bash reaps via wait(). বাংলা ইঙ্গিত: গোনার কাজটা syscall নয় — user space-এ হয়; "কোন ধাপ kernel-এ, কোনটা user-এ" আলাদা করলেই উত্তর অনন্য।

Q2. (8 marks) A cluster node has 256 GiB RAM. Express that in GB, and compute how many \(10^8\)-cell double-precision velocity fields (3 components) fit.

Solution: \(256 \times 2^{30} = 2.749\times10^{11}\) B \(\approx 274.9\) GB. One field: \(3 \times 8 \times 10^8 = 2.4\times10^9\) B = 2.4 GB. Fit: \(2.749\times10^{11} / 2.4\times10^9 \approx 114\) fields. বাংলা ইঙ্গিত: GiB→GB-তে ×1.0737; আর field-size = component × 8 byte × cell — ইউনিট-হিসাবই পুরো প্রশ্ন।

Q3. (8 marks) Explain why fork() followed by exec() is a two-step design rather than one "spawn" call, and name one thing the shell does between the two.

Solution: Separation lets the child customise itself before becoming the new program: between fork and exec the shell sets up redirections (dup2 of FDs for >/|), changes process group, resets signals. One syscall doing all of this would need a huge parameter list; the Unix way composes small primitives. বাংলা ইঙ্গিত: উত্তরের হৃদয়: মাঝখানের ফাঁকটাই feature — redirection ওখানেই বসে; Ch 3-এর > আসলে dup2-এর গল্প।

Q4. (10 marks) With the layered diagram in mind: list which layer (kernel / userland / shell / distro-policy) each item belongs to — ext4 driver, grep, tab-completion, apt, the scheduler, glibc, ~/.bashrc.

Solution: ext4 driver — kernel; grep — userland (GNU); tab-completion — shell (bash readline); apt — distro-policy/userland package manager; scheduler — kernel; glibc — userland (the syscall wrapper boundary!); ~/.bashrc — shell configuration. বাংলা ইঙ্গিত: glibc-টা ফাঁদ — সে user space-এ থাকে কিন্তু kernel-এর দরজায় দাঁড়িয়ে syscall ডাকে; এই এক লাইনের ব্যাখ্যা পার্থক্য গড়ে।

Q5. (10 marks) A program in an infinite loop in user mode: can it freeze the whole node? Justify via kernel mechanisms, and name the HPC consequence on a login node.

Solution: No — the kernel's preemptive scheduler interrupts it via timer interrupts; other processes keep running (it burns one core). BUT on a shared login node, N users doing this exhausts all cores — exactly why heavy work belongs on compute nodes via SLURM (Ch 15). বাংলা ইঙ্গিত: "preemptive scheduling + timer interrupt" — এই দুই শব্দবন্ধই নম্বরের চাবি; তারপর login-node etiquette-এ সেতু বানাও।

Level 4 — Beyond the lecture (transfer + coding)

Q1. Write a bash snippet (Ch 8 transfer) printing the ancestry chain (PID + command) of the current shell up to PID 1.

Solution:

p=$$
while [ "$p" -gt 0 ]; do
    ps -o pid=,comm= -p "$p"
    p=$(ps -o ppid= -p "$p" | tr -d ' ')
    [ "$p" -eq 1 ] && { ps -o pid=,comm= -p 1; break; }
done
Walks the PPID chain — directly visualising the process tree from §4. বাংলা ইঙ্গিত: ps -o ppid= -p PID — parent বের করার idiom; লুপটাই process-tree-র জীবন্ত প্রমাণ।

Q2. Why does an MPI rank that segfaults NOT take down the node's kernel, yet CAN take down the whole MPI job? Connect both halves precisely.

Solution: The segfault is a user-mode memory-protection violation → the kernel delivers SIGSEGV and kills only that process; ring separation protects the node. But the MPI runtime notices a dead rank (broken communication) and aborts the remaining ranks — job-level "all or nothing" is an MPI-runtime policy, not a kernel one. বাংলা ইঙ্গিত: দুই স্তর আলাদা করো: kernel বাঁচায় machine-কে, MPI-runtime মারে job-কে — সংযোগটা স্পষ্ট লিখলেই প্রশ্ন শেষ।

Q3. Container thinking: Docker images run Ubuntu userland on a Rocky host. Which kernel runs the container's processes, and what limits does that place on an HPC container?

Solution: The HOST's kernel (containers share it — no second kernel). Limits: the container cannot use kernel features/drivers absent on the host (e.g. a specific MPI-over-RDMA module or newer cgroup features); kernel-version-dependent software must match the host. That's why HPC sites use Apptainer/Singularity with host-MPI bind-ins. বাংলা ইঙ্গিত: "container = userland-এর স্যুটকেস, kernel ধার নেয়" — এই ছবিটা থাকলে সব container-প্রশ্ন সহজ।

Q4. Combine Ch 7 + Ch 2: one pipeline that counts how many DISTINCT kernel modules containing "nvidia" are loaded, using /proc or lsmod.

Solution:

lsmod | awk 'NR>1 && $1 ~ /nvidia/ {print $1}' | sort -u | wc -l
(lsmod reads /proc/modules.) awk filters the module-name column; the classic dedup-count tail finishes it. বাংলা ইঙ্গিত: lsmod আসলে /proc/modules-এর সুন্দর রূপ — "kernel-এর তথ্যও text stream" — Unix দর্শনের পূর্ণবৃত্ত।


End of Chapter 2.