Slide 1

Slide 1 text

Know an Unix An brief overview of processes, threads, and concurrency

Slide 2

Slide 2 text

What is an process? • An container for a running program • Any code that is executed happens inside a process

Slide 3

Slide 3 text

Properties of processes • Processes have IDs • Processes have parents • Processes have isolated memory • Processes have file descriptors • Processes have an environment • Processes communicate

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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!

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Communication • Exit codes • Waiting • Signals • Pipes

Slide 9

Slide 9 text

Efficiency • Memory sharing • Copy on write • fork and exec

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Efficiency and trade-offs • Lower memory consumption • Faster start up time • Thread-safety issues

Slide 12

Slide 12 text

Python and the GIL • GIL == Global Interpreter Lock • Forces process-based concurrency • Not present in other Python implementations, such as PyPy

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

~ FIN ~