SlideShare a Scribd company logo
Threads vs Processes
Andrew Tridgell
tridge@samba.org
“Aren't threads faster?”
● A very common question but a complex
answer
● Faster for what?
● What do threads/processes actually do?
● What can the hardware do?
● Systems programming
● Answering this question reveals a lot about systems
programming
● I hope it will also make you think a bit about how
operating systems work
What is a thread/process?
● An abstraction of a unit of execution
● We'll generically call them both 'tasks'
● What is in a task?
● Instructions to execute
● Memory (shared and non-shared)
● File descriptors
● Credentials
● Locks
● Network resources
● Relationship to CPUs
● Many/most computers have more than 1 CPU now
● It is common to have many tasks per CPU
The key differences
● Threads
● ???
● Processes
● ???
The key differences
● Threads
● Will by default share memory
● Will share file descriptors
● Will share filesystem context
● Will share signal handling
● Processes
● Will by default not share memory
● Most file descriptors not shared
● Don't share filesystem context
● Don't share signal handling
Underneath the hood
● On Linux, both processes and threads are
created with clone()
Creating a thread:
clone(child_stack=0x420cc260, flags=CLONE_VM|CLONE_FS|
CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|
CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID,
parent_tidptr=0x420cc9e0, tls=0x420cc950, child_tidptr=0x420cc9e0)
Creating a process:
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|
CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f4936ecc770)
A Sample Workload
● A network media server
● Clients connect, and transfer images/videos/music
● Users login with their own accounts
● Files stored in a local filesystem
● What work needs to be done?
● ???
Network Media server ....
● What work needs to be done
● Computation: for format conversions etc
● Memory manipulation
● File IO
● Database access?
● Locking
● Network IO
● Credential handling
● Should it use threads?
malloc()
● Memory allocation
● Extremely common task in almost all programs
● What work needs to be done?
● ???
malloc()
● What work needs to be done?
● Possibly grab more pages from the OS
● Lock data structures?
● Find a free region
● Initialise a block header?
● Are locks needed?
● ???
malloc()
● Are locks needed?
● For threads, locks are needed for most data structure
manipulations in malloc()
● Kernel needs locks for page allocation
● Processes need no user space locks for malloc()
read()/write()
● What about file IO?
● is file IO different in threads vs processes?
● What does an OS need to do for file IO?
● ???
Hint: Common IO system calls
ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);
read()/write()
● What does an OS need to do for file IO?
● Map from file descriptor to file structure
● Copy data to/from page cache
● Possibly initiate disk IO
● How do you map from a fd to a file?
● Simple array? Tree structure?
● Either way, it needs locking
● With threads that can give contention
Hardware vs Software
● What about the MMU?
● Memory Management Unit
● Gives hardware memory protection between tasks
● Virtualises memory addressing
● Another way to look at things
● Threads use software to protect data structures
● Processes use hardware to protect data structures
thread_perf
● Threads vs processes benchmark
● http://samba.org/~tridge/junkcode/thread_perf.c
● Compares common tasks
● malloc, setreuid, readwrite, stat, fstat, create etc.
● Thread library
● Linking to a thread library can matter for processes!
setreuid()
● A very interesting case
● setreuid() used to change task credentials
● Posix requires change of all thread credentials
● Applications often want to change just one task
● thread_perf result
● setreuid() with threads over 200x slower on Linux
● Why??
Memory Footprint
● The memory hierarchy matters
● How much faster is memory than disk?
● What about cache memory vs main memory?
● Reducing memory footprint
● Allows more tasks to run
● May give better use of cache
● Threads
● Easier to pack structures in shared memory
● Less fragmentation of memory?
● May use less memory?
Conclusions
● Non-obvious choice
● Threads and processes have significant differences
● Which is best depends on the application
● Systems programming matters
● Think about the OS level implementation!
● Learn to use strace!

More Related Content

PDF
Dfrws eu 2014 rekall workshop
ODP
Multicore
PDF
Tips and Tricks for Increased Development Efficiency
ODP
Linux logging
PPTX
Disk forensics for the lazy and the smart
ODP
JMeter performance and scalability in Moodle Montana Moot 2014
PPT
Services provided by os
Dfrws eu 2014 rekall workshop
Multicore
Tips and Tricks for Increased Development Efficiency
Linux logging
Disk forensics for the lazy and the smart
JMeter performance and scalability in Moodle Montana Moot 2014
Services provided by os

Viewers also liked (6)

PPT
Operating system services 9
PDF
Os structure
PDF
operating system structure
PPTX
PDF
Virtualization presentation
PPT
OS Functions and Services
Operating system services 9
Os structure
operating system structure
Virtualization presentation
OS Functions and Services
Ad

Similar to Threads and processes (20)

PPTX
CS345 09 - Ch04 Threads operating system1.pptx
PPTX
Threads in Operating System | Multithreading | Interprocess Communication
PPTX
THREADS IN OPERATING SYSTEM & multitasking
PPTX
Epc 3.ppt
PPTX
Processes, Threads.pptx
PPTX
Processes, Threads.pptx
PPT
Os4 2
PDF
Threads in Operating System for GATE.pdf
PDF
The Thread Chapter 4 of Operating System
PPT
Operating System 4
PPT
Operating System 4 1193308760782240 2
PPTX
prez4_operacni_systemy principles and fundamentals
PPTX
Engineeering Operating systemsOS UNIT 3 Threads.pptx
PPTX
Chapter 4 - Operating Systems Threads.pptx
PPTX
Chapter04-OS.pptx........................
PPTX
Threads and Processes in Operating Systems.pptx
PPT
Ch04 threads
PPT
Unix fundamentals
PDF
Unit 4
PPTX
Threads, signal and socket system calls.pptx
CS345 09 - Ch04 Threads operating system1.pptx
Threads in Operating System | Multithreading | Interprocess Communication
THREADS IN OPERATING SYSTEM & multitasking
Epc 3.ppt
Processes, Threads.pptx
Processes, Threads.pptx
Os4 2
Threads in Operating System for GATE.pdf
The Thread Chapter 4 of Operating System
Operating System 4
Operating System 4 1193308760782240 2
prez4_operacni_systemy principles and fundamentals
Engineeering Operating systemsOS UNIT 3 Threads.pptx
Chapter 4 - Operating Systems Threads.pptx
Chapter04-OS.pptx........................
Threads and Processes in Operating Systems.pptx
Ch04 threads
Unix fundamentals
Unit 4
Threads, signal and socket system calls.pptx
Ad

Recently uploaded (20)

PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PPTX
famous lake in india and its disturibution and importance
PPTX
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PPT
protein biochemistry.ppt for university classes
PPTX
Introduction to Cardiovascular system_structure and functions-1
PDF
. Radiology Case Scenariosssssssssssssss
PDF
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PDF
Biophysics 2.pdffffffffffffffffffffffffff
PPTX
neck nodes and dissection types and lymph nodes levels
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
Microbiology with diagram medical studies .pptx
PPTX
INTRODUCTION TO EVS | Concept of sustainability
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PPTX
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
famous lake in india and its disturibution and importance
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
protein biochemistry.ppt for university classes
Introduction to Cardiovascular system_structure and functions-1
. Radiology Case Scenariosssssssssssssss
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
TOTAL hIP ARTHROPLASTY Presentation.pptx
Biophysics 2.pdffffffffffffffffffffffffff
neck nodes and dissection types and lymph nodes levels
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
7. General Toxicologyfor clinical phrmacy.pptx
Microbiology with diagram medical studies .pptx
INTRODUCTION TO EVS | Concept of sustainability
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.

Threads and processes

  • 1. Threads vs Processes Andrew Tridgell tridge@samba.org
  • 2. “Aren't threads faster?” ● A very common question but a complex answer ● Faster for what? ● What do threads/processes actually do? ● What can the hardware do? ● Systems programming ● Answering this question reveals a lot about systems programming ● I hope it will also make you think a bit about how operating systems work
  • 3. What is a thread/process? ● An abstraction of a unit of execution ● We'll generically call them both 'tasks' ● What is in a task? ● Instructions to execute ● Memory (shared and non-shared) ● File descriptors ● Credentials ● Locks ● Network resources ● Relationship to CPUs ● Many/most computers have more than 1 CPU now ● It is common to have many tasks per CPU
  • 4. The key differences ● Threads ● ??? ● Processes ● ???
  • 5. The key differences ● Threads ● Will by default share memory ● Will share file descriptors ● Will share filesystem context ● Will share signal handling ● Processes ● Will by default not share memory ● Most file descriptors not shared ● Don't share filesystem context ● Don't share signal handling
  • 6. Underneath the hood ● On Linux, both processes and threads are created with clone() Creating a thread: clone(child_stack=0x420cc260, flags=CLONE_VM|CLONE_FS| CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM| CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x420cc9e0, tls=0x420cc950, child_tidptr=0x420cc9e0) Creating a process: clone(child_stack=0, flags=CLONE_CHILD_CLEARTID| CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f4936ecc770)
  • 7. A Sample Workload ● A network media server ● Clients connect, and transfer images/videos/music ● Users login with their own accounts ● Files stored in a local filesystem ● What work needs to be done? ● ???
  • 8. Network Media server .... ● What work needs to be done ● Computation: for format conversions etc ● Memory manipulation ● File IO ● Database access? ● Locking ● Network IO ● Credential handling ● Should it use threads?
  • 9. malloc() ● Memory allocation ● Extremely common task in almost all programs ● What work needs to be done? ● ???
  • 10. malloc() ● What work needs to be done? ● Possibly grab more pages from the OS ● Lock data structures? ● Find a free region ● Initialise a block header? ● Are locks needed? ● ???
  • 11. malloc() ● Are locks needed? ● For threads, locks are needed for most data structure manipulations in malloc() ● Kernel needs locks for page allocation ● Processes need no user space locks for malloc()
  • 12. read()/write() ● What about file IO? ● is file IO different in threads vs processes? ● What does an OS need to do for file IO? ● ??? Hint: Common IO system calls ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count);
  • 13. read()/write() ● What does an OS need to do for file IO? ● Map from file descriptor to file structure ● Copy data to/from page cache ● Possibly initiate disk IO ● How do you map from a fd to a file? ● Simple array? Tree structure? ● Either way, it needs locking ● With threads that can give contention
  • 14. Hardware vs Software ● What about the MMU? ● Memory Management Unit ● Gives hardware memory protection between tasks ● Virtualises memory addressing ● Another way to look at things ● Threads use software to protect data structures ● Processes use hardware to protect data structures
  • 15. thread_perf ● Threads vs processes benchmark ● http://samba.org/~tridge/junkcode/thread_perf.c ● Compares common tasks ● malloc, setreuid, readwrite, stat, fstat, create etc. ● Thread library ● Linking to a thread library can matter for processes!
  • 16. setreuid() ● A very interesting case ● setreuid() used to change task credentials ● Posix requires change of all thread credentials ● Applications often want to change just one task ● thread_perf result ● setreuid() with threads over 200x slower on Linux ● Why??
  • 17. Memory Footprint ● The memory hierarchy matters ● How much faster is memory than disk? ● What about cache memory vs main memory? ● Reducing memory footprint ● Allows more tasks to run ● May give better use of cache ● Threads ● Easier to pack structures in shared memory ● Less fragmentation of memory? ● May use less memory?
  • 18. Conclusions ● Non-obvious choice ● Threads and processes have significant differences ● Which is best depends on the application ● Systems programming matters ● Think about the OS level implementation! ● Learn to use strace!