Skip to content

Instantly share code, notes, and snippets.

@zmarvel
Created April 18, 2019 05:58
Show Gist options
  • Save zmarvel/369c417c781f1150a2879061a1aa3161 to your computer and use it in GitHub Desktop.
Save zmarvel/369c417c781f1150a2879061a1aa3161 to your computer and use it in GitHub Desktop.
Systems programming notes

Systems Programming Topics

  • Tasks

    • Threads share memory. You have to make sure that memory accesses are atomic when it matters, using synchronization primitives.
    • Processes by default have their own memory. In Unix, you have to use a "shared memory" API to communicate between processes. You can also use pipes and sockets.
  • Synchronization

    • Mutexes. Low-level primitive for making sure "critical sections" aren't interrupted by another scheduler giving control to another thread. Suppose two threads share some memory. This ensures thread A can finish its modification of the shared memory before thread B accesses the memory, and vice-versa. See the Xinu OS book.
    • Semaphores. Another low-level primitive for signaling that something has been done, and waiting on it in another thread. Similar to mutexes, effectively protects shared memory and indicates "critical sections." See the Xinu OS book, Wikipedia for description and examples.
  • Inter-process communication (IPC). The implementation of these depends on synchronization primitives.

    • Message queues.
    • Pipes. One-way commmunication between processes.
    • Sockets. These can be used for communication with other systems, but also for inter-process communication.

    All of these can be waited on by a process or thread. For example, if a process calls recv on a socket, it will not be scheduled until a message is ready. (This is possible because the OS handles scheduling and IPC.)

  • Scheduling. Make sure to understand gotchas around scheduling!

    • Deadlock. Make sure two+ tasks don't acquire (lock/wait on) the same resource. For example, if thread A "produces" a resource, and thread B "consumes" a resource, make sure threads A and B don't both try to "consume" the resource!
    • Race conditions. This is avoided by proper locking (use of synchronization primitives, or thread-safe IPC techniques). Suppose two threads, A and B, share memory. What if, for example, thread B tries to free the memory while thread A is trying to use it?
  • Memory allocation.

    • malloc and free are the C-programmer way to do this. C++ has constructors and destructors to make this easier. Does Objective-C have a similar facility? Suppose you have a class Foo. It owns an array. When Foo is freed, its destructor is called---it can then free the array that it owns.
    • At a higher level, buffer pools can be used to manage memory. See the Xinu OS book, or Google it.
  • Toolchain

    • Compiler. This makes object files. Eventually these get linked together into an executable by the linker.
    • Linker. This lins object files and libraries together into an executable.
    • Debugger. This can execute an executable and set breakpoints or watchpoints. It can also map between compiled code and source code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment