clilenspaceos
spaceos 时间:2021-03-28 阅读:(
)
RajkumarBuyyaSchoolofComputerScienceandSoftwareEngineeringMonashTechnologyMelbourne,AustraliaEmail:rajkumar@ieee.
orgURL:http://www.
dgs.
monash.
edu.
au/~rajkumarConcurrentProgrammingwithThreadsObjectivesExplaintheparallelcomputingrightfromarchitecture,OS,programmingparadigm,andapplicationsExplainthemultithreadingparadigm,andallaspectsofhowtouseitinanapplicationCoverallbasicMTconceptsExploreissuesrelatedtoMTContrastSolaris,POSIX,JavathreadsLookattheAPIsindetailExaminesomeSolaris,POSIX,andJavacodeexamplesDebateon:MPPandClusterComputingAgendaOverviewofComputingOperatingSystemsIssuesThreadsBasicsMultithreadingwithSolarisandPOSIXthreadsMultithreadinginJavaDistributedComputingGrandChallengesSolaris,POSIX,andJavaexamplecodePPPPPPMicrokernelMulti-ProcessorComputingSystemThreadsInterfaceHardwareOperatingSystemProcessProcessorThreadPApplicationsComputingElementsProgrammingparadigmsArchitecturesCompilersApplicationsP.
S.
EsArchitecturesCompilersApplicationsP.
S.
EsSequentialEraParallelEra1940506070809020002030TwoErasofComputingCommercializationR&DCommodityHistoryofParallelProcessingPPcanbetracedtoatabletdatedaround100BC.
Tablethas3calculatingpositions.
Inferthatmultiplepositions:Reliability/SpeedMotivatingFactorsdddJustaswelearnedtofly,notbyconstructingamachinethatflapsitswingslikebirds,butbyapplyingaerodynamicsprinciplesdemonstratedbynature.
.
.
WemodeledPPafterthoseofbiologicalspecies.
Aggregatedspeedwithwhichcomplexcalculationscarriedoutbyindividualneuronsresponseisslow(ms)-demonstratefeasibilityofPPMotivatingFactorsWhyParallelProcessingComputationrequirementsareeverincreasing--visualization,distributeddatabases,simulations,scientificprediction(earthquake),etc.
.
Sequentialarchitecturesreachingphysicallimitation(speedoflight,thermodynamics)TechnicalComputingSolvingtechnologyproblemsusingcomputermodeling,simulationandanalysisLifeSciencesMechanicalDesign&Analysis(CAD/CAM)AerospaceGeographicInformationSystemsNo.
ofProcessorsC.
P.
I.
12ComputationalPowerImprovementMultiprocessorUniprocessorAgeGrowth51015202530354045ComputationalPowerImprovementVerticalHorizontalTheTech.
ofPPismatureandcanbeexploitedcommercially;significantR&Dworkondevelopmentoftools&environment.
SignificantdevelopmentinNetworkingtechnologyispavingawayforheterogeneouscomputing.
WhyParallelProcessingHardwareimprovementslikePipelining,Superscalar,etc.
,arenon-scalableandrequiressophisticatedCompilerTechnology.
VectorProcessingworkswellforcertainkindofproblems.
WhyParallelProcessingParallelProgramhas&needs.
.
.
Multiple"processes"activesimultaneouslysolvingagivenproblem,generalmultipleprocessors.
Communicationandsynchronizationofitsprocesses(formsthecoreofparallelprogrammingefforts).
ProcessingElementsArchitectureSimpleclassificationbyFlynn:(No.
ofinstructionanddatastreams)SISD-conventionalSIMD-dataparallel,vectorcomputingMISD-systolicarraysMIMD-verygeneral,multipleapproaches.
CurrentfocusisonMIMDmodel,usinggeneralpurposeprocessors.
Nosharedmemory)ProcessingElementsSISD:AConventionalComputerSpeedislimitedbytherateatwhichcomputercantransferinformationinternally.
ProcessorDataInputDataOutputInstructionsEx:PC,Macintosh,WorkstationsTheMISDArchitectureMoreofanintellectualexercisethanapracticalconfiguration.
Fewbuilt,butcommerciallynotavailableDataInputStreamDataOutputStreamProcessorAProcessorBProcessorCInstructionStreamAInstructionStreamBInstructionStreamCSIMDArchitectureEx:CRAYmachinevectorprocessing,Thinkingmachinecm*CinoofCPUsP1P2P3timeCPUMultithreading-MultiprocessorsConcurrencyVsParallelismP1P2P3timeNoofexecutionprocess=noofCPUsCPUCPUCPUComputationalModelParallelExecutiondueto:ConcurrencyofthreadsonVirtualProcessorsConcurrencyofthreadsonPhysicalProcessorTrueParallelism:threads:processormap=1:1UserLevelThreadsVirtualProcessorsPhysicalProcessorsUser-LevelSchedule(User)Kernel-LevelSchedule(Kernel)GeneralArchitectureofThreadModelHidesthedetailsofmachinearchitectureMapsUserThreadstokernelthreadsProcessVMisshared,statechangeinVMbyonethreadvisibletoother.
ProcessParallelismintadd(inta,intb,int&result)//functionstuffintsub(inta,intb,int&result)//functionstuffpthreadt1,t2;pthread-create(&t1,add,a,b,&r1);pthread-create(&t2,sub,c,d,&r2);pthread-par(2,t1,t2);MISDandMIMDProcessingabr1cdr2addsubProcessorDataIS1IS2Processordo""dn/2dn2/+1""dnSortDataISDataParallelismsort(int*array,intcount)pthread-t,thread1,thread2;""pthread-create(&thread1,sort,array,N/2);pthread-create(&thread2,sort,array,N/2);pthread-par(2,thread1,thread2);SIMDProcessingSortProcessorProcessorPurposeThreadsModelProcessModelStartexecutionofanewthreadCreationofanewthreadWaitforcompletionofthreadExitanddestroythethreadthr_join()wait()exec()exit()fork()[thr_create()buildsthenewthreadandstartstheexecutionthr_create()thr_exit()ProcessandThreadedmodelsCodeComparisonSegment(Process)main(){fork();fork();fork();}Segment(Thread)main(){thread_create(0,0,func(),0,0);thread_create(0,0,func(),0,0);thread_create(0,0,func(),0,0);}PrintingThreadEditingThreadIndependentThreadsprinting(editing(main(id1=thread_create(printing);id2=thread_create(editing);thread_run(id1,id2)Cooperativethreads-FileCopyreader(lock(buff[i]);read(src,buff[i]);unlock(buff[i]writer(lock(buff[i]);write(src,buff[i]);unlock(buff[i]buff[0]buff[1]CooperativeParallelSynchronizedThreadsRPCCallfunc(){/*Body*/}RPC(func).
.
.
.
.
.
.
.
ClientServerNetworkServerThreadsMessagePassingFacilityServerProcessClientProcessClientProcessUserModeKernelModeMultithreadedServerCompilerThreadPreprocessorThreadMultithreadedCompilerSourceCodeObjectCodeThreadProgrammingmodels1.
Theboss/workermodel2.
Thepeermodel3.
AthreadpipelinetaskXtaskYtaskZmain()WorkersProgramFilesResourcesDatabasesDisksSpecialDevicesBossInput(Stream)Theboss/workermodelExamplemain()/*theboss*/{forever{getarequest;switch(request)caseX:pthread_create(.
.
.
.
,taskX);caseX:pthread_create(.
.
.
.
,taskX);.
.
.
.
}}taskX()/*worker*/{performthetask,syncifaccessingsharedresources}taskY()/*worker*/{performthetask,syncifaccessingsharedresources}.
.
.
.
--Aboveruntimeoverheadofcreatingthreadcanbesolvedbythreadpool*thebossthreadcreatesallworkerthreadatprograminitializationandeachworkerthreadsuspendsitselfimmediatelyforawakeupcallfrombossThepeermodeltaskXtaskYWorkersProgramFilesResourcesDatabasesDisksSpecialDevicestaskZInput(static)Examplemain(){pthread_create(.
.
.
.
,thread1.
.
.
task1);pthread_create(.
.
.
.
,thread2.
.
.
task2);.
.
.
.
signalallworkerstostartwaitforallworkerstofinishdoanycleanup}}task1()/*worker*/{waitforstartperformthetask,syncifaccessingsharedresources}task2()/*worker*/{waitforstartperformthetask,syncifaccessingsharedresources}AthreadpipelineResourcesFilesDatabasesDisksSpecialDevicesFilesDatabasesDisksSpecialDevicesFilesDatabasesDisksSpecialDevicesStage1Stage2Stage3ProgramFilterThreadsInput(Stream)Examplemain(){pthread_create(.
.
.
.
,stage1);pthread_create(.
.
.
.
,stage2);.
.
.
.
waitforallpipelinethreadstofinishdoanycleanup}stage1(){getnextinputfortheprogramdostage1processingoftheinputpassresulttonextthreadinpipeline}stage2(){getinputfrompreviousthreadinpipelinedostage2processingoftheinputpassresulttonextthreadinpipeline}stageN(){getinputfrompreviousthreadinpipelinedostageNprocessingoftheinputpassresulttoprogramoutput.
}MultithreadedMatrixMultiply.
.
.
XA=BCC[1,1]=A[1,1]*B[1,1]+A[1,2]*B[2,1].
.
….
C[m,n]=sumofproductofcorrespondingelementsinrowofAandcolumnofB.
Eachresultantelementcanbecomputedindependently.
MultithreadedMatrixMultiplytypedefstruct{intid;intsize;introw,column;matrix*MA,*MB,*MC;}matrix_work_order_t;main(){intsize=ARRAY_SIZE,row,column;matrix_tMA,MB,MC;matrix_work_order*work_orderp;pthread_tpeer[size*zize];.
.
.
/*processmatrix,byrow,column*/for(row=0;row%d\n",errno);exit(1listen(server_socket,5);while(1){clilen=sizeof(cli_addr);client_socket=accept(server_socket,(structsockaddr*)&serv_addr,&clilen);if(client_socket0IDENTI|FYUSERREQUEST….
DoNECESSARYProcessing….
.
SendResultstoServer}…CLOSEConnectandTerminateTHREADclose(client_socket);#ifdefPOSIX_THREADSpthread_exit((void*)0);#endif}TheValueofMTProgramstructureParallelismThroughputResponsivenessSystemresourceusageDistributedobjectsSinglesourceacrossplatforms(POSIX)SinglebinaryforanynumberofCPUsTothreadornottothreadImproveefficiencyonuniprocessorsystemsUsemultiprocessorHardwareImproveThroughputSimpletoimplementAsynchronousI/OLeveragespecialfeaturesoftheOSTothreadornottothreadIfalloperationsareCPUintensivedonotgofaronmultithreadingThreadcreationisverycheap,itisnotfreethreadthathasonlyfivelinesofcodewouldnotbeusefulDOS-TheMinimalOSUserSpaceKernelSpaceDOSDataStack&StackPointerProgramCounterUserCodeGlobalDataDOSCodeHardwareDOSMultitaskingOSsProcessUserSpaceKernelSpaceHardwareUNIXProcessStructure(UNIX,VMS,MVS,NT,OS/2etc.
)MultitaskingSystemsHardwareTheKernelP1P2P3P4Processes(Eachprocessiscompletelyindependent)MultithreadedProcessUserCodeGlobalDataTheKernelProcessStructure(Kernelstateandaddressspaceareshared)T1'sSPT3'sPCT1'sPCT2'sPCT1'sSPT2'sSPKernelStructuresProcessIDUIDGIDEUIDEGIDCWD.
PrioritySignalMaskRegistersKernelStackCPUStateFileDescriptorsSignalDispatchTableMemoryMapProcessIDUIDGIDEUIDEGIDCWD.
FileDescriptorsSignalDispatchTableMemoryMapTraditionalUNIXProcessStructureSolaris2ProcessStructureLWP2LWP1SchedulingDesignOptionsM:1HP-UNIX1:1DEC,NT,OS/1,AIX.
IRIXM:M2-levelSunOSTwo-LevelThreadModelProc1Proc2Proc3Proc4Proc5TraditionalprocessUserLWPsKernelthreadsKernelHardwareProcessorsThreadLifeCyclemain()main(pthread_create(func,arg);thr_create(.
.
func.
.
,arg.
void*func(pthread_exit()T2T1pthread_create(.
.
.
func.
.
.
)POSIXSolarisWaitingforaThreadtoExitmain()main(pthread_join(T2)thr_join(T2,&val_ptr)void*func(pthread_exit()T2T1pthread_join()POSIXSolarisSchedulingStates:SimplifiedViewofThreadStateTransitionsRUNNABLESLEEPINGSTOPPEDACTIVEStopContinuePreemptStopStopSleepWakeupPreemptionTheprocessofrudelyinterruptingathreadandforcingittorelinquishitsLWP(orCPU)toanother.
CPU2cannotchangeCPU3'sregistersdirectly.
ItcanonlyissueahardwareinterrupttoCPU3.
ItisuptoCPU3'sinterrupthandlertolookatCPU2'srequestanddecidewhattodo.
Higherprioritythreadsalwayspreemptlowerprioritythreads.
Preemption!
=TimeslicingAllofthelibrariesarepreemptiveEXITVs.
THREAD_EXITThenormalCfunctionexit()alwayscausestheprocesstoexit.
Thatmeansalloftheprocess--Allthethreads.
Thethreadexitfunctions:UI:thr_exit()POSIX:pthread_exit()OS/2:DosExitThread()and_endthread()NT:ExitThread()andendthread()allcauseonlythecallingthreadtoexit,leavingtheprocessintactandalloftheotherthreadsrunning.
(Ifnootherthreadsarerunning,thenexit()willbecalled.
)CancellationCancellationisthemeansbywhichathreadcantellanotherthreadthatitshouldexit.
main()main()main(pthread_cancel(T1);DosKillThread(T1);TerminateThread(T1)}}}Thereisnospecialrelationbetweenthekillerofathreadandthevictim.
(UIthreadsmust"rolltheirown"usingsignals)(pthreadexit)(pthreadcancel()T1T2POSIXOS/2WindowsNTCancellationStateandTypeStatePTHREAD_CANCEL_DISABLE(Cannotbecancelled)PTHREAD_CANCEL_ENABLE(Canbecancelled,mustconsidertype)TypePTHREAD_CANCEL_ASYNCHRONOUS(anytimewhat-so-ever)(notgenerallyused)PTHREAD_CANCEL_DEFERRED(Onlyatcancellationpoints)(OnlyPOSIXhasstateandtype)(OS/2iseffectivelyalways"enabledasynchronous")(NTiseffectivelyalways"enabledasynchronous")CancellationisAlwaysComplex!
Itisveryeasytoforgetalockthat'sbeingheldoraresourcethatshouldbefreed.
Usethisonlywhenyouabsolutelyrequireit.
Beextremelymeticulousinanalyzingthepossiblethreadstates.
Document,document,document!
ReturningStatusPOSIXandUIAdetachedthreadcannotbe"joined".
Itcannotreturnstatus.
Anundetachedthreadmustbe"joined",andcanreturnastatus.
OS/2AnythreadcanbewaitedforNothreadcanreturnstatusNothreadneedstobewaitedfor.
NTNothreadscanbewaitedforAnythreadcanreturnstatusSuspendingaThreadmain(thr_suspend(T1)thr_continue(T1);.
.
.
}continue()T2T1suspend()Solaris:*POSIXdoesnotsupportthreadsuspensionProposedUsesofSuspend/ContinueGarbageCollectorsDebuggersPerformanceAnalysersOtherToolsTheseallmustgobelowtheAPI,sotheydon'tcount.
IsolationofVMsystem"spooling"(!
)NTServicesspecifythataserviceshouldbsuspendable(Questionablerequirement)BeCarefulDoNOTThinkaboutScheduling!
ThinkaboutResourceAvailabilityThinkaboutSynchronizationThinkaboutPrioritiesIdeally,ifyou'reusingsuspend/continue,you'remakingamistake!
SynchronizationWebsters:"Torepresentorarrangeeventstoindicatecoincidenceorcoexistence.
"Lewis:"Toarrangeeventssothattheyoccurinaspecifiedorder.
"*Serializedaccesstocontrolledresources.
SynchronizationisnotjustanMPissue.
ItisnotevenstrictlyanMTissue!
ThreadsSynchronization:Onsharedmemory:sharedvariables-semaphoresOndistributedmemory:withinatask:semaphoresAcrossthetasks:BypassingmessagesUnsynchronizedSharedDataisaFormulaforDisasterThread1Thread2temp=Your->BankBalance;dividend=temp*InterestRate;newbalance=dividend+temp;Your->Dividend+=dividend;Your->BankBalance+=deposit;Your->BankBalance=newbalance;AtomicActionsAnactionwhichmustbestartedandcompletedwithnopossibilityofinterruption.
Amachineinstructioncouldneedtobeatomic.
(notallare!
)AlineofCcodecouldneedtobeatomic.
(notallare)Anentiredatabasetransactioncouldneedtobeatomic.
AllMPmachinesprovideatleastonecomplexatomicinstruction,fromwhichyoucanbuildanything.
AsectionofcodewhichyouhaveforcedtobeatomicisaCriticalSection.
CriticalSection(GoodProgrammer!
)reader(lock(DISK)unlock(DISK)writer(lock(DISK)unlock(DISK)SharedDataT1T2CriticalSection(BadProgrammer!
)reader(lock(DISK)unlock(DISK)writer(SharedDataT1T2LockSharedData!
GlobalsShareddatastructuresStaticvariables(reallyjustlexicallyscopedglobalvariables)Mutexesitem=create_and_fill_item();mutex_lock(&m);item->next=list;list=item;mutex_unlock(&m);mutex_lock(&m);this_item=list;list=list_next;mutex_unlock(&m);.
.
.
.
.
func(this-item);POSIXandUI:Ownernotrecorded,blockinpriorityorder.
OS/2andNT.
Ownerrecorded,blockinFIFOorder.
Thread1Thread2SynchronizationVariablesinSharedMemory(CrossProcess)Process1Process2SSSharedMemorySSSynchronizationVariableThreadSynchronizationProblemsDeadlockslock(M1);lock(M2);lock(M2);lock(M1);Thread1Thread2Thread1iswaitingfortheresource(M2)lockedbyThread2andThread2iswaitingfortheresource(M1)lockedbyThread1AvoidingDeadlocksEstablishahierarchy:AlwayslockMutex_1beforeMutex_2,etc.
.
,.
Usethetrylockprimitivesifyoumustviolatethehierarchy.
{while(1){pthread_mutex_lock(&m2)if(EBUSY|=pthreadmutex_trylock(&m1)break;else{pthread_mutex_unlock(&m1)wait_around_or_do_something_else(do_realwork();/*Got`emboth!
Uselockllintorsomesimilarstaticanalysisprogramtoscanyourcodeforhierarchyviolations.
RaceConditionsAraceconditioniswheretheresultsofaprogramaredifferentdependinguponthetimingoftheeventswithintheprogram.
Someraceconditionsresultindifferentanswersandareclearlybugs.
Thread1Thread2mutex_lock(&m)mutex_lock(&m)v=v-1;v=v*2;mutex_unlock(&m)mutex_unlock(&m)-->ifv=1,theresultcanbe0or1basedonwhichthreadgetschancetoenterCRfirstOperatingSystemIssuesLibraryGoalsMakeitfast!
MakeitMTsafe!
RetainUNIXsemantics!
AreLibrariesSafegetc()OLDimplementation:externintget(FILE*pcodetoreaddata*/}getc()NEWimplementation:externintget(FILE*p){pthread_mutex_lock(&m);/*codetoreaddata*/pthread_mutex_unlock(&m);}ERRNOInUNIX,thedistinguishedvariableerrnoisusedtoholdtheerrorcodeforanysystemcallsthatfail.
Clearly,shouldtwothreadsbothbeissuingsystemcallsaroundthesametime,itwouldnotbepossibletofigureoutwhichonesetthevalueforerrno.
Thereforeerrnoisdefinedintheheaderfiletobeacalltothread-specificdata.
Thisisdoneonlywhentheflag_REENTRANT(UI)_POSIX_C_SOURCE=199506L(POSIX)ispassedtothecompiler,allowingolder,non-MTprogramstocontinuetorun.
Thereisthepotentialforproblemsifyouusesomelibrarieswhicharenotreentrant.
(Thisisoftenaproblemwhenusingthirdpartylibraries.
)AreLibrariesSafeMT-SafeThisfunctionissafeMT-HotThisfunctionissafeandfastMT-UnsafeThisfunctionisnotMT-safe,butwascompiledwith_REENTRANTAlternativeCallThisfunctionisnotsafe,butthereisasimilarfunction(e.
g.
getctime_r())MT-IllegalThisfunctionwasn'tevencompiledwith_REENTRANTandthereforecanonlybecalledfromthemainthread.
ThreadsDebuggingInterfaceDebuggersDatainspectorsPerformancemonitorsGarbagecollectorsCoverageanalyzersNotastandardinterface!
TheAPIsDifferentThreadSpecificationsFunctionalityUIThreadsPOSIXThteadsNTThreadsOS/2ThreadsDesignPhilosophyBaseNear-BaseComplexComplexPrimitivesPrimitivesPrimitivesPrimitivesSchedulingClassesLocal/GlobalLocal/GlobalGlobalGlobalMutexesSimpleSimpleComplexComplexCountingSemaphoresSimpleSimpleBuildableBuildableR/WLocksSimpleBuildableBuildableBuildableConditionVariablesSimpleSimpleBuildableBuildableMultiple-ObjectBuildableBuildableComplexComplexSynchronizationThreadSuspensionYesImpossibleYesYesCancellationBuildableYesYesYesThread-SpecificDataYesYesYesYesSignal-HandlingPrimitivesYesYesn/an/aCompilerChangesRequiredNoNoYesNoVendorLibrariesMT-safeMoatMostAllAllISVLibrariesMT-safeSomeSomeSomeSomePOSIXandSolarisAPIDifferencesthreadcancellationschedulingpoliciessyncattributesthreadattributescontinuesuspendsemaphorevarsconcurrencysettingreader/writervarsdaemonthreadsjoinexitkeycreationprioritiessigmaskcreatethreadspecificdatamutexvarskillconditionvarsPOSIXAPISolarisAPIErrorReturnValuesManythreadsfunctionsreturnanerrorvaluewhichcanbelookedupinerrno.
h.
Veryfewthreadsfunctionsseterrno(checkmanpages).
The"lackofresources"errorsusuallymeanthatyou'veusedupallyourvirtualmemory,andyourprogramislikelytocrashverysoon.
AttributeObjectsUI,OS/2,andNTalluseflagsanddirectargumentstoindicatewhatthespecialdetailsoftheobjectsbeingcreatedshouldbe.
POSIXrequirestheuseof"Attributeobjects":thr_create(NULL,NULL,foo,NULL,THR_DETACHED);Vs:pthread_attr_tattr;pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);pthread_create(NULL,&attr,foo,NULL);AttributeObjectsAlthoughabitofpaininthe***comparedtopassingalltheargumentsdirectly,attributeobjectsallowthedesignersofthethreadslibrarymorelatitudetoaddfunctionalitywithoutchangingtheoldinterfaces.
(Iftheydecidetheyreallywantto,say,passthesignalmaskatcreationtime,theyjustaddafunctionpthread_attr_set_signal_mask()insteadofaddinganewargumenttopthread_create().
)Thereareattributeobjectsfor:Threadsstacksize,stackbase,schedulingpolicy,schedulingclass,schedulingscope,schedulinginheritance,detachstate.
MutexesCrossprocess,priorityinheritanceConditionVariablesCrossprocessAttributeObjectsAttributeobjectsmustbe:AllocatedInitializedValuesset(presumably)UsedDestroyed(iftheyaretobefree'd)pthread_attr_tattr;pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED)'pthread_create(NULL,&attr,foo,NULL);pthread_attr_destroy(&attr);ThreadAttributeObjectspthread_attr_t;Threadattributeobjecttype:pthread_attr_init(pthread_mutexattr_t*attr)pthread_attr_destroy(pthread_attr_t*attr)pthread_attr_getdetachstate(pthread_attr_t*attr,in*state)pthread_attr_setdetachstate(pthread_attr_t*attr,intstate)Canthethreadbejoined:pthread_attr_getscope(pthread_attr_t*attr,in*scope)pthread_attr_setscope(pthread_attr_t*attr,intscope)ThreadAttributeObjectspthread_attr_getinheritpolicy(pthread_attr_t*attr,int*policy)pthread_attr_setinheritpolicy(pthread_attr_t*attr,intpolicy)Willthepolicyintheattributeobjectbeusedpthread_attr_getschedpolicy(pthread_attr_t*attr,int*policy)pthread_attr_setschedpolicy(pthread_attr_t*attr,intpolicy)WilltheschedulingbeRR,FIFO,orOTHERpthread_attr_getschedparam(pthread_attr_t*attr,structschedparam*param)pthread_attr_setschedparam(pthreadattr_t*attr,structschedparam*param);WhatwilltheprioritybeThreadAttributeObjectspthread_attr_getinheritsched(pthread_attr_t*attr,int*inheritsched)pthread_attr_setinheritsched(pthread_attr_t*attr,intinheritsched)Willthepolicyintheattributeobjectbeusedpthread_attr_getstacksize(pthread_attr_t*attr,int*size)pthread_attr_setstacksize(pthread_attr_t*attr,intsize)Howbigwillthestackbepthread_attr_getstackaddr(pthread_attr_t*attr,size_t*base)pthread_attr_setstackaddr(pthread_attr_t*attr,size_tbase)Whatwillthestack'sbaseaddressbeMutexAttributeObjectspthread_mutexattr_t;mutexattributeobjecttypepthread_mutexattr_init(pthread_mutexattr_t*attr)pthread_mutexattr_destroy(pthread_mutexattr_t*attr)pthread_mutexattr_getshared(pthread_mutexattr_t*attr,intshared)pthread_mutexattr_setpshared(pthread_mutexattr_t*attr,intshared)WillthemutexbesharedacrossprocessesMutexAttributeObjectspthread_mutexattr_getprioceiling(pthread_mutexattr_t*attr,int*ceiling)pthread_mutexattr_setprioceiling(pthread_mutexattr_t*attr,int*ceiling)Whatisthehighestprioritythethreadowningthismutexcanacquirepthread_mutexattr_getprotocol(pthread_mutexattr_t*attr,int*protocol)pthread_mutexattr_setprotocol(pthread_mutexattr_t*attr,intprotocol)ShallthethreadowningthismutexinheritprioritiesfromwaitingthreadsConditionVariableAttributeObjectspthread_condattr_t;CVattributeobjecttypepthread_condattr_init(pthread_condattr_t*attr)pthread_condattr_destroy(pthread_condattr_t*attr)pthread_condattr_getpshared(pthread_condattr_t*attr,int*shared)pthread_condattr_setpshared(pthread_condattr_t*attr,intshared)WillthemutexbesharedacrossprocessesCreationandDestruction(UI&POSIX)intthr_create(void*stack_base,size_tstacksize,void*(*start_routine)(void*),void*arg,longflags,thread_tthread);voidthr_exit(void*value_ptr);intthr_join(thread_tthread,void**value_ptr);intpthread_create(pthread_t*thread,constpthread_attr_t*attr,voidstart_routine)(void*),void*arg);voidpthread_exit(void*value_ptr);intpthread_join(pthread_tthread,void**value_ptr);intpthread_cancel(pthread_tthread);Suspension(UI&POSIX)intthr_suspend(thread_ttarget)intthr_continue(thread_ttarget)ChangingPriority(UI&POSIX)intthr_setpriority(thread_tthread,intpriority)intthr_getpriority(thread_tthread,int*priority)intpthread_getschedparam(pthread_tthread,int*policy,structschedparam*param)intpthread_setschedparam(pthread_tthread,intpolicy,structschedparam*param)Readers/WriterLocks(UI)intrwlock_init(rwlock_t*rwlock,inttype,void*arg);intrw_rdlock(rwlock_t*rwlock);intrw_wrlock(rwlock_t*rwlock);intrw_tryrdlock(rwlock_t*rwlock);intrw_trywrlock(rwlock_t*rwlock);intrw_unlock(rwlock_t*rwlock);intrw_destroy(rwlock_t*rwlock);(Counting)Semaphores(UI&POSIX)intsema_init(sema_t*sema,unsignedintsema_count,inttype,void*arg)intsema_wait(sema_t*sema)intsema_post(sema_t*sema)intsema_trywait(sema_t*sema)intsema_destroy(sema_t*sema)intsem_init(sem_t*sema,intpshared,unsignedintcount)intsem_post(sem_t*sema)intsem_trywait(sem_t*sema)intsem_destroy(sem_t*sema)(POSIXsemaphoresarenotpartofpthread.
Usethelibposix4.
soandposix4.
h)ConditionVariables(UI&POSIX)intcond_init(contd_t*cond,inttype,void*arg)intcond_wait(cond_t*cond,mutex_t*mutex);intcond_signal(cond_t*cond)intcond_broadcast(cond_t*cond)intcond_timedwait(cond_t*cond,mutex_t*mutex,timestruc_t*abstime)intcond_destroy(cond_t*cond)intpthread_cond_init(pthread_cond_t*cond,pthread_condattr_t*attr)intpthread_cond_wait(pthread_cond_t*cond,pthread_mutex_t*mutex)intpthread_cond_signal(pthread_cond_t*cond)intpthread_cond_broadcast(pthread_cond_t*cond,pthread_mutex_t*mutex,structtimespec*abstime)intpthread_cond_destroy(pthread_cond_t*cond)Signals(UI&POSIX)intthr_sigsetmask(inthow,constsigset_t*set,sigset_t*oset);intthr_kill(thread_ttargetthread,intsig)intsigwait(sigset_t*set)intpthread_sigmask(inthow,constsigset_t*set,sigset_t*oset);intpthread_kill(thread_ttarget_thread,intsig)intsigwait(sigset_t*set,int*sig)Cancellation(POSIX)intpthread_cancel(pthread_thread_tthread)intpthreadcleanup_pop(intexecute)intpthread_cleanup_push(void(*funtion)(void*),void*arg)intpthread_setcancelstate(intstate,int*old_state)intpthread_testcancel(void)OtherAPIsthr_self(void)thr_yield()intpthread_atfork(void(*prepare)(void),void(*parent)(void),void(*child)(void)pthread_equal(pthread_thread_ttl,pthread_thread_tt2)pthread_once(pthread_once_t*once_control,void(*init_routine)(void))pthread_self(void)pthread_yield()(ThreadIDsinSolarisrecycleevery2^32threads,oraboutonceamonthifyoudocreate/exitasfastaspossible.
)CompilingSolarisLibrariesSolarishasthreelibraries:libthread.
so,libpthread.
so,libposix4.
soCorrespondingnewincludefiles:synch.
h,thread.
h,pthread.
h,posix4.
hBundledwithallO/SreleasesRunninganMTprogramrequiresnoextraeffortCompilinganMTprogramrequiresonlyacompiler(anycompiler!
)WritinganMTprogramrequiresonlyacompiler(butafewMTtoolswillcomeinveryhandy)CompilingUIunderSolarisCompilingisnodifferentthanfornon-MTprogramslibthreadisjustanothersystemlibraryin/usr/libExample:%cc-osemasema.
c-lthread-D_REENTRANT%cc-osemasema.
c-mtAllmultithreadedprogramsshouldbecompiledusingthe_REENTRANTflagAppliesforeverymoduleinanewapplicationIfomitted,theolddefinitionsforerrno,stdiowouldbeused,whichyoudon'twantAllMT-safelibrariesshouldbecompiledusingthe_REENTRANTflag,eventhoughtheymaybeusedsingleinathreadedprogram.
CompilingPOSIXunderSolarisCompilingisnodifferentthanfornon-MTprogramslibpthreadisjustanothersystemlibraryin/usr/libExample:%cc-osemasema.
c-lpthread-lposix4-D_POSIX_C_SOURCE=19956LAllmultithreadedprogramsshouldbecompiledusingthe_POSIX_C_SOURCE=199506LflagAppliesforeverymoduleinanewapplicationIfomitted,theolddefinitionsforerrno,stdiowouldbeused,whichyoudon'twantAllMT-safelibrariesshouldbecompiledusingthe_POSIX_C_SOURCE=199506Lflag,eventhoughtheymaybeusedsingleinathreadedprogramCompilingmixedUI/POSIXunderSolarisIfyoujustwanttousetheUIthreadfunctions(e.
g.
,thr_setconcurrency())%cc-osemasema.
c-1thread-1pthread-1posix4D_REENTRANT-_POSIX_PTHREAD_SEMANTICSIfyoualsowanttousetheUIsemanticsforfork(),alarms,timers,sigwait(),etc.
,.
SummaryThreadsprovideamorenaturalprogrammingparadigmImproveefficiencyonuniprocessorsystemsAllowstotakefulladvantageofmultiprocessorHardwareImproveThroughput:simpletoimplementasynchronousI/OLeveragespecialfeaturesoftheOSManyapplicationsarealreadymultithreadedMTisnotasilverbulletforallprogrammingproblems.
Threreisalreadystandardformultithreading--POSIXMultithreadingsupportalreadyavailableintheformoflanguagesyntax--JavaThreadsallowstomodeltherealworldobject(ex:inJava)JavaMultithreadinginJavaJava-AnIntroductionJava-ThenewprogramminglanguagefromSunMicrosystemsJava-AllowsanyonetopublishawebpagewithJavacodeinitJava-CPUIndependentlanguageCreatedforconsumerelectronicsJava-James,ArthurVan,andothersJava-ThenamethatsurvivedapatentsearchOak-ThepredecessorofJavaJavais"C+ObjectOrientedLanguages-AcomparisonSundefinesJavaas:SimpleandPowerfulSafeObjectOrientedRobustArchitectureNeutralandPortableInterpretedandHighPerformanceThreadedDynamicJavaIntegratesPowerofCompiledLanguagesandFlexibilityofInterpretedLanguagesClassesandObjectsClassesandObjectsMethodOverloadingMethodOverridingAbstractClassesVisibilitymodifiersdefaultpublicprotectedprivateprotected,privateThreadsJavahasbuiltinthreadsupportforMultithreadingSynchronizationThreadSchedulingInter-ThreadCommunication:currentThreadstartsetPriorityyieldrungetPrioritysleepstopsuspendresumeJavaGarbageCollectorisalow-prioritythreadWaysofMultithreadinginJavaCreateaclassthatextendstheThreadclassCreateaclassthatimplementstheRunnableinterface1stMethod:ExtendingtheThreadclassclassMyThreadextendsThread{publicvoidrun(threadbodyofexecution}}Creatingthread:MyThreadthr1=newMyThread();StartExecution:thr1.
start();2ndmethod:ThreadsbyimplementingRunnableinterfaceclassClassNameimplementsRunnable{publicvoidrun(threadbodyofexecution}}CreatingObject:ClassNamemyObject=newClassName();CreatingThreadObject:Threadthr1=newThread(myObject);StartExecution:thr1.
start();ThreadClassMembers.
.
.
publicclassjava.
lang.
Threadextendsjava.
lang.
Objectimplementsjava.
lang.
Runnable{//FieldspublicfinalstaticintMAX_PRIORITY;publicfinalstaticintMIN_PRIORITY;publicfinalstaticintNORM_PRIORITY;//ConstructorspublicThread();publicThread(Runnabletarget);publicThread(Runnabletarget,Stringname);publicThread(Stringname);publicThread(ThreadGroupgroup,Runnabletarget);publicThread(ThreadGroupgroup,Runnabletarget,Stringname);publicThread(ThreadGroupgroup,Stringname);//MethodspublicstaticintactiveCount();publicvoidcheckAccess();publicintcountStackFrames();publicstaticThreadcurrentThread();publicvoiddestroy();publicstaticvoiddumpStack();publicstaticintenumerate(Threadtarray[]);publicfinalStringgetName();.
.
.
ThreadClassMembers.
publicfinalintgetPriority();//1to10priority-pre-emptionatmid.
publicfinalThreadGroupgetThreadGroup();publicvoidinterrupt();publicstaticbooleaninterrupted();publicfinalbooleanisAlive();publicfinalbooleanisDaemon();publicbooleanisInterrupted();publicfinalvoidjoin();publicfinalvoidjoin(longmillis);publicfinalvoidjoin(longmillis,intnanos);publicfinalvoidresume();publicvoidrun();publicfinalvoidsetDaemon(booleanon);publicfinalvoidsetName(Stringname);publicfinalvoidsetPriority(intnewPriority);publicstaticvoidsleep(longmillis);publicstaticvoidsleep(longmillis,intnanos);publicvoidstart();publicfinalvoidstop();publicfinalvoidstop(Throwableobj);publicfinalvoidsuspend();publicStringtoString();publicstaticvoidyield();}ManipulationofCurrentThread//CurrentThreadDemo.
javaclassCurrentThreadDemo{publicstaticvoidmain(Stringarg[Threadct=Thread.
currentThread(ct.
setName("MyThread"System.
out.
println("CurrentThread:"+ct);tryfor(inti=5;i>0;i-System.
out.
println(""+i)Thread.
sleep(1000)catch(InterruptedExceptione)System.
out.
println("Interrupted.
Run:CurrentThread:Thread[MyThread,5,main]54321CreatingnewThread.
.
.
//ThreadDemo.
javaclassThreadDemoimplementsRunnable{ThreadDemo(Threadct=Thread.
currentThread(System.
out.
println("CurrentThread:"+ct);Threadt=newThread(this,"DemoThread"t.
start(tryThread.
sleep(3000)catch(InterruptedExceptione)System.
out.
println("Interrupted.
System.
out.
println("Exitingmainthread.
");}.
.
.
CreatingnewThread.
publicvoidrun(tryfor(inti=5;i>0;i-System.
out.
println(""+i)Thread.
sleep(1000)catch(InterruptedExceptione)System.
out.
println("Childinterrupted.
System.
out.
println("Exitingchildthread.
publicstaticvoidmain(Stringargs[newThreadDemo();}}Run:CurrentThread:Thread[main,5,main]543Exitingmainthread.
21Exitingchildthread.
ThreadPriority.
.
.
//HiLoPri.
javaclassClickerimplementsRunnable{intclick=0;privateThreadt;privatebooleanrunning=true;publicClicker(intp)t=newThread(this);t.
setPriority(p)publicvoidrun(while(running)click+publicvoidstart(t.
start(publicvoidstop(running=false;}}.
.
.
ThreadPriorityclassHiLoPri{publicstaticvoidmain(Stringargs[Thread.
currentThread().
setPriority(Thread.
MAX_PRIORITY);ClickerHi=newClicker(Thread.
NORM_PRIORITY+2);ClickerLo=newClicker(Thread.
NORM_PRIORITY-2);Lo.
start(Hi.
start(tryThread.
sleep(10000)catch(Exceptione)Lo.
stop(Hi.
stop(System.
out.
println(Lo.
click+"vs.
"+Hi.
click);}}Run1:(onSolaris)0vs.
956228Run2:(Window95)304300vs.
4066666TheJavamonitormodelMethod1Method2Block1KeyThreadsMonitor(synchronised)solvesrace-conditionproblemThreadsSynchronisation.
.
.
//Synch.
java:race-conditionwithoutsynchronisationclassCallme{//Checksynchronizedandunsynchronizedmethods/*synchronized*/voidcall(Stringmsg)System.
out.
print("["+msg);tryThread.
sleep(1000)catch(Exceptione)System.
out.
println(classCallerimplementsRunnable{Stringmsg;CallmeTarget;publicCaller(Callmet,Strings)Target=t;msg=s;newThread(this).
start();}.
.
.
ThreadsSynchronisation.
publicvoidrun(Target.
call(msg);}}classSynch{publicstaticvoidmain(Stringargs[CallmeTarget=newCallme(newCaller(Target,"Hello"newCaller(Target,"Synchronized"newCaller(Target,"World");}}Run1:Withunsynchronizedcallmethod(racecondition)[Hello[Synchronized[World]]]Run2:Withsynchronizedcallmethod[Hello][Synchronized][World]Run3:WithSynchronizedobjectsynchronized(Target){Target.
call(msg);}TheoutputisthesameasRun2Queue(nointer-threadedcommunication).
.
.
//pc.
java:produceandconsumerclassQueue{intn;synchronizedintget(System.
out.
println("Got:"+n);returnn;}synchronizedvoidput(intn)this.
n=n;System.
out.
println("Put:"+n);}}classProducerimplementsRunnable{QueueQ;Producer(Queueq)Q=q;newThread(this,"Producer").
start();}Queue(nointer-threadedcommunication).
.
.
publicvoidrun(inti=0;while(true)Q.
put(i+classConsumerimplementsRunnable{QueueQ;Consumer(Queueq)Q=q;newThread(this,"Consumer").
start(publicvoidrun(while(true)Q.
get();}}.
.
.
Queue(nointer-threadedcommunication).
classPC{publicstaticvoidmain(String[]args)QueueQ=newQueue(newProducer(Q);newConsumer(Q);}}Run:Put:1Got:1Got:1Got:1Put:2Put:3Got:3^CQueue(interthreadcommunication).
.
.
//PCnew.
java:produce-consumenrwithinterthreadcommunicationclassQueue{intn;booleanValueSet=false;synchronizedintget(tryif(!
ValueSet)wait(catch(InterruptedExceptione)System.
out.
println("Got:"+n);ValueSet=false;notify(returnn;}Queue(interthreadcommunication).
.
.
synchronizedvoidput(intn)tryif(ValueSet)wait(catch(InterruptedExceptione)this.
n=n;System.
out.
println("Put:"+n);ValueSet=true;notify();}}classProducerimplementsRunnable{QueueQ;Producer(Queueq)Q=q;newThread(this,"Producer").
start();}Queue(interthreadcommunication).
.
.
publicvoidrun(inti=0;while(true)Q.
put(i+classConsumerimplementsRunnable{QueueQ;Consumer(Queueq)Q=q;newThread(this,"Consumer").
start(publicvoidrun(while(true)Q.
get();}}.
.
.
Queue(nointerthreadcommunication).
classPCnew{publicstaticvoidmain(String[]args)QueueQ=newQueue(newProducer(Q);newConsumer(Q);}}Run:Put:0Got:0Put:1Got:1Put:2Got:2Put:3Got:3Put:4Got:4^CDeadlock.
.
.
//DeadLock.
javaclassA{synchronizedvoidfoo(Bb)Stringname=Thread.
currentThread().
getName(System.
out.
println(name+"enteredA.
foo"tryThread.
sleep(1000)catch(Exceptione)System.
out.
println(name+"tryingtocallB.
last(b.
last(synchronizedvoidlast(System.
out.
println("InsideA.
last");}}Deadlock.
.
.
classB{synchronizedvoidbar(Aa)Stringname=Thread.
currentThread().
getName(System.
out.
println(name+"enteredB.
bar"tryThread.
sleep(1000)catch(Exceptione)System.
out.
println(name+"tryingtocallA.
last(a.
last(synchronizedvoidlast(System.
out.
println("InsideB.
last");}}.
.
.
Deadlock.
classDeadLockimplementsRunnable{Aa=newA();Bb=newB();DeadLock(Thread.
currentThread().
setName("MainThread"newThread(this).
start(a.
foo(b);System.
out.
println("Backinthemainthread.
publicvoidrun(Thread.
currentThread().
setName("RacingThread"b.
bar(a);System.
out.
println("Backintheotherthread"publicstaticvoidmain(Stringargs[newDeadLock();}}Run:MainThreadenteredA.
fooRacingThreadenteredB.
barMainThreadtryingtocallB.
last()RacingThreadtryingtocallA.
last()^CGrandChallenges(IsPPPractical)NeedOSandCompilersupporttousemultiprocessormachines.
Idealwouldbefortheusertobeunawareiftheproblemisrunningonsequentialorparallelhardware-alongwaytogo.
WithHighspeedNetworksandimprovedmicroprocessorperformance,multiplestand-alonemachinescanalsobeusedasaparallelmachine-aPopularTrend.
(appealingvehicleforparallelcomputing)Languagestandardshavetoevolve.
(Portability).
Re-orientationofthinkingSequentialParallelGrandChallenges(IsPPPractical)Languagestandardshavetoevolve.
(Portability).
Re-orientationofthinkingSequentialParallelBreakingHighPerformanceComputingBarriers210021002100210021002100210021002100SingleProcessorSharedMemoryLocalParallelClusterGlobalParallelClusterGFLOPSThankYou.
.
.
TMThosting发布了今年黑色星期五的促销活动,即日起到12月6日,VPS主机最低55折起,独立服务器9折起,开设在西雅图机房。这是一家成立于2018年的国外主机商,主要提供VPS和独立服务器租用业务,数据中心包括美国西雅图和达拉斯,其中VPS基于KVM架构,都有提供免费的DDoS保护,支持选择Windows或者Linux操作系统。Budget HDD系列架构CPU内存硬盘流量系统价格单核51...
virmach这是第二波出这种一次性周期的VPS了,只需要缴费1一次即可,用完即抛,也不允许你在后面续费。本次促销的是美国西海岸的圣何塞和美国东海岸的水牛城,周期为6个月,过后VPS会被自动且是强制性取消。需要临时玩玩的,又不想多花钱的用户,可以考虑下!官方网站:https://www.virmach.comTemporary Length Service Specials圣何塞VPS-一次性6个...
Vinahost,这个主机商还是第一次介绍到,翻看商家的介绍信息,是一家成立于2008年的老牌越南主机商,业务涵盖网站设计、域名、SSL证书、电子邮箱、虚拟主机、越南VPS、云计算、越南服务器出租以及设备托管等,机房主要在越南胡志明市的Viettle和VNPT数据中心,其中VNPT数据中心对于国内是三网直连,速度优。类似很多海外主机商一样,希望拓展自己的业务,必须要降价优惠或者增加机房迎合需求用户...
spaceos为你推荐
京沪高铁上市首秀我能买京沪高铁股票吗psbc.com邮政银行卡6215995915000241921是哪个地区的rawtoolsRAW是什么衣服牌子porntimesexy time 本兮 MP3地址广告法有那些广告法?还有广告那些广告词?baqizi.cc孔融弑母是真的吗?www.1diaocha.com哪个网站做调查问卷可以赚钱 啊鹤城勿扰黑龙江省的那个 城市是被叫做鹤城?bihaiyinsha以前在碧海银沙游戏城的那个打气球的游戏叫什么?百度关键字百度推广关键词匹配方式有哪些?
虚拟主机购买 手机域名注册 已备案未注册域名 免费com域名申请 a5域名交易 火山主机 息壤主机 mach l5520 流媒体服务器 php探针 网通代理服务器 cdn联盟 空间合租 免费phpmysql空间 网站在线扫描 江苏双线服务器 后门 rewritecond 服务器托管价格 更多