Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
1/25/16
1
CSE30341Opera,ngSystemPrinciples
Lecture5–Processes/Threads
CSE30341–Opera,ngSystemPrinciples 2
Recap
• Processes– Whatisaprocess?– Whatisinaprocesscontrolblock?– Contraststack,heap,data,text.– Whatareprocessstates?– WhichqueuesareusedinanOS?– Whatdoestheschedulerdo?– Whatisacontextswitch?– Whatistheproducer/consumerproblem?– WhatisIPC?
1/25/16
2
CSE30341–Opera,ngSystemPrinciples 3
LectureOverview:Threads
• Overview• Mul,coreProgramming• Mul,threadingModels• ThreadLibraries• ImplicitThreading• ThreadingIssues• Opera,ngSystemExamples
CSE30341–Opera,ngSystemPrinciples 4
Defini,on
• Process:groupresourcestogether• Thread:en,tyscheduledforexecu,oninaprocess
• “Singlesequen?alstreamofinstruc?onswithinaprocess”
• “Lightweightprocess”
1/25/16
3
CSE30341–Opera,ngSystemPrinciples 5
ThreadofExecu,on
registers
code data files
stack registers registers registers
code data files
stackstackstack
thread thread
single-threaded process multithreaded process
CSE30341–Opera,ngSystemPrinciples 6
Threadvs.Process
• Threadshavetheirown:– ThreadID(TID)(comparetoPID)– Programcounter(PC)– Registerset– Stack
• Threadscommonlyshare:– Codesec,on(text)– Datasec,on– Resources(files,signals,etc.)
1/25/16
4
CSE30341–Opera,ngSystemPrinciples 7
WhyThreads?• Enablemul?-taskingwithinanapp– Updatedisplay– Fetchdata– Spellchecking– Answeranetworkrequest
• Reducedcost(“lightweight”process)– Processesareheavytocreate– IPCforthreadscheaper/easierthanprocesses
• Can“simplify”code&increaseefficiency• Kernelsaregenerallymul,threaded(differentthreadsprovidedifferentOSservices)
CSE30341–Opera,ngSystemPrinciples 8
Mul,-ThreadedServer
client
(1) request(2) create new
thread to servicethe request
(3) resume listeningfor additional
client requests
server thread
(threadpool)
1/25/16
5
CSE30341–Opera,ngSystemPrinciples 9
Benefits
• Responsiveness–mayallowcon,nuedexecu,onifpartofprocessisblocked,especiallyimportantforuserinterfaces
• ResourceSharing–threadsshareresourcesofprocess,easierthansharedmemoryormessagepassing
• Economy–cheaperthanprocesscrea,on,threadswitchingloweroverheadthancontextswitching
• Scalability–processcantakeadvantageofmul,processorarchitectures
CSE30341–Opera,ngSystemPrinciples 10
Mul,coreSystems
1/25/16
6
CSE30341–Opera,ngSystemPrinciples 11
Mul,coreProgramming
• Mul?coresystemspuengpressureonprogrammers;challengesinclude:– Dividingac?vi?es(whichtaskstoparallelize)– Balance(if/howtoparallelizetasks)– DataspliNng(howtodividedata)– Datadependency(threadsynchroniza,on)– Tes?nganddebugging(howtotestdifferentexecu,onpaths)
• Parallelismimpliesasystemcanperformmorethanonetasksimultaneously
• Concurrencysupportsmorethanonetaskmakingprogress– Singleprocessor/core,schedulerprovidingconcurrency
CSE30341–Opera,ngSystemPrinciples 12
Concurrencyvs.Parallelism
■ Concurrent execution on single-core system
■ Parallelism on a multi-core system
T1 T2 T3 T4 T1 T2 T3 T4 T1single core
time
…
T1 T3 T1 T3 T1core 1
T2 T4 T2 T4 T2core 2
time
…
…
1/25/16
7
CSE30341–Opera,ngSystemPrinciples 13
Mul,coreProgramming
• Typesofparallelism– Dataparallelism–distributessubsetsofthesamedataacrossmul,plecores,sameopera,ononeach
– Taskparallelism–distribu,ngthreadsacrosscores,eachthreadperforminguniqueopera,on
• As#ofthreadsgrows,sodoesarchitecturalsupportforthreading(“hyperthreading”)– CPUshavecoresaswellashardwarethreads– ConsiderOracleSPARCT4with8coresand8hardwarethreadspercore
CSE30341–Opera,ngSystemPrinciples 14
Datavs.TaskParallelism
• Countnumberof,meseachcharacterinalphabetoccurs
• DataParallelism– Thread1doespage1-100– Thread2doespage100-200
• TaskParallelism– Thread1doesleiersA-F,allpages– Thread2doesleiersG-L,allpages
1/25/16
8
SingleandMul,threadedProcesses
registers
code data files
stack registers registers registers
code data files
stackstackstack
thread thread
single-threaded process multithreaded process
CSE30341–Opera,ngSystemPrinciples 16
UserThreadsandKernelThreads• Userthreads-managementdonebyuser-levelthreadslibrary• Threeprimarythreadlibraries:
– POSIXPthreads– Win32threads– Javathreads
• Kernelthreads-SupportedbytheKernel,“schedulableen?ty”• Examples–virtuallyallgeneralpurposeopera,ngsystems,
including:– Windows– Solaris– Linux– Tru64UNIX– MacOSX
1/25/16
9
CSE30341–Opera,ngSystemPrinciples 17
Mul,threadingModels
• Many-to-One
• One-to-One
• Many-to-Many
CSE30341–Opera,ngSystemPrinciples 18
Many-to-One• Manyuser-levelthreadsmappedto
singlekernelthread• Onethreadblockingcausesallto
block• Mul,plethreadsmaynotrunin
parallelonmul,coresystembecauseonlyonemaybeinkernelata,me
• Fewsystemscurrentlyusethismodel
• Examples:– SolarisGreenThreads– GNUPortableThreads
user thread
kernel threadk
1/25/16
10
CSE30341–Opera,ngSystemPrinciples 19
One-to-One
• Eachuser-levelthreadmapstokernelthread• Crea,ngauser-levelthreadcreatesakernelthread• Moreconcurrencythanmany-to-one• Numberofthreadsperprocesssome,mesrestrictedduetooverhead
• Examples– WindowsNT/XP/2000– Linux– Solaris9andlater
user thread
kernel threadkkkk
CSE30341–Opera,ngSystemPrinciples 20
Many-to-ManyModel• Allowsmanyuserlevel
threadstobemappedtomanykernelthreads
• Allowstheopera,ngsystemtocreateasufficientnumberofkernelthreads
• Solarispriortoversion9
• WindowsNT/2000withtheThreadFiberpackage
user thread
kernel threadkkk
1/25/16
11
CSE30341–Opera,ngSystemPrinciples 21
Two-levelModel
• SimilartoM:M,exceptthatitallowsauserthreadtobeboundtokernelthread
• Examples– IRIX– HP-UX– Tru64UNIX– Solaris8andearlier
user thread
kernel threadkkk k
CSE30341–Opera,ngSystemPrinciples 22
ThreadLibraries
• ThreadlibraryprovidesprogrammerwithAPIforcrea,ngandmanagingthreads
• Twoprimarywaysofimplemen,ng– Libraryen,relyinuserspace– Kernel-levellibrarysupportedbytheOS
1/25/16
12
CSE30341–Opera,ngSystemPrinciples 23
Pthreads
• Maybeprovidedeitherasuser-levelorkernel-level
• APOSIXstandard(IEEE1003.1c)APIforthreadcrea,onandsynchroniza,on
• Specifica7on,notimplementa7on
• APIspecifiesbehaviorofthethreadlibrary,implementa,onisuptodevelopmentofthelibrary
• CommoninUNIXopera,ngsystems(Solaris,Linux,MacOSX)
CSE30341–Opera,ngSystemPrinciples 24
PthreadsExample
1/25/16
13
CSE30341–Opera,ngSystemPrinciples 25
PthreadsExample(Cont.)
CSE30341–Opera,ngSystemPrinciples 26
PthreadsCodeforJoining10Threads
1/25/16
14
CSE30341–Opera,ngSystemPrinciples 27
ImplicitThreading• Growinginpopularityasnumbersofthreadsincrease,programcorrectnessmoredifficultwithexplicitthreads
• Crea,onandmanagementofthreadsdonebycompilersandrun-,melibrariesratherthanprogrammers
• Examples:– ThreadPools– OpenMP– GrandCentralDispatch– MicrosooThreadingBuildingBlocks(TBB)
CSE30341–Opera,ngSystemPrinciples 28
ThreadPools• Createanumberofthreadsinapoolwheretheyawaitwork• Advantages:
– Usuallyslightlyfastertoservicearequestwithanexis,ngthreadthancreateanewthread
– Allowsthenumberofthreadsintheapplica,on(s)tobeboundtothesizeofthepool– Separa,ngtasktobeperformedfrommechanicsofcrea,ngtaskallowsdifferent
strategiesforrunningtask• i.e.,taskscouldbescheduledtorunperiodically
• WindowsAPI:ThreadPool.QueueUserWorkItem(newWaitCallback(ThreadProc));…sta,cvoidThreadProc(Objectstateinfo){…}
1/25/16
15
CSE30341–Opera,ngSystemPrinciples 29
OpenMP• Setofcompilerdirec,vesandanAPI
forC,C++,FORTRAN• Providessupportforparallel
programminginshared-memoryenvironments
• Iden,fiesparallelregions–blocksofcodethatcanruninparallel
#pragma omp parallel Createasmanythreadsastherearecores
#pragma omp parallel for for(i=0;i<N;i++) {
c[i] = a[i] + b[i]; } Runforloopinparallel
CSE30341–Opera,ngSystemPrinciples 30
GrandCentralDispatch
• AppletechnologyforMacOSXandiOSopera,ngsystems• ExtensionstoC,C++languages,API,andrun-,melibrary• Allowsiden,fica,onofparallelsec,ons• Managesmostofthedetailsofthreading• Blockisin“^{}”-ˆ{ printf("I am a block"); } • Blocksplacedindispatchqueue
– Assignedtoavailablethreadinthreadpoolwhenremovedfromqueue• Twotypesofdispatchqueues:
– serial–blocksremovedinFIFOorder,queueisperprocess,calledmainqueue• Programmerscancreateaddi,onalserialqueueswithinprogram
– concurrent–removedinFIFOorderbutseveralmayberemovedata,me• Threesystemwidequeueswithpriori,eslow,default,high
1/25/16
16
CSE30341–Opera,ngSystemPrinciples 31
ThreadingIssues:Seman,csoffork()andexec()
• Doesfork()duplicateonlythecallingthreadorallthreads?– Whenisduplica,ngallthreadsareallybadidea?– SomeOSeshavetwoversionsoffork– POSIX:onlythecallingthread
• Exec()usuallyworksasnormal–replacetherunningprocessincludingallthreads
CSE30341–Opera,ngSystemPrinciples 32
ThreadingIssues:SignalHandling
• SignalsareusedinUNIXsystemstono,fyaprocessthatapar,culareventhasoccurred.
• Asignalhandlerisusedtoprocesssignals1. Signalisgeneratedbypar,cularevent2. Signalisdeliveredtoaprocess3. Signalishandledbyoneoftwosignalhandlers:
1. default2. user-defined
• Everysignalhasdefaulthandlerthatkernelrunswhenhandlingsignal– User-definedsignalhandlercanoverridedefault– Forsingle-threaded,signaldeliveredtoprocess
1/25/16
17
CSE30341–Opera,ngSystemPrinciples 33
ThreadingIssues:ThreadCancella,on
• Terminating a thread before it has finished• Thread to be canceled is target thread• Two general approaches:
– Asynchronous cancellation terminates the target thread immediately
– Deferred cancellation allows the target thread to periodically check if it should be cancelled
• Pthread code to create and cancel a thread:
CSE30341–Opera,ngSystemPrinciples 34
ThreadCancella,on(Cont.)
• Invokingthreadcancella,onrequestscancella,on,butactualcancella,ondependsonthreadstate– pthread_setcancelstate() -> enable/disable – Pthread_setcanceltype():
• Ifthreadhascancella,ondisabled,cancella,onremainspendingun,lthreadenablesit
• Defaulttypeisdeferred– Cancella,ononlyoccurswhenthreadreachescancella?onpoint
• pthread_testcancel() • Asynchronous:terminateimmediately
1/25/16
18
CSE30341–Opera,ngSystemPrinciples 35
Thread-LocalStorage
• Thread-localstorage(TLS)allowseachthreadtohaveitsowncopyofdata
• Usefulwhenyoudonothavecontroloverthethreadcrea,onprocess(i.e.,whenusingathreadpool)
• Differentfromlocalvariables– Localvariablesvisibleonlyduringsinglefunc,oninvoca,on– TLSvisibleacrossfunc,oninvoca,ons
• Similartostaticdata– TLSisuniquetoeachthread
CSE30341–Opera,ngSystemPrinciples 36
LinuxThreads
■ Linuxreferstothemastasksratherthanthreads■ Threadcrea,onisdonethroughclone() systemcall■ clone() allowsachildtasktosharetheaddressspaceofthe
parenttask(process)● Flagscontrolbehavior
flag meaning
CLONE_FS
CLONE_VM
CLONE_SIGHAND
CLONE_FILES
File-system information is shared.
The same memory space is shared.
Signal handlers are shared.
The set of open files is shared.
1/25/16
19
CSE30341–Opera,ngSystemPrinciples 37
Recap
• Whatisathread?Whywouldoneuseathread?
• Howdoesathreaddifferfromaprocess?• Whatarepthreads?• Whatisakernelthread?• Howdoestaskparallelismdifferfromdataparallelism?