Processes & Scheduling
Operating systems cheat sheet: what a process is, the five process states, what the PCB holds, how a context switch works, FCFS, SJF, round robin, priority, MLFQ and CFS scheduling compared, process vs thread, and the ways processes talk to each other.
Cheat sheet · Offered as a template
In this diagram
- Processes & Scheduling
- ① What is a process?
- A program in execution: code plus its own address space
- Owns resources: memory, open files, sockets, credentials
- Has one or more threads, each with its own stack and registers
- The kernel tracks each one in a process control block (PCB)
- ② Process states
- New
- Ready
- in the run queue
- Running
- on a CPU
- Terminated
- Waiting
- blocked on I/O, a lock or a timer
- Only Running uses a CPU. Ready processes queue for one; Waiting ones sleep until their event arrives.
- ③ Inside the PCB
- task_struct (Linux)
- ④ Scheduling algorithms
- ⑤ Context switch
- A timer interrupt or syscall traps into the kernel
- Save the running thread’s registers into its PCB
- The scheduler picks the next thread from the run queue
- Switch address space: new page tables, stale TLB
- Restore the next thread’s registers, return to user mode
- ⑥ Create and wait (POSIX)
- spawn.c
- fork() clones the caller, exec() replaces the program, wait() reaps the child so it does not linger as a zombie.
- ⑦ Process or thread?
- Process
- Its own address space
- A crash stays contained
- Talks through IPC: slower, safer
- Expensive to create and to switch
- Thread
- Shares its process’s memory
- One bad pointer corrupts everyone
- Shares data directly: needs locks
- Cheap to create and to switch