Operating Systems Cheatsheet

File Systems

Use this Operating Systems reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

File System Concepts

A file system organizes and stores data on persistent storage. It provides naming, organization (directories), access control, and reliability.

File: named collection of related data. From the OS view: a sequence of bytes with metadata.

File Attributes (Metadata)

AttributeDescription
NameHuman-readable string
IdentifierUnique inode/file ID within FS
TypeRegular, directory, symlink, device, …
LocationDisk block pointers
SizeIn bytes (logical) and blocks (physical)
PermissionsOwner/group/other × r/w/x
Timestampsatime (access), mtime (modify), ctime (metadata change)
Link countNumber of directory entries pointing to this inode

File Operations (POSIX)

SyscallDescription
open(path, flags, mode)Open/create file; returns fd
read(fd, buf, n)Read n bytes
write(fd, buf, n)Write n bytes
lseek(fd, offset, whence)Move file position
close(fd)Release file descriptor
stat(path, &sb)Get file metadata
unlink(path)Remove directory entry (delete when link count = 0)
rename(old, new)Atomic rename
truncate(path, size)Set file size
fsync(fd)Force data to disk

File Types (POSIX)

Typels charDescription
Regular-Data file
DirectorydMaps names → inodes
Symbolic linklStores a path string
Block devicebRandom-access device (disk)
Character devicecStream device (terminal)
FIFO / named pipepIPC with file interface
SocketsIPC with network interface

Directory Structure

StructureDescriptionExamples
Single-levelOne flat directory for all usersEarly CP/M
Two-levelSeparate directory per userUNIX early
Hierarchical (tree)Directories contain files + subdirectoriesLinux, Windows
Acyclic graphShared subdirectories via hard/soft linksPOSIX
General graphCycles allowedRequires cycle detection

Disk Block Allocation Methods

Contiguous Allocation

File occupies consecutive blocks on disk.

  • Pros: fast sequential read (single seek), simple indexing
  • Cons: external fragmentation; file cannot grow easily

Linked Allocation

Each block contains a pointer to the next block.

  • Pros: no fragmentation; files grow freely
  • Cons: sequential access only; pointer overhead; reliability (one bad pointer corrupts chain)
  • FAT (File Allocation Table): linked allocation with pointers in a separate table (not in data blocks)

Indexed Allocation

A dedicated index block holds all block pointers for the file.

  • Pros: direct access; no fragmentation
  • Cons: index block overhead; maximum file size = pointers per block × block size

UNIX inode (Multi-Level Index)

inode holds 12–15 block pointers:

Pointer typeLevelsBlocks reachable (4KB block, 4B ptr → 1024 ptrs/block)
Direct (12)012 blocks = 48 KB
Single indirect11024 blocks = 4 MB
Double indirect21024² = 4 GB
Triple indirect31024³ = 4 TB
inode
├── direct[0..11]  → data block
├── single_indirect → [ptr ptr … ptr] → data blocks
├── double_indirect → [ptr … ptr] → [ptr … ptr] → data
└── triple_indirect → 3 levels of index

Free Space Management

MethodDescriptionLookup speed
Bit vector (bitmap)1 bit per block; 0=free, 1=usedO(1) with hardware scan
Linked listFree blocks chained togetherO(n)
GroupingFirst free block stores n free block addressesO(1) for batch
Counting(first_free_block, count) pairsCompact for runs

UNIX Filesystem (ext2/ext3/ext4) Layout

| Boot block | Superblock | Block Group 0 | Block Group 1 | … |

Block Group:
| Group descriptor | Block bitmap | Inode bitmap | Inode table | Data blocks |

Superblock: total blocks, free blocks, inode count, block size, magic number. Block group: locality optimization — inode + its data blocks in same group → fewer seeks.

Journaling (ext3/ext4, NTFS, XFS)

Prevents FS corruption on crash by writing intentions to a journal before actual changes.

ModeWhat is journaledOverhead
WritebackMetadata only; data may precede metadataLow; may expose stale data
OrderedMetadata only; data written before metadata commitDefault in ext3
Data journalingBoth data and metadataSafe; 2× write overhead

Journal commit sequence: 1. Write data to journal (log) 2. Commit record written 3. Write data to actual disk location (checkpoint) 4. Mark journal space as free

Virtual File System (VFS)

Linux VFS provides a uniform interface over heterogeneous FS types (ext4, XFS, tmpfs, FAT, NFS, …).

Application: open(), read(), write()
    ↓
VFS layer: generic inode, dentry, file, superblock objects
    ↓           ↓           ↓
  ext4       tmpfs        NFS
  driver     driver       driver
    ↓
  Block device layer → disk

Four VFS objects:

ObjectRepresents
superblockMounted filesystem metadata
inodeFile/directory metadata
dentryDirectory entry (name → inode mapping, cached)
fileOpen file instance (position, flags)

File Permissions (POSIX)

-rwxr-xr--  1 alice staff 4096 Jan 1 12:00 script.sh
 ↑↑↑↑↑↑↑↑↑
 │└─────── owner: rwx (7)
 │   └──── group: r-x (5)
 │      └─ other: r-- (4)
 └ type: - = regular

Octal: chmod 754 script.sh

Special bits:

BitOn fileOn directory
setuid (04000)Run as file owner
setgid (02000)Run as file groupNew files inherit group
sticky (01000)Only owner can delete entries