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
Tweet

More Decks by Jimmy Cuadra

Other Decks in Technology

Transcript

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

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

    parents • Processes have isolated memory • Processes have file descriptors • Processes have an environment • Processes communicate
  3. 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
  4. 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!
  5. 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
  6. 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
  7. 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
  8. Python and the GIL • GIL == Global Interpreter Lock

    • Forces process-based concurrency • Not present in other Python implementations, such as PyPy
  9. 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