Unit 6. Linux Operating System
Unit 6. Linux Operating System
• POSIX Compliance
▫ Linux is designed to comply with POSIX standards (common OS
functionality, threads, real-time operations).
▫ Several distributions have achieved official POSIX certification.
• Standards Support
▫ Linux supports key standards like POSIX threading extensions (Pthreads)
and a subset of POSIX real-time process control extensions.
Layers in a Linux System
Components of a Linux System
• Kernel
▫ The kernel is responsible for maintaining all the important abstractions of
the operating system, including such things as virtual memory and
processes.
• System libraries
▫ The system libraries define a standard set of functions through which
applications can interact with the kernel.
Components of a Linux System
• System utilities
▫ The system utilities are programs that perform individual, specialized
management tasks.
Structure of Linux Kernel
Linux Booting Process
• BIOS/UEFI Initialization
▫ BIOS (Basic Input/Output System) or UEFI (Unified Extensible Firmware
Interface) initializes when the system powers on.
▫ It performs a POST (Power-On Self Test) to check hardware components
like RAM, keyboard, etc., and identifies the bootable device.
▫ Once the bootable device (like the hard drive) is identified, BIOS/UEFI
hands control over to the bootloader by loading the boot sector.
Linux Booting Process
• In case of GRUB
▫ GRUB allows you to choose between multiple operating systems or
different kernel versions.
▫ It loads the selected kernel and the initial RAM disk (initrd/initramfs) into
memory.
Linux Booting Process
• Kernel Loading
▫ The Linux kernel is loaded into memory and starts its execution.
▫ The kernel sets up the environment by initializing memory management,
device drivers, and other system resources.
▫ The kernel mounts the root filesystem (/) as read-only.
Linux Booting Process
• initramfs/initrd
▫ initramfs (initial RAM filesystem) or initrd (initial RAM disk) is a temporary
root filesystem loaded into memory.
▫ It contains essential drivers and executables needed to mount the actual
root filesystem.
▫ Once the root filesystem is mounted, initramfs/initrd is no longer needed
and is removed.
Linux Booting Process
• Login Prompt
▫ Once the system reaches the desired target, the login prompt (or display
manager for GUI systems) is presented, allowing users to log in and use the
system.
Kernel Modules
• Dynamic Loading/Unloading
▫ Kernel modules can be loaded into the kernel when needed using the
insmod or modprobe command.
▫ They can also be unloaded using the rmmod command.
Key Features of Kernel Modules
• Driver Abstraction
▫ Many hardware device drivers are implemented as kernel modules,
allowing for easy hardware driver updates without requiring a kernel
recompilation.
Common Use Cases for Kernel Modules
• Device Drivers: Modules are often used to load drivers for devices like
network cards, USB peripherals, or storage devices.
• File System Support: New file systems (e.g., ext4, NTFS, XFS) can be loaded as
modules.
• Network Protocols: Support for additional network protocols or features like
IPv6 or VPN support can be modularized.
• Security Features: Additional security measures or firewall modules (e.g.,
Netfilter) can be loaded on demand.
Managing Kernel Modules
• Parent and Child Processes: Every process in Linux has a parent except
the initial process, init (or systemd in modern Linux distributions), which
is the first process started by the kernel.
• Process ID (PID): Each process is identified by a unique PID.
• Orphaned and Zombie Processes: An orphaned process occurs when its
parent terminates, and a zombie process occurs when a child process
has finished execution but has not been cleaned up by its parent.
/proc Filesystem
• Scheduling in the Linux operating system refers to the way the kernel
manages the allocation of CPU time to various processes.
• Linux uses a preemptive multitasking model, meaning that the system
can interrupt a running process to give another process a chance to
execute, ensuring that all tasks receive fair CPU time.
• Linux scheduling involves different policies and algorithms depending on
the type of task, whether it is a normal or real-time process.
Process Priorities
• Linux processes are divided into scheduling classes, which dictate how they are
scheduled. The main classes are
▫ Normal Scheduling (SCHED_NORMAL): Also known as SCHED_OTHER, this is the default
class for regular processes using the Completely Fair Scheduler (CFS).
▫ Real-Time Scheduling
SCHED_FIFO (First In, First Out): Real-time processes are queued in a first-come-first-served
manner. A process runs until it either voluntarily gives up the CPU or is preempted by a higher-
priority process.
SCHED_RR (Round-Robin): Similar to SCHED_FIFO but with time slicing, where processes of the
same priority are given time slices to execute in turn.
Linux Scheduling Classes
• Linux allows users and administrators to select different scheduling policies for
different types of tasks, offering flexibility depending on the nature of the application
▫ SCHED_OTHER (SCHED_NORMAL): The default policy for regular processes.
▫ SCHED_BATCH: For non-interactive tasks that don't need frequent CPU attention.
▫ SCHED_IDLE: For processes that should only run when no other processes need the CPU.
▫ SCHED_FIFO: A real-time, priority-based scheduling policy with no time slicing.
▫ SCHED_RR: A real-time, round-robin policy with time slicing.
Interactive vs Non-Interactive Tasks
• Interactive Tasks
▫ Tasks that require user interaction, such as GUI applications. They need low-latency
scheduling to respond quickly to user input.
▫ CFS gives preference to these tasks by dynamically adjusting their priority to
improve responsiveness.
• Batch/Background Tasks
▫ Non-interactive, CPU-intensive tasks that do not require immediate attention.
▫ These tasks are scheduled with lower priority to avoid disrupting interactive
processes.
Load Balancing
• Each process has its own virtual memory space, divided into Virtual
Memory Areas (VMAs). These VMAs represent different regions of a
process’s memory
▫ Text Segment: Contains the executable code.
▫ Data Segment: Stores global and static variables.
▫ Heap: Dynamically allocated memory during runtime (e.g., malloc).
▫ Stack: Holds local variables and function call data.
Memory Allocation
• Buddy System
▫ Linux uses a buddy system for managing physical memory allocation.
▫ It divides free memory into blocks of different sizes (buddies), which can be
split or merged to accommodate memory requests of various sizes.
• Slab Allocator
▫ The slab allocator is used for managing memory for kernel objects.
▫ It preallocates memory chunks of commonly used sizes to speed up kernel
operations and avoid frequent allocation and deallocation.
Page Tables and Translation Lookaside Buffer (TLB)
• Page Tables
▫ The kernel maintains page tables for each process to map virtual addresses to physical
memory addresses.
▫ Page tables are hierarchical, allowing efficient memory management even with large
address spaces.
• TLB
▫ The Translation Lookaside Buffer is a cache that stores recent virtual-to-physical address
translations, reducing the need for repeated page table lookups and improving memory
access speed.
Shared Memory
• The kernel itself requires memory for managing hardware, system resources, and
drivers.
▫ Kernel Stack: Each process has its own kernel stack used during system calls.
▫ Kernel Slab Cache: For managing frequent small memory allocations for kernel
objects.
▫ High Memory: On systems with more memory than can be directly addressed by the
CPU (e.g., 32-bit systems), a portion of memory is treated as high memory, used
indirectly through temporary mappings.
Memory Management Tools
• Page Cache
▫ The Linux kernel uses page cache to buffer frequently accessed files in
memory, speeding up file access.
▫ Cached pages are automatically freed if memory is needed by other
processes.
• Buffers
▫ Used for buffering block device operations (e.g., disk I/O).
File Systems
• XFS
▫ High-performance journaling file system.
▫ Suitable for large files and high-throughput workloads, often used in enterprise and
server environments.
▫ Key Features
Highly scalable, supporting very large file systems (up to 8 EiB).
Excellent for handling large files, such as databases and multimedia.
Efficient metadata operations.
Common Linux File Systems
• Many modern file systems in Linux (e.g., Ext4, XFS) use journaling to
improve reliability and prevent data corruption
• Journal: A special area where file system changes are first written
before being applied.
• In case of a system crash or power failure, the journal helps recover the
file system to a consistent state.
Virtual File Systems (VFS)
• The Linux kernel uses the Virtual File System (VFS) to provide a uniform
interface for different file systems.
• VFS allows the kernel to support multiple file system types (e.g., Ext4,
XFS, NFS) transparently, without changing how user programs interact
with files.
Special File Systems
• procfs (/proc)
▫ Virtual file system.
▫ Provides information about system processes and kernel parameters.
▫ Key Features
Contains files that represent the state of the kernel and system processes
(e.g., /proc/cpuinfo, /proc/meminfo).
Used to monitor system performance and behaviour.
Special File Systems
• sysfs (/sys)
▫ Virtual file system.
▫ Exposes information about kernel devices and drivers.
▫ Key Features
Shows information about hardware devices and their configurations.
Helps manage hardware via user-space applications.
Special File Systems
• tmpfs
▫ RAM-based file system.
▫ Temporary storage that resides in volatile memory (RAM).
▫ Key Features
Files stored in tmpfs disappear after a reboot.
Often used for /tmp, /run, and other temporary directories.
Network File Systems
• In Linux, input and output (I/O) refers to the way the system handles
data flowing between programs and devices, such as keyboards, mice,
screens, files, and network interfaces.
• Linux, being a Unix-like operating system, treats nearly everything as a
file, including hardware devices and I/O streams.
• This unified abstraction allows for efficient and consistent I/O
management.
Standard Input, Output, and Error
• Standard Input (stdin)
▫ File descriptor: 0
▫ Purpose: Receives input from the user or another program.
▫ Default source: Keyboard.
• Standard Output (stdout)
▫ File descriptor: 1
▫ Purpose: Sends normal output from a program.
▫ Default destination: Terminal/screen.
• Standard Error (stderr)
▫ File descriptor: 2
▫ Purpose: Sends error messages and diagnostics.
▫ Default destination: Terminal/screen (separate from stdout).
Redirection
• Output Redirection
▫ >: Redirects stdout to a file (overwrites the file).
▫ >>: Redirects stdout to a file (appends to the file).
• Input Redirection
▫ <: Redirects stdin from a file.
Redirection
• Error Redirection
▫ 2>: Redirects stderr to a file.
• Programs and system calls use file descriptors to read, write, and
manipulate open files or streams.
I/O on Devices
• Devices in Linux are represented as files in the /dev directory.
• Input/output operations on hardware devices (like disks, keyboards, and printers) are done through
these device files.
• Character Devices
▫ These devices handle data streams byte by byte, such as keyboards or serial ports.
▫ Example: /dev/ttyS0 for a serial port.
• Block Devices
▫ Block devices handle data in fixed-size blocks, like hard drives and USB storage.
▫ Example: /dev/sda for a hard disk.
• I/O operations on these device files use standard system calls like open(), read(), write(), and close().
File I/O Operations
• Opening Files
▫ The open() system call opens a file and returns a file descriptor.
• Closing Files
▫ The close() system call closes a file descriptor when it is no longer needed.
Network I/O
• Linux has several I/O schedulers that determine how disk I/O requests are
managed and optimized.
▫ CFQ (Completely Fair Queuing): Provides balanced I/O access for all
processes.
▫ Deadline: Ensures I/O requests are served within a certain deadline, reducing
latency.
▫ Noop: A minimal scheduler that uses FIFO (First-In-First-Out), useful for SSDs
where reordering requests has little benefit.
Inter-process Communication