Keyboard shortcuts

/ or ⌘/Ctrl K
Find a note
j / k
Next / previous section or linked note
h / l
Collapse or go to parent / expand or enter
e or Alt-click
Read a linked note here
o
Open focused note on its own
g g / G
First / last section or linked note
g h / g a
Home / all notes
g b / g t
Backlinks / table of contents
t
Cycle System, Light, Dark
? / Esc
Show / close this reference

Search: ↑/↓ or Ctrl N/P, Enter to open. Shortcuts pause while typing.

operating systems [b05008d3]

Tags: computers

1. Major Parts

  • Isolation between processes
  • Mobility (for mobile)
  • Security
  • Minimizing the overhead
  • Syscalls transition the privilage level

    • syscalls are like any regular procedure calls, but with a trap instruction
    • kernel agrees to put syscalls on well known locations

1.1. Trapping and untrapping for syscalls

  • Process needs to save the program counter (PC)/instruction pointer (IP) which stores the address of the next instructino to be executed (for resuming)
  • Some flags about resuming execution on the CPU state
  • All of them get written onto a stack on the kernel side, which then pops back up

1.2. Limited Direct Execution

  1. At boot kernel initializes a trap table
  2. When running a process, kernel sets up a trap and return of trap to switch back and forth to do syscalls

2. Forms of Concurrency Bugs

  • Atomicity violations
  • Order violation
  • Classical deadlock bugs

    • Conditions for a deadlock

      1. Mutual exclusion
      2. Hold and wait
      3. No preemption
      4. Circular wait

3. Threads [813d6555]

  • Similar to a linux process that always run in the same memory

    • own instruction pointer, and can be scheduled on different CPU's and executing different things on the same program
    • shares the process structure (execpt stacks)
    • linux threads have their own pid
    • solaris is completely thread based
    • in linux, a thread is a prococess without a thread
    • created with the clone() system call
    • treat as one process in some games, uses the thread group
    • getpid() will tell you some info
    • syn and locks!
    • threads tend to be pretty fast, usually an async that does stuff in the background, and never switches off the task running state, spinning is TASK_RUN, but it's not waiting on IO but rather a lock
    • biggest reason to use over a fork is because you can do the same memory
  • linux kernel only uses lightweight processes, or threads.

    • Threads use libc to transition to kernel space, but posix threads originate from libc and libpthread. Note that the kernel only uses LWPs. However, at the kernel layer, since a userspace thread maps to a lwp, it doesn't really matter

    2024-02-11_19-16-26_screenshot.png

  • Debugger commands

    • GDB

      • info threads -> list threads
      • thread <n> -> switches between threads
      • thread apply all bt -> backtraces
    • LLDB

      • thread list
      • thread select 1
      • thread backtrace all~/~bt all
  • Thread stacks

    • each thread has its own stack to store data, and we can dump it like normal
    • Stack tracing a thread is when a function A calls function B, and function B has a return address to where function A called it.

      2024-02-11_19-18-17_screenshot.png

  • Symbol files

    • Symbols provide mappings between memory address ranges and associated symbol names, if you don't have the symbols, you just have the current function

4. Concurrent Forward Progress

  • If your threads are preemptible, it's possible that they, while being preempted for another task, might come back to having less resources than before, since the preemption task might take a different amount of resources. How do we fix this?

5. NixOS [945f2300]