threads - swarthmore college · • modern oses separate the concepts of processes and threads. •...

25
Threads 11/15/16

Upload: others

Post on 25-Aug-2020

5 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Summary• Physicallimitstohowmuchfasterwecanmakeasinglecorerun.• Usetransistorstoprovidemorecores.• Parallelizeapplicationstotakeadvantage.

• OSabstraction:thread• Sharesmostoftheaddressspacewithotherthreadsinsameprocess• Getsprivateexecutioncontext(registers)+stack

• Coordinatingthreadsischallenging!