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
.koobject file that extends the running kernel without rebuilding or rebooting the whole kernel for every supported device.
It has nomain; it usesmodule_initandmodule_exit, and may use only kernel APIs, not ordinary user-space libraries. - Symbols and licensing:
EXPORT_SYMBOLmakes a kernel symbol available to other modules;EXPORT_SYMBOL_GPLexposes 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_DESCRIPTIONandMODULE_VERSIONsupply metadata shown bymodinfo. - Module tools:
insmodinserts one.kofile but does not resolve dependencies.modprobeusesmodules.depand loads dependencies first.rmmodremoves one module only if its reference count is zero.modprobe -rremoves a module and unused dependencies.depmodbuildsmodules.dep.lsmodlists 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.
rmmodfails while the count is greater than zero. Device opens, dependencies, and explicittry_module_getcalls can raise it; closing or releasing them lowers it. - Initialization and cleanup: an init function registered by
module_initmust return0on success and a negativeerrnoon failure.
A negative return aborts loading.__initplaces initialization code in a section whose memory can be freed after initialization;__exitis for cleanup code. - Version matching: modules are built for a particular kernel version and configuration. A version mismatch, shown by
vermagicor 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./sysis sysfs, used mainly for devices, drivers and kernel objects;/devholds device nodes.initprocess: after the kernel mounts the root filesystem, it starts PID 1, the first user-space process. Modern Linux commonly usessystemd; classic systems useinit.
PID 1 starts services and login/getty processes, and reaps orphaned processes. PID 0 is the idle/scheduler kernel thread in classic terminology, soinitis 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 beforefork()remain shared, because both processes map the same physical pages.
A successfulfork()returns0in the child and the child’s PID in the parent; on failure it returns-1in the parent.execfamily: 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,execdoes not return; on failure it returns-1.clone(): Linux’s general process/thread creation call.
Flags control sharing:CLONE_VMshares address space,CLONE_FILESshares file-descriptor table,CLONE_SIGHANDshares signal handlers, andCLONE_THREADcreates 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.
SIGKILLandSIGSTOPcannot 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
forkplusexec; 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 asSCHED_FIFOandSCHED_RRprovide preemptive priority scheduling. Thenicerange is conventionally-20highest priority to+19lowest 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
- 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.
- 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 returns0, the module stays loaded; if it returns a negative value, loading aborts. - Module insertion with
modprobe:modprobefirst reads/lib/modules/$(uname -r)/modules.depand configuration files, determines dependencies, loads dependency modules first, then loads the requested module. This is whymodprobeis preferred when dependencies exist. - Module removal:
rmmodchecks 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. - Process creation and execution: the shell uses
forkto create a child, then the child usesexecto replace itself with the requested command.cloneis used when controlled sharing, especially threads, is required.
Signals may notify a process at any stage.
Drawing the diagram…
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
| Feature | Kernel module | User-space program |
|---|---|---|
| Runs in | Kernel mode, kernel address space | User mode, own virtual address space |
| Entry point | module_init | main |
| Fault effect | Can crash or panic whole system | Usually kills only that process |
| Libraries | Kernel APIs only | libc and user libraries |
| Removal | rmmod or modprobe -r if refcount zero | Process exits |
| Tool | Purpose | Key point |
|---|---|---|
insmod | Insert one .ko | Does not load dependencies |
modprobe | Insert module with dependencies | Uses modules.dep and config |
rmmod | Remove one module | Fails if reference count > 0 |
modprobe -r | Remove module and unused dependencies | Dependency-aware removal |
lsmod | List loaded modules | Reads /proc/modules |
modinfo | Show module metadata | Reads the .ko |
depmod | Build modules.dep | Run after installing modules |
| Call/event | Creates a process? | Effect |
|---|---|---|
fork | Yes | Duplicates process; returns 0 in child, child PID in parent, -1 on failure |
exec | No | Replaces image; same PID; no return on success |
clone | Yes, or creates a thread | Flags control sharing such as CLONE_VM |
| Signal | No | Notifies process about an event |
| Path | Type | Main use |
|---|---|---|
/proc | procfs pseudo-filesystem | Process info, /proc/modules, kernel parameters under /proc/sys |
/sys | sysfs pseudo-filesystem | Devices, drivers, kernel object attributes |
/dev | Device nodes | Files representing I/O devices |
Revision summary
- Linux is monolithic with loadable modules, not a microkernel.
- A module runs in kernel mode; a module bug can crash the whole system.
fork: child sees0, parent sees child PID, failure is-1.forkdoes not share heap or stack; shared memory segments remain shared.execkeeps the PID, creates no new process, and does not return on success.cloneuses flags; threads useCLONE_VM,CLONE_FILES,CLONE_THREAD.insmodloads one module;modprobeloads dependencies;depmodbuildsmodules.dep.rmmodworks only when the reference count is zero.__initmemory can be freed after init; init returns0on success, negative on failure.EXPORT_SYMBOLexposes symbols; GPL-only symbols require a GPL-compatible module.- PID 1 is
init/systemd, the first user-space process; PID 0 is the idle/scheduler kernel thread. - CFS is fair and preemptive; real-time classes use preemptive priority; nice runs from
-20to+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,modinfodo, and what/procor/sysprovides. - Statement I/II questions test whether
execcreates a new process, whether a module runs in kernel mode, whetherrmmodcan 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 pairEXPORT_SYMBOL/module_init/modinfo/depmodwith 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
rmmodonly at zero. - Code-based module questions check that init returns
0on success and a negativeerrnoon failure;__initmemory 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
initthe first process overall, sayingexecreturns in the child, saying modules run in user mode, sayinginsmodresolves dependencies, and confusing/procwith/sysor/dev.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- Define a loadable kernel module and explain why Linux is monolithic-with-modules.
- Trace
forkreturn values and explain which process state is shared and which is copied. - Explain the complete effect of a successful
execcall, including PID and open descriptors. - Distinguish
fork,exec,cloneand signal in a match-the-list question. - List the roles of
insmod,modprobe,rmmod,lsmod,modinfo,depmod,/proc/modulesandmodules.dep. - Compute module reference counts after opens and closes, and decide whether
rmmodsucceeds. - Explain
__init,module_init,module_exit,EXPORT_SYMBOLand GPL-only symbol resolution. - Describe the correct order of steps used by
modprobewhen dependencies exist. - State the roles of
/proc,/sys,/dev, PID 0, PID 1 and the CFS scheduler. - Identify version-mismatch and license traps in module-loading questions.
Attempt the sub-topic test first, then the subject test.