You are on page 1of 18

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 mae you thin a bit about how


operating systems wor
What is a thread/process?

An abstraction of a unit of execution

We!ll generically call them both !tass!

What is in a tas?

Instructions to execute

"emory #shared and non$shared%

File descriptors

&redentials

'ocs

(etwor resources

)elationship to &*+s

"any/most computers have more than , &*+ now

It is common to have many tass per &*+


The key differences

-hreads

???

*rocesses

???
The key differences

-hreads

Will by default share memory

Will share file descriptors

Will share filesystem context

Will share signal handling

*rocesses

Will by default not share memory

"ost file descriptors not shared

.on!t share filesystem context

.on!t share signal handling


Underneath the hood

/n 'inux0 both processes and threads are


created with clone#%
Creating a thread:
clone#child1stac23x453cc5630 flags2&'/(718"9&'/(71FS9
&'/(71FI'7S9&'/(71SI:;A(.9&'/(71-;)7A.9&'/(71S<S8S7"9
&'/(71S7--'S9&'/(71*A)7(-1S7--I.9&'/(71&;I'.1&'7A)-I.0
parent1tidptr23x453cc=e30 tls23x453cc=>30 child1tidptr23x453cc=e3%
Creating a process:
clone#child1stac230 flags2&'/(71&;I'.1&'7A)-I.9
&'/(71&;I'.1S7--I.9SI:&;'.0 child1tidptr23x?f4=@6ecc??3%
A Sap!e Work!oad

A networ media server

&lients connect0 and transfer images/videos/music

+sers login with their own accounts

Files stored in a local filesystem

What wor needs to be done?

???
"et#ork $edia server %%%%

What wor needs to be done

&omputationA for format conversions etc

"emory manipulation

File I/

.atabase access?

'ocing

(etwor I/

&redential handling

Should it use threads?


a!!oc&'

"emory allocation

7xtremely common tas in almost all programs

What wor needs to be done?

???
a!!oc&'

What wor needs to be done?

*ossibly grab more pages from the /S

'oc data structures?

Find a free region

Initialise a bloc header?

Are locs needed?

???
a!!oc&'

Are locs needed?

For threads0 locs are needed for most data structure


manipulations in malloc#%

Bernel needs locs for page allocation

*rocesses need no user space locs for malloc#%


read&'/#rite&'

What about file I/?

is file I/ different in threads vs processes?

What does an /S need to do for file I/?

???
;intA &ommon I/ system calls
ssiCe1t read#int fd0 void Dbuf0 siCe1t count%E
ssiCe1t write#int fd0 const void Dbuf0 siCe1t count%E
read&'/#rite&'

What does an /S need to do for file I/?

"ap from file descriptor to file structure

&opy data to/from page cache

*ossibly initiate dis I/

;ow do you map from a fd to a file?

Simple array? -ree structure?

7ither way0 it needs locing

With threads that can give contention


(ard#are vs Soft#are

What about the ""+?

"emory "anagement +nit

:ives hardware memory protection between tass

8irtualises memory addressing

Another way to loo at things

-hreads use software to protect data structures

*rocesses use hardware to protect data structures


thread)perf

-hreads vs processes benchmar

httpA//sambaForg/Gtridge/Huncode/thread1perfFc

&ompares common tass

malloc0 setreuid0 readwrite0 stat0 fstat0 create etcF

-hread library

'ining to a thread library can matter for processesI


setre*id&'

A very interesting case

setreuid#% used to change tas credentials

*osix requires change of all thread credentials

Applications often want to change Hust one tas

thread1perf result

setreuid#% with threads over 533x slower on 'inux

Why??
$eory +ootprint

-he memory hierarchy matters

;ow much faster is memory than dis?

What about cache memory vs main memory?

)educing memory footprint

Allows more tass to run

"ay give better use of cache

-hreads

7asier to pac structures in shared memory

'ess fragmentation of memory?

"ay use less memory?


Conc!*sions

(on$obvious choice

-hreads and processes have significant differences

Which is best depends on the application

Systems programming matters

-hin about the /S level implementationI

'earn to use straceI

You might also like