Operating systems¶
~12 min
In short¶
An operating system (OS) manages the processor, memory, disks, and devices so that many programs can share them safely. This page covers the main ideas you will meet in a first course: processes, threads, scheduling, memory, files, and system calls. It also says what to leave for later.
What the subject is about¶
When you run a program, the OS creates a process. A process has its own address space, open files, and state. The OS decides which process runs on the processor at each moment. This decision is called scheduling. Threads are units of execution inside a process; several threads share the same memory, which makes them fast but also makes bugs such as race conditions possible.
Memory is another core job. Virtual memory gives each process the illusion of its own large address space. The OS and the hardware translate virtual addresses to physical ones using page tables. When memory runs short, pages can move to disk.
Programs ask the OS for services through system calls, such as opening a file, reading from a socket, or creating a process. The OS checks the request and performs it in privileged mode. Your program runs in user mode and cannot touch hardware directly.
What to learn first¶
- The difference between a program and a process.
- The process states (new, ready, running, waiting, terminated) and what moves a process between them.
- System calls: what they are and why programs need them.
- Threads, and how they differ from processes.
- Scheduling: first come first served and round robin, at the idea level.
- Race conditions and why locks (mutexes) are needed.
- Deadlock: the four conditions that must hold for it to happen.
- Virtual memory and paging, at the level of addresses and page tables.
- File systems: files, directories, and how a file is found on disk.
- Interrupts and how the OS regains control from a running program.
The Python course's ../../python/04-functions-and-structure/05-files-and-exceptions.md is a good first hands-on look at files as the OS sees them. The backend lesson ../../python/08-backend/01-http-request-lifecycle.md shows how a request moves through a server process.
What you can skip at first¶
- Writing kernel code. Read about it, but do not start with it.
- Specific scheduler algorithms used in Linux and Windows. Learn the principles first.
- Detailed page-replacement algorithms beyond the basic idea of least recently used.
- Driver development and device internals.
- Distributed file systems and cluster schedulers.
How it connects to the other subjects¶
Computer architecture gives you the privilege modes, interrupts, and the memory hierarchy that the OS builds on. See computer architecture.
Databases rely on the OS for files, locks, and caching. A database transaction that waits on a lock is using OS concurrency primitives underneath. See databases.
Networking uses sockets, which are file-like handles provided by the OS. A web server handling many clients uses threads, processes, or event loops, and the OS decides how they run. See computer networks.
Data structures appear in the kernel too: run queues, hash tables for lookups, and trees for memory maps.
Free resources¶
- Operating Systems: Three Easy Pieces, Arpaci-Dusseau (https://pages.cs.wisc.edu/~remzi/OSTEP/). A free textbook organised around virtualisation, concurrency, and persistence. It suits a student who wants clear explanations with a focus on ideas.
- MIT 6.S081, Operating System Engineering (https://pdos.csail.mit.edu/6.S081/2020/). Lectures and labs built around the small teaching kernel xv6. It suits someone who wants to write OS code after the basics.
- MIT 6.033, Computer System Engineering (https://ocw.mit.edu/courses/6-033-computer-system-engineering-spring-2018/). Lectures from MIT OpenCourseWare on how real systems are structured and what goes wrong. It suits a student who wants the bigger picture of systems design.
- The Missing Semester of Your CS Education (https://missing.csail.mit.edu/). Covers the shell and the command-line tools you use to observe processes and files. It suits anyone who wants to see the OS in action on their own machine.
Practice¶
- Open a terminal and run a command that lists running processes. Find one process and write down its process ID, its parent process ID, and what it does.
- Write a Python program that starts a child process and waits for it to finish. Use the
subprocessmodule from the standard library. - Write a program that starts two threads, each adding 1 to a shared counter 100000 times without a lock. Run it three times and explain why the totals can differ.
- Fix the counter program from task 3 using a lock. Explain in two sentences what the lock guarantees.
- Using a file-reading program, list the system calls it makes with
straceon Linux ordtrusson macOS, and write down the three calls you recognise.
Sources¶
- Arpaci-Dusseau and Arpaci-Dusseau, Operating Systems: Three Easy Pieces, https://pages.cs.wisc.edu/~remzi/OSTEP/
- MIT, 6.S081 Operating System Engineering, https://pdos.csail.mit.edu/6.S081/2020/
- MIT OpenCourseWare, 6.033 Computer System Engineering, https://ocw.mit.edu/courses/6-033-computer-system-engineering-spring-2018/
- MIT, The Missing Semester of Your CS Education, https://missing.csail.mit.edu/