Processes and Threads

Operating Systems — Lecture 06

Three numbers

$ ps -e --no-headers | wc -l
620                                    from 377 different programs

$ ps -o pid,comm,%cpu -C ffmpeg
  12632 ffmpeg   641                   one process, 641% of a CPU

$ diff /proc/15672/maps /proc/15672/task/15674/maps
                                       56 threads, one address space

Running an application means making a process. This lecture: what a process is, and what runs inside it.

The story

A process is two halves

$ ps -e --no-headers | wc -l        620 processes
$ ps -e -L --no-headers | wc -l    3412 threads

The address space stores; the thread(s) run.

The address space, recapped

Code and data from the executable; heap and libraries grown at run time; one stack per thread. The threads share all of it bar each other’s stacks.

What a thread is

A thread is an execution context: two pointers.

  • IP — what to execute next, an address in the code.
  • SP — the thread’s stack: the call chain and the locals.

Give a CPU an IP and an SP and it has a flow of execution.

Shared, and not shared

IP: 0x40127d  SP: 0x7572...edec     thread_sleep1
IP: 0x40127d  SP: 0x7572...ddec     thread_sleep1   same IP, other SP
IP: 0x4012f6  SP: 0x7572...bdec     thread_sleep2   other IP

Same function, same IP: shared code. Every SP different: a stack each.

Create, wait, terminate

$ ./posix_spawn
PID: 24687, parent PID: 8217, child PID: 24688
Process exited with status 0

A process is created by another, which waits and reads how it ended.

Zombies and orphans

$ ps -o pid,stat,cmd -C sleep     222221 Z   [sleep] <defunct>   ended, not waited
$ ps -o pid,ppid,cmd -C sleep      24533 4984 /usr/bin/sleep     parent is now init

Threads from a function

pthread_create(&tid, NULL, worker, arg);
pthread_join(tid, &result);
$ ./create_join      total: 500000500000   matches single-threaded sum: yes

Own slice each, read after the join: no lock. Sharing writable memory is lecture 08.

fork() and exec()

fork() copies and returns twice; exec() replaces and does not return.

One call underneath

$ strace -f -e clone,clone3,execve ./fork_exec
clone(... SIGCHLD ...) = 105189
[pid 105189] execve("/usr/bin/sleep", ...) = 0

$ strace -f -e clone,clone3,execve ./posix_spawn
clone3({flags=CLONE_VM|CLONE_VFORK|...}) = 105405

fork() copies, pthread_create() shares: both are clone(). A thread’s clone3 adds CLONE_VM.

A process is an address space plus one or more threads.

The space holds the program; the threads run it and share the space, each with its own stack.

A second flow of control is another process with fork()+exec(), or another thread with pthread_create() — and both are clone().