CMPSC 311 - Introduction to Systems Programming
UNIX Origins
- Multics project started in 1964 by MIT, General Electric, and Bell Labs.
- Though the project failed, it generated numerous valuable concepts.
- Thompson and Ritchie worked on Multics and later ported "Space Travel" to PDP-7.
- They built tools and a file system for PDP-7, leading to the creation of UNIX.
- Key attributes of UNIX:
- Multiuser: Supports multiple users simultaneously, each with their own terminal.
- Multitasking: Supports multiple programs running concurrently.
- Portability: Only the lowest software layers need reimplementation when moving to different hardware.
UNIX Variants
- (A visual representation of UNIX variants was shown, but specific details aren't provided in the text.)
Linux Origins
- GNU (GNU is Not UNIX) is a free software project initiated by Richard Stallman in 1983.
- It produced essential tools like gcc, gdb, bash, and emacs.
- GNU also developed an operating system kernel called Hurd, but it's not widely used.
- Linux is an operating system kernel.
- A Linux distribution comprises the Linux kernel, user tools, and a desktop environment.
- Linus Torvalds announced the project:
- He was developing a free operating system (a hobby project, not intended to be large or professional like GNU) for 386/486 AT clones.
Linux Software Layers
- OS kernel: Direct interaction with hardware/firmware.
- System calls: Interface to the kernel.
- System libraries: Wrappers around system calls.
- Programming language libraries: Extends system libraries.
- System utilities: Application-independent tools (e.g., fsck, fdisk, ifconfig, mknod, mount, nfsd).
- Command interpreter/shell: User interface (in a terminal program).
- Application libraries: Application-specific tools.
- Applications: Complete programs for general users.
- Some applications have their own command shells and programming languages (e.g., Perl, Python).
Linux Distributions
- Semi-Commercial systems emerged since 1991, including Red Hat, SUSE/Novell, Caldera (defunct, SCO), Debian, Mandrake/Mandriva, Slackware, Gentoo, Ubuntu, Knoppix, Fedora.
- distrowatch.com provides a list of Linux distributions.
- Android, based on Linux, appeared in 2003.
Open Source
- Many software systems today are distributed as open source.
- Open-source software is licensed to allow users to review, modify, and distribute the source code without cost.
- Variations exist regarding the ability to charge for derived software or prohibiting charges for derivative works.
- The concept of "free beer" (gratis) is contrasted with "free speech" (libre).
Operating Systems
- Software that:
- Interacts directly with hardware.
- The OS is trusted to do this; user-level programs aren't.
- The OS needs to be ported to new hardware; user-level programs are portable.
- Manages hardware resources (allocates, schedules, protects).
- It decides which programs can access specific files, memory, screen pixels, and when.
- Abstracts away hardware devices.
- Provides high-level, convenient, portable abstractions (e.g., files vs. disk blocks).
- The OS is essentially a program running directly on the hardware.
UNIX as an Abstraction Provider
- The OS is the "layer below."
- A module your program can call (using system calls).
- Provides a powerful API (the POSIX API).
- Examples of OS API:
- File system:
open(), read(), write(), close() - Network stack:
connect(), listen(), read(), write() - Virtual memory:
brk(), shm_open() - Process management:
fork(), wait(), nice()
UNIX as a Protection System
- The OS isolates processes from each other.
- Permits controlled sharing through shared name spaces (e.g., file system names).
- The OS isolates itself from processes.
- Prevents processes from directly accessing hardware.
- The OS is allowed to access the hardware.
- User processes run in unprivileged (user) mode.
- The OS runs in privileged (kernel) mode.
- User-level processes invoke system calls to safely enter the OS.
Hardware Privilege Modes
- A privilege mode is a hardware state that restricts the operations that code can perform (e.g., prevents direct hardware access).
- Two main modes:
- User mode: Normal programs running with low privilege (also system services in "user space").
- Kernel mode: The operating system running.
- Unrelated to superuser (root, administrator) privileges.
Device Drivers
- A device driver is a software module that implements the interface to real or virtual hardware.
- Examples: printers, monitors, graphics cards, USB devices.
- Often provided by the device manufacturer.
- Drivers commonly run within the OS kernel for performance reasons (in kernel space).
- Historically, drivers were compiled directly into the kernel, requiring recompilation when a new device type was introduced.
- Each system had a different kernel.
Recompiling Kernels?
- Recompilation of the kernel is problematic:
- Time-consuming.
- Requires expertise.
- Versioning issues.
- Solution 1: User-space modules
- Creating user-space programs that support the OS.
- Leverages protection against buggy code.
- Allows independent patching and upgrading.
- Reduces dependency on the kernel version (mostly).
- Problem: performance; interacting with user space is slower than in-kernel operations.
- Solution 2: Kernel modules (loadable kernel modules)
- Software modules that run in kernel space and can be loaded/unloaded on a running system.
- Extends kernel functionality without recompilation.
- The kernel provides generic interfaces (APIs) for modules to communicate with it.
- Used by almost every modern OS (OSX, Windows, etc.).
- Tip: Use the
lsmod command to see running modules on a UNIX system.