Operating systems orchestrate many processes, allow access to memory, disk and network and execute the process by scheduling them to the CPU. Sounds simple when we put it this way but this task is vast. So vast indeed that writing programs that are efficient really depends on how much understanding the engineer has in operating systems.
Operating systems orchestrate many processes, allow access to memory, disk and network and execute the process by scheduling them to the CPU. Sounds simple when we put it this way but this task is vast. So vast indeed that writing programs that are efficient really depends on how much understanding the engineer has in operating systems.
I have been fascinated by revealing and demystifying anything that is hidden from me, mainly because by treating black boxes as black boxes that takes input and produces output, I feel that I am at the mercy of this black box. Databases were a black box for me a decade ago, I was at completely lost when a query that I wrote was originally running fast but after few month it had gone slow. The reason is I didn't really have any understanding of database systems and how their internals work.
The same thing is true for operation systems, we run our program which turns into a process that uses bit of memory and access disk and network but we really don't know how this is done and as a result we as engineers are bound to write inefficient code.
I built this course to demystify what I believe are the fundamentals operating systems to software engineers. Like all my courses, I recommend the student having some programming experience to take this course, it just makes the course relatable. I will be using multiple operating systems like Linux, Windows and Mac in this course.
I hope you enjoy it.
Most operating systems are general purpose software that link software to hardware. You don't have to use one. By the end of this course you will feel that for most of the apps we write we can use a general purpose OS like Linux or windows but sometime to be really efficient one might consider writing their own special purpose.
A lecture explaining the most critical hardware components and the OS,
CPU
Memory
Storage
Network
File System
Security
Program
Process
Kernel
Userspace
Kernel space (drivers)
System calls
In this lecture we show an example of the main function with instruction pointer moving to the next and execute. Simple with no stack or heap
The brilliant invention of the stack, We introduce the stack function calling another function and local variables, return address, frames
It is understanding the stack that makes us pay attention to performance, too many function calls will jump from one to another
We Introduce the heap with an example, place where random blobs of memory is allocated, need to access it from multiple place and manipulate it. static vars are like this, except we can change the heap. Here lies the danger when it comes to threads as two threads changing the same place.
Allocating in the heap does not guarantee memory will be contagious, so if I store A and B , If I try to read A and B it might need two memory accesses as they might be in different pages, we will learn this in the virtual memory section. Whereas in the stack all variables are next to each other and memory access might give us both at once minimizing memory accesses.
Use linux, show all processes top
run ./a.out
then run cat /proc/29404/maps
this will list all mappings, including c library lib c, (except kernel mappings)
The process components
We talk about memory, random access read and write memory, fast access volatile, DIMMs, double data read how CPU reads
We walk through the process of reading from memory, show how it must go through the CPU
We will discuss the challenges of physical memory, we want to run many programs but we don't have enough memory contiguous should we just fail if I'm out of memory? Meet swap to disk. I have 10 programs all using libc a shared library, should I load 10 copies of the identical code or should I share them? I have 10 processes wanting to share memory.
We will talk cost of virtual memory, page tables and is it really worth it
Internal fragmentation (memcached allocates large block but in the block lots of small space)
External fragmentation, the physical memory is available but all over the place cant even allocate a block to you
Shared memory
Isolation
Large Programs (Swap)
CPU involvement in memory reading is slow, Direct memory access allow a device to write or read directly between the device and user space as opposed to moving it from device to the register and write it back to memory
Going through Top command explain the memory list
Virtual memory
Residential
Shared
Code to allocate memory and leak, then fix it. and show memory
Components
ALU
CU
Registers and Caches
Clock Speed
Cores
Pipelining
Parallelism
Architecture
RISC vs CISC
32 bit 64 bit
uname -m to know the cpu architecture (32 or 64 bit)
cat /proc/cpuinfo
Walk through fetch an instruction, we check L1, L2, L3, if not, we get it from memory, (64 byte cache line) put it in L3, L2 and L1i then we fetch the actual instruction to the IR (instruction register)
we then decode the instruction, covert the opcode of the instruction to actual instruction, RISC is simple, CISC takes time. (CU)
Then we actually execute (ALU),
(optional memory read)
(write back to register)
we fetch another instruction, (cache hit L1)
Pipelining, one instruction requires many stages, fetch, decode, execute, memory access , memory write
if we wait all stages cpu won't be doing anything, we pipeline them. The laundry example
Parallelism, multiple cores multiple processes simple
multiple cores single process, multiple threads
hyper threading or Simultaneous multi threading
- one core is represented as two logical core with logical registers (program counter etc) and caches are shared.
We discuss how we can saturate the workload of the process and thread
Mutexes and Semphores
Show a fork() and CoW where forking creates a new process
cow.c
cat /proc/1478/smaps | grep -A 50 heap
to grab the 50 lines after heap
notice the
Shared_Clean: 0 kB
Shared_Dirty: 9692 kB
Private_Clean: 0 kB
Private_Dirty: 552 kB
Why persist?
HDD vs SSD
App -> FileSystem -> Block Layer -> NVMe Driver
File system
FAT
ext4
Page cache
Read vs Write example
File Modes
Discuss some of the file modes O_SYNC, O_DIRECT
Little intro on NIC/ networks, OSI model, ip , tcp, UDP
socket
connection
Accept vs SYN queue
Receive vs Send Queue
tcp_hashinfo memory for mapping connection
select
epoll
IOCP
kqueue
io_uring
Build a tcp server with C
Compiled Language vs Interrupted
Execution File format (exe/elf)
cgroup vs namespace
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.