What is a program

  • An executable file that contains the code, or a set of processor instructions stored as a file on the disk
  • Sequence of instructions written in a programming language
  • When the code executed by the processor, it becomes a process

Process

  • Active process includes the resources that program needs to run, managed by the OS (processor registers, program counters, stack pointers, memory pages, code segment, data segment, heap)
    • Instance of a program being executed
  • Each process has its own memory address space
    • Independent of other processes
    • But increases execution time to switch between processes to access resources from other process
  • One process cannot corrupt the memory space of another process
  • When one process malfunctions, other processes still can keep running
  • Chrome runs each tab in its own process
    • When one tab misbehaves due to a bug or malicious attack, other tabs are unaffected
    • Chrome reduces memory allocation to idle tabs

Thread

  • Unit of execution within a process
  • A process has at least one thread (main thread)
  • A process usually has many threads (single threaded process VS multi-threaded process)
    • Allow the process to do several things at the same time
  • Each thread has its own:
    • Stack, registers, program counters
    • But code segment, data segment etc. are shared
  • Threads within a process share a memory address space
  • Threads can communicate with each other using the shared memory space efficient
  • A misbehaving thread can bring down the whole process
  • Apache web server uses threading to handle simultaneous requests efficient
    • Does not incur the overhead of creating a separate process for each request

How does the OS run a thread or process on the CPU

  • Handled by context switching
    • 1 process is switched out of the CPU so another process can run
    • OS stores the states of the current running processes so it can be restored and resume execution at a later point
    • It then restores the previously saved states of a different process and resumes execution for that process
  • Context switching is expensive, needs to save and load registers, switching out memory pages and updating various kernel data structures
  • Switching executions between threads also requires context switching, but faster compared to processes
    • Due to fewer states to track, and because threads also share the same memory address space no need to switch out virtual memory pages (one of the more expensive operations

How to minimise context switching

  • Fibers and coroutines
  • Trades complexity for even lower context switching costs