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

Viewers also liked

Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9
myrajendra
 
Os structure
Os structureOs structure
Os structure
Mohd Arif
 
operating system structure
operating system structureoperating system structure
operating system structure
Waseem Ud Din Farooqui
 
Virtualization
VirtualizationVirtualization
Virtualization presentation
Virtualization presentationVirtualization presentation
Virtualization presentation
Mangesh Gunjal
 
OS Functions and Services
OS Functions and ServicesOS Functions and Services
OS Functions and Services
sgpraju
 

Viewers also liked (6)

Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9
 
Os structure
Os structureOs structure
Os structure
 
operating system structure
operating system structureoperating system structure
operating system structure
 
Virtualization
VirtualizationVirtualization
Virtualization
 
Virtualization presentation
Virtualization presentationVirtualization presentation
Virtualization presentation
 
OS Functions and Services
OS Functions and ServicesOS Functions and Services
OS Functions and Services
 

Similar to Threads and processes

CSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptxCSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptx
MadhuraK13
 
Active Data Stores at 30,000ft
Active Data Stores at 30,000ftActive Data Stores at 30,000ft
Active Data Stores at 30,000ft
Jeffrey Sica
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
Dave Cross
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
Bitta_man
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
dilip kumar
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
Munmun Das Bhowmik
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
Sher Shah Merkhel
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess Communication
Shivam Mitra
 
Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?
Robert Burrell Donkin
 
Silverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applicationsSilverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applications
BrettTasker
 
Processor Organization
Processor OrganizationProcessor Organization
Processor Organization
Dominik Salvet
 
MongoDB: Advantages of an Open Source NoSQL Database
MongoDB: Advantages of an Open Source NoSQL DatabaseMongoDB: Advantages of an Open Source NoSQL Database
MongoDB: Advantages of an Open Source NoSQL Database
FITC
 
operating system
operating systemoperating system
operating system
Mayank Saxena
 
Main Memory
Main MemoryMain Memory
Main Memory
Mustafa Ugur Oduncu
 
The Accidental DBA
The Accidental DBAThe Accidental DBA
The Accidental DBA
PostgreSQL Experts, Inc.
 
Case study of BtrFS: A fault tolerant File system
Case study of BtrFS: A fault tolerant File systemCase study of BtrFS: A fault tolerant File system
Case study of BtrFS: A fault tolerant File system
Kumar Amit Mehta
 
Cloud accounting software uk
Cloud accounting software ukCloud accounting software uk
Cloud accounting software uk
Arcus Universe Ltd
 
Introduction to Memoria
Introduction to MemoriaIntroduction to Memoria
Introduction to Memoria
Victor Smirnov
 
How to Monitor MySQL
How to Monitor MySQLHow to Monitor MySQL
How to Monitor MySQL
Server Density
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
Command Prompt., Inc
 

Similar to Threads and processes (20)

CSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptxCSE2010- Module 4 V1.pptx
CSE2010- Module 4 V1.pptx
 
Active Data Stores at 30,000ft
Active Data Stores at 30,000ftActive Data Stores at 30,000ft
Active Data Stores at 30,000ft
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 
08 operating system support
08 operating system support08 operating system support
08 operating system support
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess Communication
 
Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?
 
Silverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applicationsSilverstripe at scale - design & architecture for silverstripe applications
Silverstripe at scale - design & architecture for silverstripe applications
 
Processor Organization
Processor OrganizationProcessor Organization
Processor Organization
 
MongoDB: Advantages of an Open Source NoSQL Database
MongoDB: Advantages of an Open Source NoSQL DatabaseMongoDB: Advantages of an Open Source NoSQL Database
MongoDB: Advantages of an Open Source NoSQL Database
 
operating system
operating systemoperating system
operating system
 
Main Memory
Main MemoryMain Memory
Main Memory
 
The Accidental DBA
The Accidental DBAThe Accidental DBA
The Accidental DBA
 
Case study of BtrFS: A fault tolerant File system
Case study of BtrFS: A fault tolerant File systemCase study of BtrFS: A fault tolerant File system
Case study of BtrFS: A fault tolerant File system
 
Cloud accounting software uk
Cloud accounting software ukCloud accounting software uk
Cloud accounting software uk
 
Introduction to Memoria
Introduction to MemoriaIntroduction to Memoria
Introduction to Memoria
 
How to Monitor MySQL
How to Monitor MySQLHow to Monitor MySQL
How to Monitor MySQL
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 

Recently uploaded

SAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdfSAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdf
KrushnaDarade1
 
ESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptxESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptx
PRIYANKA PATEL
 
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
Sérgio Sacani
 
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
vluwdy49
 
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdfwaterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
LengamoLAppostilic
 
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
Abdul Wali Khan University Mardan,kP,Pakistan
 
8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf
by6843629
 
Shallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptxShallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptx
Gokturk Mehmet Dilci
 
Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
University of Hertfordshire
 
Immersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths ForwardImmersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths Forward
Leonel Morgado
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
pablovgd
 
Mending Clothing to Support Sustainable Fashion_CIMaR 2024.pdf
Mending Clothing to Support Sustainable Fashion_CIMaR 2024.pdfMending Clothing to Support Sustainable Fashion_CIMaR 2024.pdf
Mending Clothing to Support Sustainable Fashion_CIMaR 2024.pdf
Selcen Ozturkcan
 
Equivariant neural networks and representation theory
Equivariant neural networks and representation theoryEquivariant neural networks and representation theory
Equivariant neural networks and representation theory
Daniel Tubbenhauer
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
Sérgio Sacani
 
20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx
Sharon Liu
 
Eukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptxEukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptx
RitabrataSarkar3
 
Pests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdfPests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdf
PirithiRaju
 
Direct Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart AgricultureDirect Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart Agriculture
International Food Policy Research Institute- South Asia Office
 
Katherine Romanak - Geologic CO2 Storage.pdf
Katherine Romanak - Geologic CO2 Storage.pdfKatherine Romanak - Geologic CO2 Storage.pdf
Katherine Romanak - Geologic CO2 Storage.pdf
Texas Alliance of Groundwater Districts
 
Randomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNERandomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNE
University of Maribor
 

Recently uploaded (20)

SAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdfSAR of Medicinal Chemistry 1st by dk.pdf
SAR of Medicinal Chemistry 1st by dk.pdf
 
ESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptxESR spectroscopy in liquid food and beverages.pptx
ESR spectroscopy in liquid food and beverages.pptx
 
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
 
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
 
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdfwaterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
waterlessdyeingtechnolgyusing carbon dioxide chemicalspdf
 
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
 
8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf
 
Shallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptxShallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptx
 
Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
 
Immersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths ForwardImmersive Learning That Works: Research Grounding and Paths Forward
Immersive Learning That Works: Research Grounding and Paths Forward
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
 
Mending Clothing to Support Sustainable Fashion_CIMaR 2024.pdf
Mending Clothing to Support Sustainable Fashion_CIMaR 2024.pdfMending Clothing to Support Sustainable Fashion_CIMaR 2024.pdf
Mending Clothing to Support Sustainable Fashion_CIMaR 2024.pdf
 
Equivariant neural networks and representation theory
Equivariant neural networks and representation theoryEquivariant neural networks and representation theory
Equivariant neural networks and representation theory
 
The binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defectsThe binding of cosmological structures by massless topological defects
The binding of cosmological structures by massless topological defects
 
20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx20240520 Planning a Circuit Simulator in JavaScript.pptx
20240520 Planning a Circuit Simulator in JavaScript.pptx
 
Eukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptxEukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptx
 
Pests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdfPests of Storage_Identification_Dr.UPR.pdf
Pests of Storage_Identification_Dr.UPR.pdf
 
Direct Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart AgricultureDirect Seeded Rice - Climate Smart Agriculture
Direct Seeded Rice - Climate Smart Agriculture
 
Katherine Romanak - Geologic CO2 Storage.pdf
Katherine Romanak - Geologic CO2 Storage.pdfKatherine Romanak - Geologic CO2 Storage.pdf
Katherine Romanak - Geologic CO2 Storage.pdf
 
Randomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNERandomised Optimisation Algorithms in DAPHNE
Randomised Optimisation Algorithms in DAPHNE
 

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!