Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Know an Unix

Know an Unix

An brief overview of processes, threads, and concurrency


Jimmy Cuadra

January 30, 2014


  1. Know an Unix An brief overview of processes, threads, and

  2. What is an process? • An container for a running

    program • Any code that is executed happens inside a process
  3. Properties of processes • Processes have IDs • Processes have

    parents • Processes have isolated memory • Processes have file descriptors • Processes have an environment • Processes communicate
  4. Listing running processes • The ps command lists running processes

    • It can list your own processes, processes from other users, processes without controlling terminals, and much more • The list shows the command, process ID, owning user, CPU and memory usage, and much more
  5. Concurrency • Initially, a process is running only one thread

    (more on threads later) • This limits the process to executing a single sequence of instructions • To achieve parallel execution, a process can FORK!
  6. Forking • A thing that gets you in trouble at

    PyCon • When a process forks, it creates an identical copy of itself • All memory and file descriptors are copied to the child process • The parent process receives the PID of the child • Both processes continue executing at the same point
  7. from os import fork, getpid ! print "parent process pid

    is %s" % getpid() ! if fork(): print "entered the if block from %s" % getpid() else: print "entered the else block from %s" % getpid() ! ! ! ! parent process pid is 14890 entered the if block from 14890 entered the else block from 14911
  8. Communication • Exit codes • Waiting • Signals • Pipes

  9. Efficiency • Memory sharing • Copy on write • fork

    and exec
  10. Threads • A single sequence of code execution • Time

    scheduled by the kernel • Can be multiplexed across CPU cores • A process can spawn multiple threads • Share the containing process’s memory
  11. Efficiency and trade-offs • Lower memory consumption • Faster start

    up time • Thread-safety issues
  12. Python and the GIL • GIL == Global Interpreter Lock

    • Forces process-based concurrency • Not present in other Python implementations, such as PyPy
  13. Moore’s Law • “Moore's law is the observation that, over

    the history of computing hardware, the number of transistors on integrated circuits doubles approximately every two years.” • No longer true! Processor speed is leveling off • Computers are now getting faster by adding cores • Multi-core computing is the future
  14. ~ FIN ~