1. Gyanastra
  2. Operating Systems
  3. UGC NET: Linux Operating System
  4. Linux Design Principles and Kernel Modules
Free lesson, no account needed 12 min read 26 practice questions after it
Asked in past papers1 time
  • UGC NET2013

Operating Systems · UGC NET: Linux Operating System

Linux Design Principles and Kernel Modules

Overview

Linux is a Unix-like, open-source, multiuser, preemptive multitasking operating system whose core is a monolithic kernel with loadable modules. This lesson explains Linux design principles, how a loadable kernel module (LKM) is built, loaded, used and unloaded, and how module code differs from user-space code.
You also need the surrounding exam facts: the shell as command interpreter, process creation with fork and replacement with exec, clone for threads, signals, init/PID 1, /proc, and Linux’s fair preemptive scheduling.
Prerequisites are basic process states, system calls, file descriptors, and kernel mode versus user mode.

Key concepts

  • Linux design principles: Unix-like interface, open source, multiuser, preemptive multitasking, virtual memory, hierarchical file system, device files, and a monolithic kernel with loadable modules. Core services such as scheduling, memory management, VFS and networking are compiled into the kernel; optional drivers and filesystems are loaded at runtime as modules.
  • Monolithic-with-modules: loaded modules are linked into the kernel’s address space and run in kernel mode. This is not a microkernel; a faulty module can crash the whole system.
  • Loadable kernel module: a .ko object file that extends the running kernel without rebuilding or rebooting the whole kernel for every supported device.
    It has no main; it uses module_init and module_exit, and may use only kernel APIs, not ordinary user-space libraries.
  • Symbols and licensing: EXPORT_SYMBOL makes a kernel symbol available to other modules; EXPORT_SYMBOL_GPL exposes it only to GPL-compatible modules. A proprietary module that uses a GPL-only symbol is rejected during symbol resolution.
    Loading a non-GPL module normally taints the kernel. MODULE_LICENSE, MODULE_AUTHOR, MODULE_DESCRIPTION and MODULE_VERSION supply metadata shown by modinfo.
  • Module tools: insmod inserts one .ko file but does not resolve dependencies. modprobe uses modules.dep and loads dependencies first. rmmod removes one module only if its reference count is zero. modprobe -r removes a module and unused dependencies. depmod builds modules.dep. lsmod lists loaded modules, reading /proc/modules.
    Modules for the running kernel live under /lib/modules/$(uname -r).
  • Reference count: the number of current users or dependent modules. rmmod fails while the count is greater than zero. Device opens, dependencies, and explicit try_module_get calls can raise it; closing or releasing them lowers it.
  • Initialization and cleanup: an init function registered by module_init must return 0 on success and a negative errno on failure.
    A negative return aborts loading. __init places initialization code in a section whose memory can be freed after initialization; __exit is for cleanup code.
  • Version matching: modules are built for a particular kernel version and configuration. A version mismatch, shown by vermagic or symbol-version differences, normally makes loading fail.
    Linux does not promise a stable binary module ABI across arbitrary kernel versions.
  • /proc: the procfs pseudo-filesystem. It is not ordinary disk storage; it exposes process and kernel information, including /proc/modules, /proc/cpuinfo, /proc/meminfo, and the sysctl area /proc/sys. /sys is sysfs, used mainly for devices, drivers and kernel objects; /dev holds device nodes.
  • init process: after the kernel mounts the root filesystem, it starts PID 1, the first user-space process. Modern Linux commonly uses systemd; classic systems use init.
    PID 1 starts services and login/getty processes, and reaps orphaned processes. PID 0 is the idle/scheduler kernel thread in classic terminology, so init is not the first process overall; it is the first user-space process.
  • fork(): creates a child that is an almost exact copy of the parent. Heap, stack, data and text are private copies under copy-on-write; changes to ordinary heap or stack variables are not seen across processes.
    Shared memory segments attached before fork() remain shared, because both processes map the same physical pages.
    A successful fork() returns 0 in the child and the child’s PID in the parent; on failure it returns -1 in the parent.
  • exec family: replaces the current process image with a new program. It does not create a new process: the PID stays the same. Open file descriptors stay open unless marked close-on-exec.
    On success, exec does not return; on failure it returns -1.
  • clone(): Linux’s general process/thread creation call.
    Flags control sharing: CLONE_VM shares address space, CLONE_FILES shares file-descriptor table, CLONE_SIGHAND shares signal handlers, and CLONE_THREAD creates a thread in the same thread group. fork() is effectively no-sharing clone; pthreads use clone with sharing flags.
  • Signal: an asynchronous notification to a process about an event. It is not process creation. SIGKILL and SIGSTOP cannot be caught, blocked or ignored.
  • Shell: the command-line interpreter, not part of the kernel. It expands variables and wildcards, handles redirection and pipes, and executes commands by fork plus exec; it also runs shell scripts.
  • Scheduling: Linux uses multiple scheduling classes. Normal tasks use the Completely Fair Scheduler (CFS), which is fair and preemptive and tracks virtual runtime.
    Real-time policies such as SCHED_FIFO and SCHED_RR provide preemptive priority scheduling. The nice range is conventionally -20 highest priority to +19 lowest priority.
  • UNIX versus Linux trap: classical UNIX is treated as monolithic in UGC questions; Linux’s loadable-module design is a Linux feature. Do not transfer Linux module facts to UNIX unless the question explicitly says Linux.

How it works

  1. Boot to init: the kernel initialises memory, scheduler and drivers, mounts the root filesystem, then starts PID 1. PID 1 starts services, consoles and login/getty processes.
  2. Module insertion with insmod: the kernel reads the .ko, checks format and version, allocates memory, resolves undefined symbols against exported kernel symbols, then calls the module’s init function.
    If init returns 0, the module stays loaded; if it returns a negative value, loading aborts.
  3. Module insertion with modprobe: modprobe first reads /lib/modules/$(uname -r)/modules.dep and configuration files, determines dependencies, loads dependency modules first, then loads the requested module. This is why modprobe is preferred when dependencies exist.
  4. Module removal: rmmod checks the reference count. If it is greater than zero, removal fails. If it is zero, the kernel calls the exit function, releases the module’s memory, and removes its symbols.
  5. Process creation and execution: the shell uses fork to create a child, then the child uses exec to replace itself with the requested command. clone is used when controlled sharing, especially threads, is required.
    Signals may notify a process at any stage.
Figure. Notice that modprobe loads dependency modules before the requested module; the kernel then validates version and license, resolves exported symbols, and runs module_init so the module becomes active.

Worked examples

Example 1 — shared memory versus heap after fork. Ramesh’s process sets a global heap variable x = 10 and attaches a shared memory segment containing 99. It forks. The child changes x to 20 and the shared memory value to 77.
The parent later reads both. The parent sees x = 10, because the heap is a private copy under copy-on-write. The parent sees 77 in the shared memory segment, because shared memory pages are genuinely shared.
Hence heap and stack are not shared by fork, but shared memory segments are.

Example 2 — effect of exec. Geeta’s process has PID 4210 and calls execl("/bin/echo","echo","hello",NULL). On success, PID 4210 remains, /bin/echo replaces the old program image, hello is printed, and the old code after execl never runs.
Open file descriptors remain open unless close-on-exec. On failure, exec returns -1 and the old program continues. No new process is created.

Example 3 — reference count and rmmod. A module demo has reference count 2 because two open device files are held by programs in Ankur’s and Meena’s sessions. Ankur closes his file: count becomes 1.
Meena closes hers: count becomes 0. Now rmmod demo succeeds. If rmmod demo is attempted before both closes, it fails because the count is positive. If another module depends on demo, that dependency also counts as use.

Example 4 — match the module facility.
List I: A. EXPORT_SYMBOL, B. module_init, C. modinfo, D. depmod.
List II: i. displays metadata such as author and license; ii. builds modules.dep; iii. marks a symbol for use by other modules; iv. registers the initialization function.
Correct match: A-iii, B-iv, C-i, D-ii. EXPORT_SYMBOL exposes a symbol; module_init registers init; modinfo reads metadata from a .ko; depmod creates the dependency database.

Example 5 — order of modprobe steps. Steps: 1. kernel resolves symbols, 2. modprobe reads modules.dep, 3. module_init runs, 4. dependency module is loaded, 5. lsmod shows the module.
Correct order: 2, 4, 1, 3, 5. modprobe uses the dependency database first, loads dependencies, the kernel resolves symbols against exported symbols, init runs, and the active module appears in lsmod.

Comparison

FeatureKernel moduleUser-space program
Runs inKernel mode, kernel address spaceUser mode, own virtual address space
Entry pointmodule_initmain
Fault effectCan crash or panic whole systemUsually kills only that process
LibrariesKernel APIs onlylibc and user libraries
Removalrmmod or modprobe -r if refcount zeroProcess exits
ToolPurposeKey point
insmodInsert one .koDoes not load dependencies
modprobeInsert module with dependenciesUses modules.dep and config
rmmodRemove one moduleFails if reference count > 0
modprobe -rRemove module and unused dependenciesDependency-aware removal
lsmodList loaded modulesReads /proc/modules
modinfoShow module metadataReads the .ko
depmodBuild modules.depRun after installing modules
Call/eventCreates a process?Effect
forkYesDuplicates process; returns 0 in child, child PID in parent, -1 on failure
execNoReplaces image; same PID; no return on success
cloneYes, or creates a threadFlags control sharing such as CLONE_VM
SignalNoNotifies process about an event
PathTypeMain use
/procprocfs pseudo-filesystemProcess info, /proc/modules, kernel parameters under /proc/sys
/syssysfs pseudo-filesystemDevices, drivers, kernel object attributes
/devDevice nodesFiles representing I/O devices

Revision summary

  1. Linux is monolithic with loadable modules, not a microkernel.
  2. A module runs in kernel mode; a module bug can crash the whole system.
  3. fork: child sees 0, parent sees child PID, failure is -1.
  4. fork does not share heap or stack; shared memory segments remain shared.
  5. exec keeps the PID, creates no new process, and does not return on success.
  6. clone uses flags; threads use CLONE_VM, CLONE_FILES, CLONE_THREAD.
  7. insmod loads one module; modprobe loads dependencies; depmod builds modules.dep.
  8. rmmod works only when the reference count is zero.
  9. __init memory can be freed after init; init returns 0 on success, negative on failure.
  10. EXPORT_SYMBOL exposes symbols; GPL-only symbols require a GPL-compatible module.
  11. PID 1 is init/systemd, the first user-space process; PID 0 is the idle/scheduler kernel thread.
  12. CFS is fair and preemptive; real-time classes use preemptive priority; nice runs from -20 to +19.

Previous-year exam insights

  • Direct definition questions ask what an LKM is, why Linux is called monolithic-with-modules, what insmod, modprobe, depmod, lsmod, modinfo do, and what /proc or /sys provides.
  • Statement I/II questions test whether exec creates a new process, whether a module runs in kernel mode, whether rmmod can unload a busy module, and whether a proprietary module can use GPL-only symbols.
  • Match-the-list questions pair fork/exec/clone/signal with effects, or pair EXPORT_SYMBOL/module_init/modinfo/depmod with their roles.
  • Assertion-reason questions often use: module bug crashes the system because module code runs in kernel mode, or Linux is monolithic even with modules because modules are linked into kernel address space.
  • Numeric questions on reference count require simple decrementing: start with count, close each user, and allow rmmod only at zero.
  • Code-based module questions check that init returns 0 on success and a negative errno on failure; __init memory is discarded after initialization.
  • Linux scheduling is asked as fair preemptive CFS for normal tasks and preemptive priority plus time-sharing support through multiple scheduling classes.
  • Common traps: calling init the first process overall, saying exec returns in the child, saying modules run in user mode, saying insmod resolves dependencies, and confusing /proc with /sys or /dev.

Practice at PARIKSHA

You are ready for the PARIKSHA tests of this sub-topic when you can:

  1. Define a loadable kernel module and explain why Linux is monolithic-with-modules.
  2. Trace fork return values and explain which process state is shared and which is copied.
  3. Explain the complete effect of a successful exec call, including PID and open descriptors.
  4. Distinguish fork, exec, clone and signal in a match-the-list question.
  5. List the roles of insmod, modprobe, rmmod, lsmod, modinfo, depmod, /proc/modules and modules.dep.
  6. Compute module reference counts after opens and closes, and decide whether rmmod succeeds.
  7. Explain __init, module_init, module_exit, EXPORT_SYMBOL and GPL-only symbol resolution.
  8. Describe the correct order of steps used by modprobe when dependencies exist.
  9. State the roles of /proc, /sys, /dev, PID 0, PID 1 and the CFS scheduler.
  10. Identify version-mismatch and license traps in module-loading questions.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2