POSIX
POSIX (Portable Operating System Interface) is an IEEE and Open Group standard designed to ensure source-code portability across Unix-like operating systems, including Linux, macOS, and the BSDs. By defining a universal interface between applications and the operating system kernel, POSIX enables developers to write programs in C or C++ that compile and run seamlessly across different systems without requiring platform-specific code changes.
Beyond signals and core system calls, POSIX standardizes the complete software runtime environment through four main volumes: Base Definitions, System Interfaces (C APIs), Commands & Utilities (CLI/Shell), and Rationale. Together, these specifications cover everything from process execution and multi-threading (pthreads) to memory management (mmap), inter-process communication, network sockets, file system semantics, and standard command-line tools like ls, grep, and the POSIX shell (/bin/sh).
Process Management & Execution Models
- Process Lifecycle: System calls for creating, replacing, and reaping processes (
fork(),vfork(),execve(),wait(),waitpid()). - Threads (pthreads): The POSIX threads standard (IEEE 1003.1c) specifies thread creation (
pthread_create), lifetime management (pthread_join,pthread_detach), and synchronization primitives:- Mutexes:
pthread_mutex_lock,pthread_mutex_unlock - Condition Variables:
pthread_cond_wait,pthread_cond_signal - Read-Write Locks:
pthread_rwlock_rdlock,pthread_rwlock_wrlock
- Mutexes:
- Scheduling: Real-time priority scheduling policies (
SCHED_FIFO,SCHED_RR,SCHED_OTHER) and nice values (nice(),sched_setscheduler()).
File Systems, Memory & I/O Architecture
- File Operations & File Descriptors: File creation, read/write primitives, symbolic/hard links, and metadata (
open,read,write,close,lseek,stat,chmod,link,symlink,readlink). - Asynchronous & Multiplexed I/O: Mechanisms for monitoring file descriptor readiness (
select(),poll()) and non-blocking asynchronous I/O (aio_read(),aio_write()). - Virtual Memory Management: Memory mapping, permission controls, and locking (
mmap,munmap,mprotect,madvise,mlock,msync). - Directory Semantics: Path hierarchy rules (e.g.,
/root,.current,..parent, max path length limits) and directory navigation (opendir,readdir,mkdir,rmdir).
Inter-Process Communication (IPC)
Beyond standard signals, POSIX defines several key IPC mechanisms:
- Pipes and FIFOs: Unidirectional data streams (
pipe(),mkfifo()). - POSIX Semaphores: Named and un-named counting semaphores (
sem_open,sem_wait,sem_post). - POSIX Shared Memory: High-speed shared memory objects mounted via file-like APIs (
shm_open,shm_unlink). - Message Queues: Priority-based message queues (
mq_open,mq_send,mq_receive).
Networking & Socket APIs
- Berkeley Sockets API: Standardizes transport and network layer primitives (
socket(),bind(),listen(),accept(),connect(),send(),recv()). - Network Name Resolution: Domain name lookup functions (
getaddrinfo(),getnameinfo(),freeaddrinfo()).
System Utilities & Shell Command Language
POSIX defines the exact behavior, options, and exit statuses for command-line execution:
- POSIX Shell Specification: Rules for standard shell grammar (e.g.,
/bin/sh), subshells, variable expansion, parameter parsing, pipes, and control flow. It strictly excludes non-standard extensions ("bashisms"). - Core Utilities: Standard flags and behaviors for ubiquitous utilities like
ls,grep,cd,cat,find,sed,awk,cp,mv, andrm. - Regular Expressions: Standardized specs for Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE), backed by the C regex library (
regcomp(),regexec()).
System Environment & C Library Standard
- C Standard Library Extensions: Extends the ISO C standard with POSIX-specific header files (
<unistd.h>,<sys/types.h>,<fcntl.h>,<pthread.h>,<termios.h>). - Environment & Time Management: Environment variables (
PATH,HOME,TZ), process limits (getrlimit,setrlimit), and high-resolution clocks (clock_gettime,nanosleep). - Locale & Internationalization: Character set definitions, locale environment handling (
LC_ALL,LANG), and string transformation APIs (setlocale,iconv).
What POSIX is NOT?
POSIX strictly defines the contract between application programs and the user-space C library/runtime environment—it explicitly avoids defining how the operating system kernel works internally.
What POSIX Defines vs. Ignores
+-------------------------------------------------------------+
| User Application |
+-------------------------------------------------------------+
│
POSIX Standard Boundary (APIs & Behaviors)
│
▼
+-------------------------------------------------------------+
| C Standard Library (libc / POSIX) |
| e.g., pthread_create(), open(), malloc(), printf() |
+-------------------------------------------------------------+
│
Kernel ABI Boundary (OS-Specific Syscalls)
│
▼
+-------------------------------------------------------------+
| Kernel Internals |
| (Linux, XNU/macOS, FreeBSD, Illumos, Windows Subsystem) |
| - Scheduler logic & thread representations |
| - Virtual Memory & Page Table structures |
| - Filesystem layout & disk drivers |
| - System Call numbers and CPU traps |
+-------------------------------------------------------------+
What POSIX Defines (User-Facing)
- Function Signatures & Headers: Expects
#include <unistd.h>,#include <pthread.h>, and standard signatures likeint open(const char *path, int flags, ...). - Behavioral Expectations: Dictates what happens when a call succeeds or fails (e.g., "If
open()fails because the file doesn't exist, seterrnotoENOENT"). - Process / Thread Semantics: Defines expected concepts from a programmer's perspective—signals, process IDs (PIDs), file descriptors, standard streams (
stdin,stdout,stderr).
What POSIX Ignores (Kernel-Facing)
- System Calls: Does not specify syscall names, numbers, registers, or CPU interrupt instructions.
- Kernel Data Structures: Has no concept of Linux's
task_struct,struct page,epollitems, or VFSdentrytrees. - Internal Scheduling: Does not dictate how the kernel scheduler runs threads (e.g., completely fair scheduler vs. round-robin) or whether threads are implemented as kernel tasks, light-weight processes (LWPs), or fiber abstractions.
- Memory Management: Doesn't care how page tables (x86 4-level paging vs. ARM page tables) or page replacement algorithms work inside the MMU.
Real-World Example: Reading a File
When you write POSIX C code:
#include <unistd.h>
ssize_t bytes_read = read(fd, buffer, count);
- POSIX specifies:
read()takes a file descriptor, buffer, and byte count, returning the number of bytes read or-1on error. - Linux implements:
glibcissuessys_read(orsys_pread64), jumping into the Linux Virtual File System (VFS), traversing page caches, invoking ext4 or btrfs block drivers, and waking the thread scheduler. - macOS implements:
libSystemissues a BSD trap into the XNU kernel, invoking APFS (Apple File System) drivers.
Both operating systems are POSIX-compliant, even though their internal kernels, filesystems, and syscall implementations are completely different.