threads - swarthmore college · • modern oses separate the concepts of processes and threads. •...
TRANSCRIPT
![Page 1: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/1.jpg)
Threads11/15/16
![Page 2: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/2.jpg)
CS31teachesyou…
• Howacomputerrunsaprogram.• Howthehardwareperformscomputations• Howthecompilertranslatesyourcode• Howtheoperatingsystemconnectshardwareandsoftware
• Theimplicationsforyouasaprogrammer• Pipelininginstructions• Caching• Virtualmemory• Processswitching• SupportforParallelprogramming(threads)
![Page 3: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/3.jpg)
Transistors(*10^3)
ClockSpeed(MHZ)
Power(W)
ILP(IPC)
Whydowecareaboutparallel?
![Page 4: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/4.jpg)
Moore’sLaw
• Circuitdensity(numberoftransistorsinafixedarea)doublesroughlyeverytwoyears.
• Thisusedtomeanthatclockspeeddoubledtoo.• Allyourprogramsruntwiceasfastforfree.• Problem:heat
• Fornow,circuitdensityisstillincreasing.Howcanwemakeuseofit?
![Page 5: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/5.jpg)
The“Multi-CoreEra”
• Wecan’tmakeasinglecoregomuchfaster.• WecanusetheextratransistorstoputmultipleCPUcoresonthechip.
• Thisisexciting:CPUscandoalotmore!• Problem:it’snowuptotheprogrammertotakeadvantageofmultiplecores.• Humansarebadatthinkinginparallel…
![Page 6: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/6.jpg)
ParallelAbstraction
• Tospeedupajob,youhavetodivideitacrossmultiplecores.
• Aprocesscontainsbothexecutioninformationandmemory/resources.
• Whatifwewanttoseparatetheexecutioninformationtogiveusparallelismwithinaprocess?
![Page 7: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/7.jpg)
Threads
• ModernOSes separatetheconceptsofprocessesandthreads.• Theprocessdefinestheaddressspaceandgeneralprocessattributes(e.g.,openfiles).• Thethreaddefinesasequentialexecutionstreamwithinaprocess(PC,SP,registers),
• Athreadisboundtoasingleprocess.• Processes,however,canhavemultiplethreads.• Eachprocesshasatleastonethread.
![Page 8: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/8.jpg)
Threads
Text
Data
Stack1
Thread1PC1
SP1
Process1
OS
Heap
Thisisthepicturewe’vebeenusingallalong:
Aprocesswithasinglethread,whichhasexecutionstate(registers)andastack.
![Page 9: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/9.jpg)
Threads
Thread1PC1
SP1
Thread2
PC2
SP2
Process1
Text
Data
Stack1
OS
Heap
Stack2
Wecanaddathreadtotheprocess.Newthreadsshareallmemory(VAS)withotherthreads.
Newthreadgetsprivateregisters,localstack.
![Page 10: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/10.jpg)
Threads
Thread1PC1
SP1
Thread2
Thread3
PC2
SP2PC3
SP3
Process1
Text
Data
Stack1
OS
Heap
Stack2
Stack3
Athirdthreadadded.
Note:they’reallexecutingthesameprogram(sharedinstructionsintext),thoughtheymaybeatdifferentpointsinthecode.
![Page 11: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/11.jpg)
Threads• Private:tid,copyofregisters,executionstack• Shared:everythingelseintheprocess
11
tid0tid1…tidm
...
per-threadstacks
Process:+ Sharingiseasy+ Sharingischeap
nodatacopyfromonePi’saddressspacetoanotherPj’s addressspace
+ Threadcreatefasterthanprocess+ OScanscheduleonmultipleCPUs
+ Parallelism- Coordination/Synchronization
- Howtonotmuck-upeachother’sstate- Can’tusethreadsindistributedsystems
(whencooperatingPis areondifferentcomputers)
![Page 12: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/12.jpg)
ProgrammingThreads
12
...
EveryProcesshas1threadofexecution• Thesinglemainthreadexecutesfrombeginning
Anexamplethreadedprogram’sexecution:1. Mainthreadofteninitializes
sharedstate2. Thenspawns multiplethreads3. Setofthreadsexecuteconcurrently
toperformsometask4. Mainthreadmaydoajoin,towaitfor
otherthreadstoexit(likewait&reapingprocesses)5. Mainthreadmaydosomefinalsequentialprocessing(like
writeresultstoafile)
![Page 13: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/13.jpg)
LogicalViewofThreads
13
• Threadsformapoolofpeersw/inaprocess(Unlikeprocesseswhichformatreehierarchy)
pwd sh ls
ls
T1
ProcesshierarchyThreadsassociatedwithaprocess
T2T0
T4 T3
sharedcode,data
bash
![Page 14: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/14.jpg)
ThreadConcurrency
14
SingleCoreProcessorSimulatebytimeslicing
Multi-CoreProcessorTrueconcurrency
Time
ThreadA ThreadB ThreadC ThreadA ThreadB ThreadC
Runonmultiplecores
Threads’ExecutionControlFlowsOverlap
![Page 15: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/15.jpg)
Concurrency?
• Severalcomputationsorthreadsofcontrolareexecutingsimultaneously,andpotentiallyinteractingwitheachother.
• Wecanmultitask!Whydoesthathelp?• TakingadvantageofmultipleCPUs/cores• OverlappingI/Owithcomputation
![Page 16: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/16.jpg)
Whyusethreadsoverprocesses?Separatingthreadsandprocessesmakesiteasiertosupportparallelapplications:
• Creatingmultiplepathsofexecutiondoesnotrequirecreatingnewprocesses(lessstatetostore,initialize).
• Low-overheadsharingbetweenthreadsinsameprocess(threadssharepagetables,accesssamememory).
![Page 17: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/17.jpg)
Threads&Sharing
• Code(text)sharedbyallthreadsinprocess• Globalvariablesandstaticobjectsareshared• Storedinthestaticdatasegment,accessiblebyanythread
• Dynamicobjectsandotherheapobjectsareshared• Allocatedfromheapwithmalloc/freeornew/delete
• LocalvariablescanBUTSHOULDNOTbeshared• Refertodataonthestack• Eachthreadhasitsownstack• Neverpass/share/storeapointertoalocalvariableonanotherthread’sstack
![Page 18: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/18.jpg)
Threads&Sharing
• Localvariablesshouldnotbeshared• Refertodataonthestack• Eachthreadhasitsownstack• Neverpass/share/storeapointertoalocalvariableonanotherthread’sstack
…
functionC
functionD
…
functionA
functionB
SharedHeapint *x;
Z
Thread1’sstack Thread2’sstack
Thread2candereferencextoaccessZ.FunctionBreturns…
![Page 19: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/19.jpg)
Threads&Sharing
• Localvariablesshouldnotbeshared• Refertodataonthestack• Eachthreadhasitsownstack• Neverpass/share/storeapointertoalocalvariableonanotherthread’sstack
…
functionC
functionD
…
functionA
functionB
SharedHeapint *x;
Thread1’sstack Thread2’sstack
Thread2candereferencextoaccessZ.
Z
Shareddataonheap!
![Page 20: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/20.jpg)
Thread-levelParallelism• SpeedupapplicationbyassigningportionstoCPUs/coresthatprocessinparallel
• Requires:• partitioningresponsibilities(e.g.,parallelalgorithm)• managingtheirinteraction
• Example:processinganarray
Onecore: Fourcores:
![Page 21: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/21.jpg)
IfoneCPUcorecanrunaprogramatarateofX,howquicklywilltheprogramrunontwocores?
A. Slowerthanonecore(<X)B. Thesamespeed(X)C. Fasterthanonecore,butnotdouble(X-2X)D. Twiceasfast(2X)E. Morethantwiceasfast(>2X)
![Page 22: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/22.jpg)
ParallelSpeedup
• Performancebenefitofparallelthreadsdependsonmanyfactors:• algorithmdivisibility• communicationoverhead• memoryhierarchyandlocality• implementationquality
• Formostprograms,morethreadsmeansmorecommunication,diminishingreturns.
![Page 23: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/23.jpg)
Example
23
static int x;
int foo(int *p) {int y;
y = 3;y = *p;*p = 7;x += y;
} Heap:
Stack:
0x0
Globals:
Instructions:
max
Tid jTid i
Ifthreadsi andjbothexecutefunctionfoocode:Q1:whichvariablesdotheyeach
getowncopyof?whichdotheyshare?
Q2:whichstmts canaffectvaluesseenbytheotherthread?
SharedVirtualAddressSpace:
foo:
![Page 24: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/24.jpg)
Example
24
static int x;
int foo(int *p) {int y;
y = 3;y = *p;*p = 7;x += y;
}
Eachtid getsitsowncopyofyonitsstack
x isinglobalmemoryandissharedbyeverythread
Heap:
Stack:
0x0
Globals:
Instructions:
max
p isparameter,eachtid getsitsowncopyofp.However,pcouldpointanintstoragelocation:onthestack,oringlobalmem,orontheheap,oreveninanother’sstackframe
x:10
y:3
p:-
y:3
p:-
Tid jTid i
![Page 25: Threads - Swarthmore College · • Modern OSes separate the concepts of processes and threads. • The process defines the address space and general process attributes (e.g., open](https://reader035.vdocuments.us/reader035/viewer/2022071101/5fda9666bb426017793c7f96/html5/thumbnails/25.jpg)
Summary• Physicallimitstohowmuchfasterwecanmakeasinglecorerun.• Usetransistorstoprovidemorecores.• Parallelizeapplicationstotakeadvantage.
• OSabstraction:thread• Sharesmostoftheaddressspacewithotherthreadsinsameprocess• Getsprivateexecutioncontext(registers)+stack
• Coordinatingthreadsischallenging!