The Software Stack

Operating Systems — Lecture 01

Four small programs

strcat() x4          309604 us        strcpy() at known offsets   10475 us

char *str = "..."    segfault         char str[] = "..."          works

find /usr/share      2.66 s           the same command again      0.18 s

s = s + "a"          O(N)             keep = s; s = s + "a"       O(N^2)

Same output. Same machine. Same library.

You do not need to know everything that is under you.

You need to know that something is under you, roughly what it does, and where to look when the numbers stop making sense.

Today

  1. What are the layers?
  2. What is the operating system?
  3. What does a layer cost?
  4. Applications and libraries?
  5. How do components interact?

The stack

Every horizontal line is an interface

ISA · system call API · C API · language API · framework API · UI

An interface is a promise about what.

It is valuable in proportion to how little it says about how.

Demo: the same message, five heights

./hello-nolibc         2 system calls        8 888 B     0.25 ms
./hello-asm            30 system calls      15 712 B     0.35 ms
./hello-c              35 system calls      16 032 B     0.35 ms
./hello-cpp            65 system calls      16 568 B     0.70 ms
python3 hello.py     6155 system calls           ---       32 ms

All five end in the same write().

So why does nobody write the first one?

The operating system

Management · arbitration · system services

System calls

Family Examples Lecture
processes and threads fork(), execve() 06–08
memory brk(), mmap() 03–05
file I/O open(), read(), write() 09–10
network I/O socket(), connect() 11
IPC pipes, signals, shared memory 10, 12

man 2 syscalls · almost nothing calls them directly

One door

User mode: cannot touch device registers, page tables, another process’s memory.

Kernel mode: can.

Across: the syscall instruction, to one address the kernel chose at boot.

A mode switch costs hundreds of nanoseconds. That is the price of the check — and the reason libc buffers.

The kernel is a library, not a process

It runs when called:

  • a process makes a system call — and the kernel runs on behalf of that process
  • a device raises an interrupt

Between those, it is not running at all.

Where is it in ps?

Trade-offs

Demo: what a guarantee costs

djb2       0.032593 s per million hashes
SHA-256    0.697281 s per million hashes      21.4x

The slowness is the security property.

For storing passwords, both are wrong — SHA-256 is too fast.

Applications and libraries

An entry point, and something that starts it · an interface, and something that calls it

Interaction

UI a person · API the same process · protocol a different one

Software is built in layers, and every layer is an interface that hides an implementation.

Understanding the layer below yours is what turns a surprising measurement into a decision you can defend.