0% found this document useful (0 votes)
48 views

Unit 6. Linux Operating System

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Unit 6. Linux Operating System

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 92

Operating Systems Fundamentals

LINUX Operating System

Dr. Akshay Bhosale

Symbiosis Institute of Technology, Pune


Linux Design Principles
• Traditional UNIX-Like System
▫ Linux resembles traditional, non-microkernel UNIX implementations.
▫ It is a multiuser, preemptively multitasking system with full UNIX-
compatible tools.
• File System and Networking Model
▫ The file system follows traditional UNIX semantics.
▫ The standard UNIX networking model is fully implemented.
Linux Design Principles

• Influence of Development History


▫ Heavily influenced by its early development on PC architecture by
individual enthusiasts.
▫ Emphasized efficient use of limited resources in its early stages.

• Efficient on Limited Resources


▫ Initially designed to operate efficiently on systems with minimal resources.
▫ Capable of running on high-end hardware today but maintains backward
Linux Design Principles

• Growth with PC Hardware


▫ As PC hardware improved, Linux kernels expanded to implement more
UNIX functionality.
• Design Goals
▫ Speed and efficiency remain core goals.
▫ A third key goal is standardization, to ensure compatibility with various
UNIX systems and environments.
Linux Design Principles

• POSIX Compliance
▫ Linux is designed to comply with POSIX standards (common OS
functionality, threads, real-time operations).
▫ Several distributions have achieved official POSIX certification.

• Standard UNIX Interfaces


▫ Linux provides standard interfaces for programmers and users, similar to
UNIX, reducing surprises for those familiar with UNIX systems.
Linux Design Principles

• SVR4 Semantics by Default


▫ Linux follows SVR4 UNIX semantics by default, but also provides libraries for
BSD semantics where needed.
• Certification Challenges
▫ Full certification with all UNIX standards is often delayed due to the high costs
of certification.
▫ Despite this, Linux strives to implement relevant standards to support a broad
range of applications.
Linux Design Principles

• 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

• MBR/GPT and Bootloader Execution


▫ MBR (Master Boot Record) or GPT (GUID Partition Table) contains the
partition table and the bootloader.
▫ The bootloader is a small program responsible for loading the Linux kernel
into memory.
▫ Common bootloaders include GRUB (GRand Unified Bootloader) or LILO
(Linux Loader).
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

• Systemd or init (Initial Process)


▫ The kernel starts the first user-space process, typically init (PID 1), which used to
be the SysV init system.
▫ Nowadays, most modern Linux distributions use systemd.
▫ Systemd manages the boot process and services, initializing the system by starting
necessary daemons, mounting filesystems, and enabling network configurations.
▫ Systemd works with targets, where each target represents a specific mode of
operation (like multi-user mode, graphical mode, etc.).
Linux Booting Process

• Runlevel or Target Setup


▫ The system continues by reaching the specified runlevel or systemd target
(like multi-user.target or graphical.target).
▫ Necessary services and daemons are launched based on this target,
bringing the system into full operation.
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

• Kernel modules (also called loadable kernel modules, or LKMs) are


pieces of code that can be dynamically loaded or unloaded into the
Linux kernel at runtime.
• They extend the functionality of the kernel without requiring a full
reboot, enabling features like device drivers, file system drivers, and
network protocols to be added or removed as needed.
Key Features of 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

• Extends Kernel Functionality


▫ They allow the kernel to support additional hardware devices or
filesystems, or to add extra features like new system calls or network
protocols.
▫ This helps keep the base kernel small and efficient, loading only necessary
modules based on system configuration.
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

• Loading Kernel Modules


▫ insmod: This command is used to insert a module into the running kernel.
 sudo insmod <module_name>.ko

▫ modprobe: This is the more commonly used command because it resolves


dependencies and loads required modules.
 sudo modprobe <module_name>
Managing Kernel Modules

• Listing Loaded Kernel Modules


▫ lsmod: Displays a list of currently loaded kernel modules.
 lsmod

• Removing Kernel Modules


▫ rmmod: This command removes a loaded module from the kernel.
 sudo rmmod <module_name>
Managing Kernel Modules

• modprobe -r: Removes the module and resolves dependencies, if


necessary.
▫ sudo modprobe -r <module_name>

• Checking Module Information


▫ modinfo: Provides detailed information about a specific kernel module,
such as version, author, license and more.
 modinfo <module_name>
Structure of a Kernel Module

• A basic kernel module is written in C and follows a specific structure


• Initialization function: This function is called when the module is loaded
into the kernel.
▫ int init_module(void) or static int __init my_module_init(void)

• Cleanup function: This function is called when the module is removed


from the kernel.
▫ void cleanup_module(void) or static void __exit my_module_exit(void)
Advantages of Kernel Modules

• Flexibility: Modules can be loaded and unloaded without rebooting the


system, allowing for dynamic updates or driver installations.
• Memory Efficiency: Only required modules are loaded, freeing up
memory when unused features are unloaded.
• Development and Debugging: Kernel modules allow developers to test
new features or device drivers without recompiling the entire kernel.
Processes and Their Types

• Process: A running instance of a program.


• Foreground Process: Runs in the foreground and occupies the terminal
until completed.
• Background Process: Runs without occupying the terminal, allowing
other tasks to continue.
Process States

• Running: Actively executing or waiting to execute.


• Sleeping: Waiting for some resource (interruptible or uninterruptible).
• Stopped: Halted, usually by a signal or user intervention.
• Zombie: Completed execution but not yet cleaned up by its parent
process.
Important Process-Related Commands

• ps: Displays information about running processes.


▫ ps aux # Shows detailed information about all processes

• top: Provides a dynamic real-time view of running processes.


• htop: Similar to top, but with an easier-to-use interface (requires
installation).
• kill: Sends a signal to a process, usually to terminate it.
Important Process-Related Commands

• nice and renice: Adjust the priority of a process.


• bg and fg: Manage processes running in the background or foreground.
Process Scheduling
• Linux uses a preemptive multitasking system, meaning the scheduler
interrupts processes to ensure fair CPU usage.
• Scheduling Policies
▫ SCHED_NORMAL: Default for normal tasks, using the Completely Fair
Scheduler (CFS).
▫ SCHED_FIFO and SCHED_RR: Real-time scheduling policies, where
processes are scheduled in a more deterministic way.
▫ SCHED_IDLE: For processes that should only run when the system is idle.
Signals
• Signals allow communication between processes or from the OS to a
process. Common signals
▫ SIGTERM (15): Graceful termination.
▫ SIGKILL (9): Forcefully kills the process.
▫ SIGSTOP (19): Pauses a process.
▫ SIGCONT (18): Resumes a paused process.
Process Hierarchy and init Process

• 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

• The /proc filesystem is a virtual filesystem that contains information


about running processes.
▫ cat /proc/<PID>/status # View status of a process
▫ cat /proc/cpuinfo # View CPU information
Job Control

• Linux allows users to run multiple processes in the background and


bring them to the foreground when needed. Commands related to job
control include
▫ jobs: Lists background jobs.
▫ &: Appends to a command to run it in the background.
Scheduling

• 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

• Processes are assigned a priority to determine the order in which they


will be scheduled.
• Nice Value: Used to determine the priority of normal processes. The
range is from -20 (highest priority) to 19 (lowest priority). A lower nice
value means higher priority.
Linux Scheduling Classes

• 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

▫ SCHED_BATCH: For batch jobs that don't require interactive scheduling.


Designed for long-running, non-interactive tasks.
▫ SCHED_IDLE: For very low-priority background tasks that should only run
when the system is idle.
Completely Fair Scheduler (CFS)
• The Completely Fair Scheduler is the default Linux scheduler for normal tasks (SCHED_NORMAL).
• It aims to fairly allocate CPU time to all processes based on their "weight," which is derived from
their nice values.
• The CFS keeps track of the "virtual runtime" of each process and tries to ensure that each process
gets a fair share of the CPU based on its priority.
• Key features of CFS:
▫ Fairness: Ensures that each task gets a proportionate amount of CPU time.
▫ Red-Black Tree: CFS uses a red-black tree to manage runnable processes, which allows efficient
insertion and deletion of tasks.
▫ Time Slices: The time slice given to a process depends on its priority (nice value). Higher-priority
processes get longer time slices.
Real-Time Scheduling
• Linux supports real-time scheduling for tasks that require deterministic, low-latency execution.
• These tasks use SCHED_FIFO and SCHED_RR policies.
• Real-time tasks have higher priorities than normal tasks, so they are scheduled before regular
processes
▫ SCHED_FIFO
 Processes are scheduled in the order they are ready to run.
 Once a process gets the CPU, it runs until it voluntarily relinquishes the CPU, blocks, or is preempted by a
higher-priority process.
▫ SCHED_RR
 Similar to FIFO, but each process gets a fixed time quantum (time slice).
 When the time quantum expires, the next real-time process with the same priority gets a chance to run.
Scheduling Policies

• 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

• On multiprocessor systems, Linux uses load balancing to distribute tasks


across all available CPUs.
• This ensures that no single CPU is overloaded while others are idle.
• The scheduler moves processes between CPUs to balance the load,
especially in cases where tasks are CPU-bound.
Latency and Preemption
• Kernel Preemption
▫ The Linux kernel can preempt tasks to improve the responsiveness of
interactive tasks.
▫ This ensures that the system remains responsive even when performing CPU-
bound operations.
• Low-Latency Kernels
▫ Some Linux kernels are optimized for low-latency workloads, such as those
used in real-time systems or audio processing, where responsiveness is critical.
Memory Management

• Memory management in the Linux operating system is responsible for


efficiently managing system memory, including RAM and swap space,
for processes running on the system.
• The Linux kernel uses advanced techniques to allocate, manage, and
free memory, ensuring that all processes can access the resources they
need while maintaining system stability.
Memory Addressing

• Linux uses both physical and virtual memory


▫ Physical Memory: The actual RAM installed in the system.
▫ Virtual Memory: A layer of abstraction that provides processes with their
own address space. Virtual memory allows processes to use more memory
than physically available by using paging.
Paging
• Paging is the process of dividing virtual memory into fixed-size blocks
called pages (usually 4 KB in size), and similarly dividing physical memory
into page frames.
• When a process needs memory, the kernel allocates pages from the
physical memory and maps them to the process’s virtual address space.
• If there is not enough physical memory, the Linux kernel can move
inactive pages to disk storage (swap space), a process called paging out.
• When needed, the kernel retrieves these pages from disk (paging in).
Swap Space

• Swap space is a reserved area on the disk used to temporarily store


pages that cannot fit in physical RAM.
• Linux uses swap space to prevent out-of-memory situations when the
physical RAM is fully utilized.
• While swapping improves memory availability, accessing data from disk
is much slower than from RAM, so excessive swapping can lead to
thrashing, degrading system performance.
Virtual Memory Areas (VMAs)

• 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

• Linux allows multiple processes to share memory regions for efficient


communication.
• Shared memory avoids the overhead of copying data between processes and is
commonly used in inter-process communication (IPC).
▫ Anonymous Shared Memory: Managed by the kernel and can be created using
functions like mmap or shmget.
▫ File-Backed Shared Memory: Created by mapping files into memory (using mmap),
allowing multiple processes to share access to the same file content.
Kernel Memory Management

• 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

• free: Displays total, used, and free memory.


• top and htop: Display real-time system performance information,
including memory usage.
• vmstat: Displays memory, process, and CPU statistics.
• /proc/meminfo: Provides detailed memory information via the /proc
filesystem.
Memory Caching and Buffers

• 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

• File systems in the Linux operating system play a crucial role in


organizing, storing, and managing files and directories on storage
devices like hard drives, SSDs, and removable media.
• Linux supports multiple types of file systems, each with its own features,
optimizations, and use cases.
File System Structure
• Files
▫ Everything in Linux is treated as a file, including regular files, directories, devices, and even
network sockets.
• Directories
▫ Directories are special files that store the list of other files and directories.
▫ The entire system is organized in a hierarchical directory structure rooted at / (the root
directory).
• Mount Points
▫ Each file system is mounted to the directory tree at a specific mount point.
▫ For example, a file system might be mounted at /home or /mnt/usb.
Common Linux File Systems

• Ext4 (Fourth Extended File System)


▫ Journaling file system.
▫ Default file system for most Linux distributions.
▫ Key Features
 Supports large file sizes (up to 16 TiB) and volumes (up to 1 EiB).
 Journaling capability helps prevent data corruption in case of system crashes.
 Improved performance and scalability over earlier Ext versions.
 Backward compatibility with Ext2 and Ext3.
Common Linux 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

• Btrfs (B-Tree File System)


▫ Copy-on-Write (CoW) file system.
▫ Modern file system designed for reliability, scalability, and advanced features.
▫ Key Features
 Supports snapshots and subvolumes.
 Built-in RAID support.
 Online defragmentation and file system checks.
 Advanced data integrity features.
Common Linux File Systems

• FAT32 and exFAT


▫ Non-journaling, legacy file systems.
▫ Primarily used for removable media (USB drives, SD cards) for compatibility
with non-Linux systems.
▫ Key Features
 FAT32: Limited to 4 GB maximum file size and 2 TB partition size.
 exFAT: A newer version of FAT that supports larger files and partitions.
Common Linux File Systems

• NTFS (New Technology File System)


▫ Journaling file system.
▫ Used in Windows operating systems, often found on dual-boot systems or
external drives.
▫ Key Features
 Linux can read/write NTFS through the ntfs-3g driver.
 Journaling capability ensures data integrity.
Journaling

• 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

• NFS (Network File System)


▫ Allows a system to share directories and files with other machines over a
network.
• SMB/CIFS (Server Message Block/Common Internet File System)
▫ Primarily used for sharing files with Windows systems.

• SSHFS (SSH File System)


▫ Allows access to a remote file system over SSH.
Input and Output

• 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

• Redirection allows the user to change the source or destination of


standard I/O streams.
• This is useful when you want to save output to a file, discard it, or take
input from a file instead of the terminal.
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.

• Redirect stdout and stderr together


▫ &>: Redirects both stdout and stderr to the same file.
Pipes
• Pipes (|) allow the output of one command to be used as the input for
another command.
• This is a powerful feature in Linux that allows chaining commands to create
efficient workflows.
• ls -l | grep "file" | wc -l # Count the number of files with "file" in their name
▫ ls -l: Lists files in long format.
▫ grep "file": Filters the output to show only lines containing "file."
▫ wc -l: Counts the number of lines in the filtered output.
File Descriptors

• A file descriptor is a non-negative integer that uniquely identifies an open


file or I/O resource.
▫ 0: Standard input (stdin).
▫ 1: Standard output (stdout).
▫ 2: Standard error (stderr).

• 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.

• Reading and Writing Files


▫ The read() and write() system calls read from and write to files, respectively.

• Closing Files
▫ The close() system call closes a file descriptor when it is no longer needed.
Network I/O

• Linux treats network communication in much the same way as it treats


file I/O.
• Network connections use file descriptors, and the system calls for
reading and writing to a network socket are similar to those for file I/O.
▫ Sockets: Network connections are represented as sockets in Linux.
▫ Sockets use the same system calls (read(), write(), etc.) to send and receive
data over the network.
I/O Scheduling

• 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

• Inter-process Communication (IPC) in Linux refers to mechanisms that allow


processes to exchange data and signals, coordinate actions, and share
resources efficiently.
• Since processes run in separate memory spaces, IPC provides a means for
them to communicate without direct access to each other’s memory.
• Linux offers various IPC mechanisms, each suited for different use cases, such
as message passing, shared memory, and synchronization.
Signals
• Signals are a basic form of IPC used to notify processes of events, such as
termination, errors, or user-defined signals.
• Each signal has a unique number, and processes can send and handle signals
using system calls.
▫ SIGKILL (9): Forcefully terminates a process.
▫ SIGTERM (15): Requests graceful process termination.
▫ SIGINT (2): Interrupt signal (usually from Ctrl+C in the terminal).
▫ SIGHUP (1): Sent when a terminal is closed.
▫ SIGCHLD: Sent to a parent process when a child process stops or terminates.
Pipes

• Pipes allow one-way communication between processes. They can be


used to pass output from one process as input to another.
▫ Unnamed Pipes
 Unnamed pipes are commonly used for communication between parent and
child processes created by fork(). The data flow is unidirectional (one process
writes, and the other reads).
Pipes

▫ Named Pipes (FIFOs)


 Named pipes, or FIFOs (First In, First Out), are similar to unnamed pipes but
have a persistent presence in the filesystem. Multiple unrelated processes can
use them for communication.
Message Queues

• Message queues allow processes to exchange discrete messages, which


are stored in a queue until the receiving process reads them.
• Unlike pipes, messages are not simply streams of bytes but structured
data.
• This method supports asynchronous communication (sender and
receiver do not need to interact at the same time).
Shared Memory

• Shared memory is the fastest form of IPC, allowing multiple processes to


access the same memory segment.
• However, shared memory does not provide synchronization, so
processes need to manage access through other mechanisms (e.g.,
semaphores).
Semaphores

• Semaphores are used for controlling access to shared resources and


ensuring synchronization between processes.
• They are typically used with shared memory to avoid race conditions.
Sockets
• Sockets provide a more general way for processes to communicate, especially
when they are on different machines or networks.
• Sockets can use various communication protocols, such as TCP/IP or Unix
domain sockets (for local communication)
• Types of sockets
▫ Stream Sockets (SOCK_STREAM): Provide reliable, connection-oriented
communication (e.g., TCP).
▫ Datagram Sockets (SOCK_DGRAM): Provide connectionless communication (e.g.,
Sockets

• Unix Domain Sockets


▫ Unix domain sockets provide IPC between processes on the same machine,
using the same interface as network sockets but without the overhead of
networking protocols.
• Internet Sockets
▫ Internet sockets allow communication over networks, using IP addresses
and ports.

You might also like