Unix O/S
 INTERNALS

Some Concepts /
A Trip Down the Rabbit-hole

Unix

  • Operating System
  • Developed in the 1970s
  • Now exists in may variants
  • Inspiration for Linux
  • Foundation for Mac OS X

The Unix Philosophy


    • a set of simple tools that each perform a limited, well-defined function
    • a unified filesystem as the main means of communication
    • a shell scripting and command language  to combine the tools to perform complex workflows

Variants


Things we will cover


How programs are run

Virtual memory
Memory addressing
Swapping

Files, inodes

By Example


What happens when you type:

$ ls > my_files 

By Example


What happens when you type:

$ ls > my_files 

  • tell the shell (bash)

By Example


What happens when you type:

$ ls > my_files 

  • tell the shell (bash)
  • run the list files program

By Example


What happens when you type:

$ ls > my_files

  • tell the shell (bash)
  • run the list files program
  • save the output to file my_files

Run


But what does "run" actually mean?

How do programs
(binary code on disk)

get turned into processes
(running code in memory)

FORK/EXEC

An elegant mechanism

Fork
clone the current process

Exec
replace current process with new program

Fork


How does the o/s fork a process?

Wait a minute, what is a process?

A Process


Code

"The program" - compiled machine instructions


Data

Data the program "brought with it"
Data created during execution


Execution State

What is the next instruction to execute
How did we get here (who called who)


Associated System Resources

e.g. Open files

Process Memory


The Process table


Where the kernel keeps track of all running processes

This table is what you are looking at when you run:

ps

Virtual Memory

Tracking Process Memory



Page Table Entry


Metadata
  • valid
  • dirty
  • age
  • CoW
  • protect

Page base address

32 Bit Addressing

Given a 32 bit address size and 4k page size:

Paging & Swapping

Fork


Create a new clone of an existing process
  • Memory
  • Open files
  • Execution state

Fork

Process memory, before:

Fork

Process memory, after:

File I/O


File Descriptors

Small integers describing open files

0 = Standard Input (STDIN)
1 = Standard Output (STDOUT)
2 = Standard Error (STDERR)

File I/O



Files

After Fork

$ ls > my_files

Before


$ ls > my_files

Step 1: Fork

$ ls > my_files

Step 2: Close STDOUT

$ ls > my_files

Step 3: Open my_files (as STDOUT)

$ ls > my_files

Step 5: Exec /bin/ls