©2004, 2005 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Table of Contents

1. Fundamental Concepts
   1.1. A Whirlwind Tour of UNIX and Linux
      1.1.1. Files
      1.1.2. Programs, Processes, and Threads
      1.1.3. Signals
      1.1.4. Process-IDs, Process Groups, and Sessions
      1.1.5. Permissions
      1.1.6. Other Process Attributes
      1.1.7. Interprocess Communication
   1.2. Versions of UNIX
   1.3. Using System Calls
      1.3.1. C (and C++) Bindings
      1.3.2. Other Language Bindings
      1.3.3. Guidelines for Calling Library Functions
      1.3.4. Function Synopses
   1.4. Error Handling
      1.4.1. Checking for Errors
      1.4.2. Error-Checking Convenience Macros for C
      1.4.3. Using C++ Exceptions
   1.5. UNIX Standards
      1.5.1. Evolution of the API Standards
      1.5.2. Telling the System What You Want
      1.5.3. Asking the System What It Has
      1.5.4. Checking for Options
      1.5.5. sysconf System Call
      1.5.6. pathconf and fpathconf System Calls
      1.5.7. confstr System Call
      1.5.8. Checking for a Specific OS
      1.5.9. Bonus Features
   1.6. Common Header File
   1.7. Dates and Times
      1.7.1. Calendar Time
      1.7.2. Execution Time
   1.8. About the Example Code
   1.9. Essential Resources
2. Basic File I/O
   2.1. Introduction to File I/O
   2.2. File Descriptors and Open File Descriptions
      2.2.1. Standard File Descriptors
      2.2.2. Using File Descriptors
      2.2.3. Open File Descriptions and Sharing
   2.3. Symbols for File Permission Bits
   2.4. open and creat System Calls
      2.4.1. Opening an Existing File
      2.4.2. Creating a New File
      2.4.3. Using a File as a Lock
      2.4.4. Summary of open Flags
   2.5. umask System Call
   2.6. unlink System Call
   2.7. Creating Temporary Files
   2.8. File Offsets and O_APPEND
   2.9. write System Call
   2.10. read System Call
   2.11. close System Call
   2.12. User Buffered I/O
      2.12.1. User vs. Kernel Buffering
      2.12.2. Functions for User Buffering
   2.13. lseek System Call
   2.14. pread and pwrite System Calls
   2.15. readv and writev System Calls
   2.16. Synchronized I/O
      2.16.1. Synchronized vs. Synchronous
      2.16.2. Buffer-Flushing System Calls
      2.16.3. open Flags for Synchronization
   2.17. truncate and ftruncate System Calls
3. Advanced File I/O
   3.1. Introduction
   3.2. Disk Special Files and File Systems
      3.2.1. I/O On Disk Special Files
      3.2.2. Low-Level Access to a File System
      3.2.3. statvfs and fstatvfs System Calls
      3.2.4. Mounting and Unmounting File Systems
   3.3. Hard and Symbolic Links
      3.3.1. Creating Hard Links (link System Call)
      3.3.2. Renaming a File or Directory (rename System Call)
      3.3.3. Creating Symbolic Links (symlink System Call)
   3.4. Pathnames
      3.4.1. How Long is a Pathname?
      3.4.2. getcwd System Call
   3.5. Accessing and Displaying File Metadata
      3.5.1. stat, fstat, and lstat System Calls
      3.5.2. getpwuid, getgrgid, and getlogin System Calls
      3.5.3. More on Displaying File Metadata
   3.6. Directories
      3.6.1. Reading Directories
      3.6.2. chdir and fchdir System Calls
      3.6.3. mkdir and rmdir System Calls
      3.6.4. Implementing getcwd (Walking Up the Tree)
      3.6.5. Implementing ftw (Walking Down the Tree)
   3.7. Changing an I-Node
      3.7.1. chmod and fchmod System Calls
      3.7.2. chown, fchown, and lchown System Calls
      3.7.3. utime System Call
   3.8. More File-Manipulation Calls
      3.8.1. access System Call
      3.8.2. mknod System Call
      3.8.3. fcntl System Call
   3.9. Asynchronous I/O
      3.9.1. Synchronized vs. Synchronous, Once Again
      3.9.2. AIO Control Block
      3.9.3. aio_read and aio_write
      3.9.4. aio_error and aio_return
      3.9.5. aio_cancel
      3.9.6. aio_fsync
      3.9.7. aio_suspend
      3.9.8. Example Comparing Synchronous and Asynchronous I/O
      3.9.9. lio_listio
4. Terminal I/O
   4.1. Introduction
   4.2. Reading From a Terminal
      4.2.1. Normal Terminal I/O 
      4.2.2. Non-Blocking Input
      4.2.3. select System Call
      4.2.4. poll System Call
      4.2.5. Testing and Reading a Single Input
   4.3. Sessions and Process Groups (Jobs)
      4.3.1. Terminology
      4.3.2. System Calls for Sessions
      4.3.3. System Calls for Process Groups
      4.3.4. System Calls for Controlling Terminals
      4.3.5. Using the Session-Related System Calls
   4.4. ioctl System Call
   4.5. Setting Terminal Attributes
      4.5.1. Basic tcgetattr and tcsetattr Usage
      4.5.2. Character Size, and Parity
      4.5.3. Speed
      4.5.4. Character Mapping
      4.5.5. Delays and Tabs
      4.5.6. Flow Control
      4.5.7. Control Characters
      4.5.8. Echo
      4.5.9. Punctual vs. Canonical Input
      4.5.10. Raw Terminal I/O
   4.6. Additional Terminal-Control System Calls
   4.7. Terminal-Identification System Calls
   4.8. Full-Screen Applications
   4.9. STREAMS I/O
   4.10. Pseudo Terminals
      4.10.1. Pseudo-Terminal Library
      4.10.2. Record and Playback Example
5. Processes and Threads
   5.1. Introduction
   5.2. Environment
   5.3. exec System Calls
   5.4. Implementing a Shell (Version 1)
   5.5. fork System Call
   5.6. Implementing a Shell (Version 2)
   5.7. exit System Calls and Process Termination
   5.8. wait, waitpid, and waitid System Calls
   5.9. Signals, Termination, and Waiting
   5.10. Implementing a Shell (Version 3)
   5.11. Getting User and Group IDs
   5.12. Setting User and Group IDs
   5.13. Getting Process IDs
   5.14. chroot System Call
   5.15. Getting and Setting the Priority
   5.16. Process Limits
   5.17. Introduction to Threads
      5.17.1. Thread Creation
          pthread starting function (5.17.1.)
      5.17.2. Waiting for a Thread to Terminate
      5.17.3. Thread Synchronization (Mutexes)
      5.17.4. Condition Variables
      5.17.5. Canceling a Thread
      5.17.6. Threads vs. Processes
   5.18. The Blocking Problem
      5.18.1. Solutions Using Processes and Threads
      5.18.2. Unified Event Manager Prototype
6. Basic Interprocess Communication
   6.1. Introduction
   6.2. Pipes
      6.2.1. pipe System Call
      6.2.2. Pipe (and FIFO) I/O Behavior
      6.2.3. Pipe Examples
   6.3. dup and dup2 System Calls
   6.4. A Real Shell
   6.5. Two-Way Communication with Unidirectional Pipes
   6.6. Two-Way Communication with Bidirectional Pipes
7. Advanced Interprocess Communication
   7.1. Introduction
   7.2. FIFOs, or Named Pipes
      7.2.1. Creating a FIFO
      7.2.2. A Simple FIFO Example
      7.2.3. FIFOs Critiqued
   7.3. An Abstract Simple Messaging Interface (SMI)
      7.3.1. SMI Types and Functions
      7.3.2. Example Server and Client Using SMI
      7.3.3. FIFO Implementation of SMI
   7.4. System V IPC (Interprocess Communication)
      7.4.1. System V IPC Objects
      7.4.2. Identifiers, Keys, and the ftok System Call
      7.4.3. System V IPC Ownership and Permissions
      7.4.4. System V IPC Utilities
   7.5. System V Message Queues
      7.5.1. System V Message-Queue System Calls
      7.5.2. System V Message-Queue Limits
      7.5.3. System V Message-Queue Implementation of SMI
      7.5.4. System V Message Queues Critiqued
   7.6. POSIX IPC
      7.6.1. POSIX IPC History
      7.6.2. POSIX IPC Names
      7.6.3. POSIX IPC Feature-Test Macros
      7.6.4. POSIX IPC Utilities
   7.7. POSIX Message Queues
      7.7.1. POSIX Message-Queue System Calls
      7.7.2. POSIX Message-Queue Implementation of SMI
      7.7.3. POSIX Message Queues Critiqued
   7.8. About Semaphores
      7.8.1. Basic Semaphore Usage
      7.8.2. Implementing Semaphores With Files and Messages
   7.9. System V Semaphores
      7.9.1. System V Semaphore System Calls
      7.9.2. Simple Semaphore Interface
   7.10. POSIX Semaphores
      7.10.1. Named POSIX Semaphores
      7.10.2. Un-Named POSIX Semaphores
      7.10.3. System V and POSIX Semaphores Critiqued
      7.10.4. Process-Shared Mutexes and Read-Write Locks
   7.11. File Locking
      7.11.1. A Bad Example
      7.11.2. Using a Semaphore as a File Lock
      7.11.3. lockf System Call
      7.11.4. fcntl System Call for File Locking
      7.11.5. Advisory and Mandatory Locks
      7.11.6. High-Performance Database Locking
   7.12. About Shared Memory
   7.13. System V Shared Memory
      7.13.1. System V Shared-Memory System Calls
      7.13.2. Shared Memory and Semaphores
      7.13.3. System V Shared-Memory Implementation of SMI
      7.13.4. System V Shared Memory Critiqued
   7.14. POSIX Shared Memory
      7.14.1. POSIX Shared-Memory System Calls
      7.14.2. POSIX Shared-Memory Implementation of SMI
      7.14.3. POSIX Shared Memory Critiqued
   7.15. Performance Comparisons
8. Networking and Sockets
   8.1. Socket Basics
      8.1.1. How Sockets Work
      8.1.2. Basic System Calls For Connected Sockets
      8.1.3. Handling Multiple Clients
      8.1.4. Byte Order
   8.2. Socket Addresses
      8.2.1. Socket-Address Structures
      8.2.2. AF_UNIX Socket Addresses
      8.2.3. AF_INET Socket Addresses
      8.2.4. AF_INET6 Socket Addresses
      8.2.5. The Domain Name System (DNS)
      8.2.6. getaddrinfo
      8.2.7. gethostname
   8.3. Socket Options
   8.4. Simple Socket Interface (SSI)
      8.4.1. SSI Function Calls
      8.4.2. A Brief Introduction to HTTP
      8.4.3. SSI Web Browser
      8.4.4. SSI Web Server
      8.4.5. SSI Implementation
   8.5. Socket Implementation of SMI
   8.6. Connectionless Sockets
      8.6.1. About Datagrams
      8.6.2. sendto and recvfrom System Calls
      8.6.3. sendmsg and recvmsg
      8.6.4. Using connect with Connectionless Sockets
   8.7. Out-of-Band Data
   8.8. Network Database Functions
      8.8.1. Host Functions
      8.8.2. Network Functions
      8.8.3. Protocol Functions
      8.8.4. Service Functions
      8.8.5. Network Interface Functions
   8.9. Miscellaneous System Calls
      8.9.1. send and recv
      8.9.2. getsockname and getpeername
      8.9.3. socketpair
      8.9.4. shutdown
      8.9.5. inet_ntop and inet_pton
   8.10. High Performance Considerations
9. Signals and Timers
   9.1. Signal Basics
      9.1.1. Introduction to Signals
      9.1.2. A Signal’s Lifecycle
      9.1.3. Types of Signals
      9.1.4. Interrupted System Calls
      9.1.5. Managing the Signal Mask
      9.1.6. sigaction System Call
      9.1.7. Signal Handlers
      9.1.8. Minimal Defensive Signal Handling
      9.1.9. Generating a Signal Synthetically
      9.1.10. Effect of fork, pthread_create, and exec on Signals
   9.2. Waiting for a Signal
      9.2.1. pause System Call
      9.2.2. sigwait System Call
      9.2.3. sigsuspend System Call
   9.3. Miscellaneous Signal System Calls
   9.4. Deprecated Signal System Calls
          sigset – set signal action (9.4.)
   9.5. Realtime Signals Extension (RTS)
      9.5.1. RTS Signal Handlers
      9.5.2. RTS Signals
      9.5.3. Queued Signals
      9.5.4. sigqueue
      9.5.5. sigwaitinfo and sigtimedwait
      9.5.6. sigevent Structure and SIGEV_THREAD
   9.6. Global Jumps
   9.7. Clocks and Timers
      9.7.1. alarm System Call
      9.7.2. sleep System Call
      9.7.3. Higher-Resolution Sleeping
      9.7.4. Basic Interval-Timer System Calls
      9.7.5. Realtime Clocks
      9.7.6. Advanced Interval-Timer System Calls
Appendix A: Process Attributes
Appendix B: Ux: A C++ Wrapper for Standard UNIX Func-tions
Appendix C: Jtux: A Java/Jython Interface to Standard UNIX Functions
Appendix D: Alphabetical and Categorical Function Lists