Case Study - Linux - Part 1
Case Study - Linux - Part 1
Part I
Mostly taken from:
Silberschatz – Chapter 20
Nutt – Chapter 20
The Linux System
*Linux History
*Design Principles
*Kernel Modules
*Process Management
*Scheduling
*Memory Management
*File Systems
*Input and Output
*Interprocess Communication
*Network Structure
*Security
History
*Linux is a modern, free operating system based on UNIX standards
*First developed as a small but self-contained kernel in 1991 by Linus Torvalds, with the major design
goal of UNIX compatibility exclusively on PC platform
*It has been designed to run efficiently and reliably on common PC hardware, but also runs on a variety
of other platforms
*The core Linux operating system kernel is entirely original, but it can run much existing free UNIX
software, resulting in an entire UNIX-compatible operating system free from proprietary code
The Linux System
*Linux uses many tools developed as part of Berkeley’s BSD operating system, MIT’s X Window
System, and the Free Software Foundation's GNU project
*Linux networking-administration tools were derived from 4.3BSD code; later BSD derivatives such as
Free BSD have borrowed code from Linux in return
*The Linux system is maintained by a loose network of developers collaborating over the Internet, with a
small number of public ftp sites acting as de facto standard repositories
Linux Licensing
*The Linux kernel is distributed under the GNU General Public License (GPL), the terms of which are set
out by the Free Software Foundation
*Anyone using Linux, or creating their own derivative of Linux, may not make the derived product
proprietary; software released under the GPL may not be redistributed as a binary-only product
The Linux Kernel
*Version 0.01 (May 1991) had no networking, ran only on 80386-compatible Intel processors and on PC
hardware, had extremely limited device-driver support, and supported only the MINIX file system
*2.4 and 2.6 increased Symmetric Multiprocessing (SMP) support, added journaling file system,
preemptive kernel, 64-bit memory support
Design Principles
*Linux is a multiuser, multitasking system with a full set of UNIX-compatible tools
*Its file system adheres to traditional UNIX semantics, and it fully implements the standard UNIX
networking model
*Main design goals are speed, efficiency, and standardization
*Linux is designed to be compliant with the relevant POSIX documents; at least two Linux distributions
have achieved official POSIX certification
*The Linux programming interface adheres to the SVR4 UNIX semantics, rather than to BSD behavior
Components of a Linux System
*Like most UNIX implementations, Linux is composed of three main bodies of code
*Kernel
*System libraries
*System utilities
*The most important distinction between the kernel and the other components
Components of a Linux System
*The kernel is responsible for maintaining the important abstractions of the operating system
*Kernel code executes in kernel mode with full access to all the physical resources of the
computer
*All kernel code and data structures are kept in the same single address space
Components of a Linux System
*The system libraries define a standard set of functions through which applications interact with the
kernel, and which implement much of the operating-system functionality that does not need the full
privileges of kernel code – for example sorting
*The system utilities perform individual specialized management tasks – for example configure network
devices, daemons, etc.
The Linux Kernel
*Designed and implemented as a monolithic kernel
*Process and resource management, memory management, and file management are implemented within
a single executable software module.
*Data structures for each aspect are available to all – single address space for the entire kernel code
including device drivers.
*Main concern: performance
*No context switching
OS Kernel Organization
The Linux Kernel – using kernel services
*The kernel as an ADT
*Whenever a user process desires a service from the OS, it simply invokes the appropriate OS function
through POSIX.1 system call interface.
*If the function is implemented in a DD or Module, the user call is passed through an internal interface to
the appropriate DD or module.
*The process executing the user program also executes the kernel program when a system call is made
*Single-threaded kernel – kernel functions are normally executed as if they were in a critical section.
*Daemons are separate processes that operate on behalf of the OS.
*Interrupts can suspend the execution of a system call.
The Linux Kernel – device manager
*Implemented as a separate collection of device drivers and interrupt handlers.
*The only new kernel space software that should be added to the system should be for device
management.
*Was designed to support disk drives, keyboards, and character displays.
*With the evolution of technology it became difficult to provide kernel support
The Linux Kernel – module manager
*The new support is provided through modules
*The module interface is more general than the Device Driver interface.
*Linux module is an independent software unit that can be designed and implemented later.
*Can be compiled, loaded, and unloaded independent of the rest of the kernel
*A kernel module may typically implement a device driver, a file system, or a networking protocol
Kernel Modules
*Loadable modules run in privileged kernel mode
*The module interface allows third parties to write and distribute, on their own terms, device drivers or
file systems that could not be distributed under the GPL
*Three components to Linux module support:
*module management
*driver registration
*conflict resolution
Module Management
*Supports loading modules into memory and letting them talk to the rest of the kernel
*The module requestor manages loading requested, but currently unloaded, modules; it also regularly
queries the kernel to see whether a dynamically loaded module is still in use, and will unload it
when it is no longer actively needed
Module loading
*Reservation of continuous area in kernel memory
*Updating of kernel symbol table
Driver Registration
*Allows modules to tell the rest of the kernel that a new driver has become available
*The kernel maintains dynamic tables of all known drivers, and provides a set of routines to allow drivers
to be added to or removed from these tables at any time
*Each module must implement init_module() and cleanup_module() functions.
*A new API for the module may also be defined.
*Each new function that is added to the module must be registered/ unregistered with the kernel when the
module is installed/ uninstalled.
*Registration tables include:
*Device drivers
*File systems
*Network protocols
*Binary format
Conflict Resolution
*A mechanism that allows different device drivers to reserve hardware resources and to protect those
resources from accidental use by another driver
*Credentials.
*Each process must have an associated user ID and one or more group IDs that determine the
process’s rights to access system resources and files
*Passing environment variables among processes and inheriting variables by a process’s children are
flexible means of passing information to components of the user-mode system software
*The environment-variable mechanism provides a customization of the operating system that can be set
on a per-process basis, rather than being configured for the system as a whole
Process Context
*The (constantly changing) state of a running program at any point in time
*Scheduling context
*File table
*File system
*Signal handler
*Virtual memory
*Accounting
Process Context
*The scheduling context is the most important part of the process context; it is the information that the
scheduler needs to suspend and restart the process
*Saved copies of all the process registers
*FP registers are saved separately
*Scheduling priority
*Outstanding signals waiting to be delivered to the process
*Process’ kernel stack
*a separate area of kernel memory reserved for use exclusively by kernel-mode code.
*Used by system calls and interrupts
Process Context (Cont.)
*The file table lists the existing open files and is an array of pointers to kernel file structures
*When making file I/O system calls, processes refer to files by their index into this table
*The signal-handler table defines the routine in the process’s address space to be called when specific
signals arrive
*The virtual-memory context of a process describes the full contents of the its private address space
*The kernel maintains accounting information about the resources currently being consumed by each
process, and the total resources consumed by the process in its lifetime so far
*Threads represent separate, concurrent execution context within a single process running a single
program.
*Any two separate processes have their own independent address space even if they are using shared
memory.
*Two threads within the same process will share the same address space.
*Any change to the virtual memory layout made by one thread will be immediately visible to other
threads in the same process.
Processes and Threads
*Linux does not hold process’ entire context within the main process data structure.
*Context is held within independent sub-contexts
*File-system context, file-descriptor data structure, signal-handler table, and virtual
memory contexts
*Linux uses the same internal representation for processes and threads; a thread is simply a new process
that happens to share the same address space as its parent
*A distinction is only made when a new thread is created by the clone system call
*fork creates a new process with its own entirely new process context
*clone creates a new process with its own identity, but that is allowed to share the data structures
of its parent
*Deep versus shallow copy
*Using clone gives an application fine-grained control over exactly what is shared between two threads
*In fact, fork is a special case of clone.
Kernel Synchronization
*A request for kernel-mode execution can occur in two ways:
*A running program may request an operating system service, either explicitly via a system call,
or implicitly, for example, when a page fault occurs
*A device driver may deliver a hardware interrupt that causes the CPU to start executing a kernel-
defined handler for that interrupt
*Kernel code has to run in the critical section as all data structures are shared.
*Kernel synchronization requires a framework that will allow the kernel’s critical sections to run without
interruption by another critical section
Kernel Synchronization (Cont.)
*Linux uses two techniques to protect critical sections:
*Normal kernel code is non-preemptible
– when a time interrupt is received while a process is
executing a kernel system service routine, the kernel’s
need_resched flag is set so that the scheduler will run
once the system call has completed and control is
about to be returned to user mode
2. The second technique applies to critical sections that occur in an interrupt service
routines
– By using the processor’s interrupt control hardware to disable interrupts during a
critical section, the kernel guarantees that it can proceed without the risk of concurrent
access of shared data structures
*Interrupt service routines are separated into a top half and a bottom half.
*The top half is a normal interrupt service routine, and runs with recursive interrupts disabled
*The bottom half is run, with all interrupts enabled, by a miniature scheduler that ensures that
bottom halves never interrupt themselves
*This architecture is completed by a mechanism for disabling selected bottom halves while
executing normal, foreground kernel code
Task Control Flow
Task Control Flow
*The process traps into the kernel code to the entry point of the for the target function
*The function is executed
*Function returns to the kernel
*The kernel performs a standard set of task in ret_from_sys_call code.
*Dispatches any accumulated system work such as handling signals
*Complete the pending bottom halves
*Schedule next task
*While scheduling is normally thought of as the running and interrupting of processes, in Linux,
scheduling also includes the running of the various kernel tasks
*Running kernel tasks encompasses both tasks that are requested by a running process and tasks that
execute internally on behalf of a device driver
The Linux Scheduler
*Responsible for multiplexing the CPU among the programs in memory.
*Incorporates the scheduling policy including the priority based policy.
*Always runs as a task that is associated either with a user process or an interrupt.
*Scheduling policy for each can be set at runtime – sched_setscheduler()
Process Scheduling
*Linux uses three process-scheduling algorithms:
*A time-sharing algorithm for fair preemptive scheduling between multiple processes -
SCEHD_OTHER
*Two real-time algorithms for tasks where absolute priorities are more important than fairness
* FCFS or FIFO - SCHED_FIFO
* Round-robin - SCHED_PR
*A process’s scheduling class defines which algorithm to apply
*Whenever a process with a priority that is higher than the currently running process becomes runnable,
the lower priority process is preempted and the higher-priority process begins to run.
Process Scheduling
*For time-sharing processes, Linux uses a prioritized, credit based algorithm
*The crediting rule
*To preserve the kernel’s nonpreemptible synchronization requirements, SMP imposes the restriction, via
a single kernel spinlock, that only one processor at a time may execute kernel-mode code
Linux Memory Manager
*Demand paged virtual memory model
*Page fault handler implements
*Normal page fault
*Copy-on-write strategy for sharing pages among address spaces.
*Dynamic page allocation strategy – allocation based upon working set size
Memory Management
*Linux’s physical memory-management system deals with allocating and freeing pages, groups of pages,
and small blocks of memory
*It has additional mechanisms for handling virtual memory, memory mapped into the address space of
running processes
Kernel Memory Allocation – Goals
*Must be fast (this is crucial)
*Should minimize memory waste
*Try to avoid memory fragmentation
*Cooperate with other kernel subsystems
Managing Physical Memory
*The page allocator allocates and frees all physical pages; it can allocate ranges of physically-contiguous
pages on request
*Smallest block – 4KB
*Largest block – 128KB
*The allocator uses a buddy-heap algorithm to keep track of available physical pages
*Each allocatable memory region is paired with an adjacent partner
*Whenever two allocated partner regions are both freed up they are combined to form a larger
region
*If a small memory request cannot be satisfied by allocating an existing small free region, then a
larger free region will be subdivided into two partners to satisfy the request
Splitting of Memory in a Buddy Heap
Managing Physical Memory
*Memory allocations in the Linux kernel occur either statically (drivers reserve a contiguous area of
memory during system boot time) or dynamically (via the page allocator)
*Also uses slab allocator for kernel memory
Slab allocator
*Kernel functions tend to request objects of the same type repeatedly, such as process descriptors, file
descriptors, etc.
*Many requests are for less than a full page
*Wasteful to allocate an entire page!
*The objective is that a single page can now be used to contain a number of objects thus saving memory
and avoid internal fragmentation.
*Linux uses a ‘slab allocator’ subsystem
*The memory block contains several equal-sized ‘slabs’
*The slab allocator does not discard objects, but caches them
Slab Allocator
*Divide blocks in to “large” and “small” by picking an arbitrary threshold size.
*Bookkeeping for small blocks is relatively easy: just use a bitmap for each range of blocks of the same
size
*Allocating is easy and fast: compute the size of the block to allocate and find a free bit in the
corresponding bitmap.
*Freeing is also easy and fast: figure out which slab the address belongs to and clear the corresponding
bit.
Slab Allocator