资源预览内容
第1页 / 共169页
第2页 / 共169页
第3页 / 共169页
第4页 / 共169页
第5页 / 共169页
第6页 / 共169页
第7页 / 共169页
第8页 / 共169页
第9页 / 共169页
第10页 / 共169页
亲,该文档总共169页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
1Rajkumar BuyyaSchool of Computer Science and Software EngineeringMonash TechnologyMelbourne, AustraliaEmail: rajkumarieee.orgURL: :/ dgs.monash.edu.au/rajkumarConcurrent Programming with Threads2ObjectivescExplain the parallel computing right from architecture, OS, programming paradigm, and applicationscExplain the multithreading paradigm, and all aspects of how to use it in an applicationcCover all basic MT conceptscExplore issues related to MTcContrast Solaris, POSIX, Java threadscLook at the APIs in detailcExamine some Solaris, POSIX, and Java code examplescDebate on: MPP and Cluster Computing3 Agenda+ Overview of Computing+ Operating Systems Issues+ Threads Basics+ Multithreading with Solaris and POSIX threads+ Multithreading in Java+ Distributed Computing+ Grand Challenges+ Solaris, POSIX, and Java example code 4PPPPPP MicrokernelMulti-Processor Computing SystemThreads InterfaceHardwareOperating SystemProcessProcessorThreadPApplicationsComputing ElementsProgramming paradigms5 Architectures Compilers Applications Architectures Compilers Applications P.S.Es SequentialEraParallelEra1940 50 60 70 80 90 2000 2030Two Eras of Computing Commercialization R & D Commodity6History of Parallel ProcessingZPP can be traced to a tablet dated around 100 BC.uTablet has 3 calculating positions.uInfer that multiple positions:Reliability/ Speed7Motivating Factorsddd(Just as we learned to fly, not by constructing a machine that flaps its wings like birds, but by applying aerodynamics principles demonstrated by nature. sWe modeled PP after those of biological species.8Aggregated speed with which complex calculations carried out by individual neurons response is slow (ms) - demonstrate feasibility of PPMotivating Factors9Why Parallel Processing?Computation requirements are ever increasing - visualization, distributed databases, simulations, scientific prediction (earthquake), etc.Sequential architectures reaching physical limitation (speed of light, thermodynamics)10Technical ComputingSolving technology problems usingcomputer modeling, simulation and analysisLife SciencesLife SciencesMechanical Design & Analysis (CAD/CAM)Mechanical Design & Analysis (CAD/CAM)AerospaceAerospaceGeographicGeographicInformationInformationSystemsSystems11No. of ProcessorsC.P.I.1 2 . . . .Computational Power ImprovementMultiprocessorUniprocessor12AgeGrowth5 10 15 20 25 30 35 40 45 . . . . Computational Power ImprovementVerticalHorizontal13The Tech. of PP is mature and can be exploited commercially; significant R & D work on development of tools & environment.Significant development in Networking technology is paving a way for heterogeneous computing.Why Parallel Processing?14Hardware improvements like Pipelining, Superscalar, etc., are non-scalable and requires sophisticated Compiler Technology.Vector Processing works well for certain kind of problems.Why Parallel Processing?15Parallel Program has & needs .Multiple “processes active simultaneously solving a given problem, general multiple processors.Communication and synchronization of its processes (forms the core of parallel programming efforts).16Processing Elements Architecture17Simple classification by Flynn: (No. of instruction and data streams)SISD - conventionalSIMD - data parallel, vector computingMISD - systolic arraysMIMD - very general, multiple approaches.Current focus is on MIMD model, using general purpose processors. (No shared memory)Processing Elements18SISD : A Conventional ComputerSpeed is limited by the rate at which computer can transfer information internally.ProcessorData InputData OutputInstructionsEx:PC, Macintosh, Workstations19The MISDArchitectureMore of an intellectual exercise than a practical configuration. Few built, but commercially not availableData InputStreamData OutputStreamProcessorAProcessorBProcessorCInstructionStream AInstructionStream BInstruction Stream C20SIMD ArchitectureEx: CRAY machine vector processing, Thinking machine cm*Ci no of CPUsNumber of Simulatneous execution units no of CPUsP1P1P2P2P3P3timetimeCPU42Multithreading - Multithreading - MultiprocessorsMultiprocessorsConcurrency Vs Parallelism Concurrency Vs Parallelism Concurrency Vs Parallelism P1P1P2P2P3P3timetimeNo of execution process = no of CPUsNo of execution process = no of CPUsCPUCPUCPU43Computational ModelComputational ModelParallel Execution due to :Parallel Execution due to :HHConcurrency of threads on Virtual ProcessorsConcurrency of threads on Virtual ProcessorsHHConcurrency of threads on Physical ProcessorConcurrency of threads on Physical ProcessorTrue Parallelism :True Parallelism :threads : processor map = 1:1threads : processor map = 1:1User Level ThreadsVirtual ProcessorsPhysical ProcessorsUser-Level Schedule (User)Kernel-Level Schedule (Kernel)44General Architecture ofGeneral Architecture ofThread ModelThread ModelHides Hides the the details details of of machine machine architecturearchitectureMaps Maps User User Threads Threads to to kernel kernel threadsthreadsProcess Process VM VM is is shared, shared, state state change change in in VM VM by by one one thread thread visible visible to to other.other.45Process ParallelismProcess Parallelismint add (int a, int b, int & result)int add (int a, int b, int & result)/ function stuff/ function stuffint sub(int a, int b, int & result)int sub(int a, int b, int & result)/ function stuff/ function stuffpthread t1, t2;pthread-create(&t1, add, a,b, & r1);pthread-create(&t2, sub, c,d, & r2);pthread-par (2, t1, t2);MISD and MIMD ProcessingMISD and MIMD Processingabr1cdr2addsubProcessorDataIS1IS2Processor46do“dn/2dn2/+1“dnSortDataISData ParallelismData Parallelismsort( int *array, int count)sort( int *array, int count)/./././.pthread-t, thread1, thread2;“pthread-create(& thread1, sort, array, N/2);pthread-create(& thread2, sort, array, N/2);pthread-par(2, thread1, thread2);SIMD ProcessingSIMD ProcessingSortProcessorProcessor47PurposePurposeThreads Threads ModelModelProcess Process ModelModelStart execution of a new threadStart execution of a new threadCreation of a new threadCreation of a new threadWait for completion of Wait for completion of threadthreadExit and destroy the Exit and destroy the threadthreadthr_join()thr_join()wait( )wait( )exec( )exec( )exit( )exit( )fork ( )fork ( ) thr_create() builds thr_create() builds the new thread and the new thread and starts the executionstarts the executionthr_create( )thr_create( )thr_exit()thr_exit()Process and Threaded modelsProcess and Threaded models48Code ComparisonCode ComparisonSegment (Process)Segment (Process)main ( )main ( )fork ( );fork ( );fork ( );fork ( );fork ( );fork ( );Segment(Thread)Segment(Thread)main()main()thread_create(0,0,func(),0,0);thread_create(0,0,func(),0,0);thread_create(0,0,func(),0,0);thread_create(0,0,func(),0,0);thread_create(0,0,func(),0,0);thread_create(0,0,func(),0,0);49Printing ThreadPrinting ThreadEditing ThreadEditing Thread50Independent ThreadsIndependent Threadsprinting()printing()- - - - - - - - - - - - - - - - - - - - - - -editing()editing()- - - - - - - - - - - - - - - - - - - - - - -main()main()- - - - - - - - - - - - - - - - - - - - - - -id1 = thread_create(printing);id1 = thread_create(printing);id2 = thread_create(editing);id2 = thread_create(editing);thread_run(id1, id2);thread_run(id1, id2);- - - - - - - - - - - - - - - - - - - - - - -51Cooperative threads - File CopyCooperative threads - File Copyreader()reader()- - - - - - - - - - - - - - - - - - -lock(buffi);lock(buffi);read(src,buffi);read(src,buffi);unlock(buffi);unlock(buffi);- - - - - - - - - - - - - - - - - - -writer()writer()- - - - - - - - - - - - - - - - - - -lock(buffi);lock(buffi);write(src,buffi);write(src,buffi);unlock(buffi);unlock(buffi);- - - - - - - - - - - - - - - - - - -buff0buff1Cooperative Parallel Synchronized Threads52RPC CallRPC Callfunc()func()/* Body */* Body */RPC(func)RPC(func).ClientClientServerServerNetworkNetwork53ServerThreadsMessage PassingFacilityServer ProcessClient ProcessClient ProcessUser ModeKernel ModeMultithreaded Server54Compiler ThreadPreprocessor ThreadMultithreaded CompilerSourceCodeObjectCode55Thread Programming models1. The boss/worker model2. The peer model3. A thread pipeline56taskXtaskYtaskZmain ( )WorkersProgramFilesResourcesDatabasesDisksSpecialDevicesBossInput (Stream)The boss/worker model57Examplemain() /* the boss */ forever get a request;switch( request )case X: pthread_create(.,taskX);case X: pthread_create(.,taskX);.taskX() /* worker */ perform the task, sync if accessing shared resourcestaskY() /* worker */ perform the task, sync if accessing shared resources.-Above runtime overhead of creating thread can be solved by thread pool* the boss thread creates all worker thread at program initialization and each worker thread suspends itself immediately for a wakeup call from boss 58The peer modeltaskXtaskYWorkersProgramFilesResourcesDatabasesDisksSpecialDevicestaskZInput(static)59Examplemain() pthread_create(.,thread1.task1); pthread_create(.,thread2.task2);. signal all workers to start wait for all workers to finish do any cleanuptask1() /* worker */wait for start perform the task, sync if accessing shared resourcestask2() /* worker */wait for start perform the task, sync if accessing shared resources60A thread pipelineResourcesFilesDatabasesDisksSpecial DevicesFilesDatabasesDisksSpecial DevicesFilesDatabasesDisksSpecial DevicesStage 1Stage 2Stage 3ProgramFilter ThreadsInput (Stream)61Examplemain() pthread_create(.,stage1);pthread_create(.,stage2);.wait for all pipeline threads to finishdo any cleanupstage1() get next input for the programdo stage 1 processing of the inputpass result to next thread in pipelinestage2()get input from previous thread in pipelinedo stage 2 processing of the inputpass result to next thread in pipelinestageN()get input from previous thread in pipelinedo stage N processing of the inputpass result to program output.62Multithreaded Matrix Multiply.XA= BCC1,1 = A1,1*B1,1+A1,2*B2,1.Cm,n=sum of product of corresponding elements in row of A and column of B.Each resultant element can be computed independently.63Multithreaded Matrix Multiplytypedef struct int id; int size;int row, column;matrix *MA, *MB, *MC; matrix_work_order_t;main() int size = ARRAY_SIZE, row, column;matrix_t MA, MB,MC;matrix_work_order *work_orderp;pthread_t peersize*zize;./* process matrix, by row, column */for( row = 0; row size; row+ ) for( column = 0; column size; column+) id = column + row * ARRAY_SIZE; work_orderp = malloc( sizeof(matrix_work_order_t);/* initialize all members if wirk_orderp */pthread_create(peerid, NULL, peer_mult, work_orderp); /* wait for all peers to exist*/ for( i =0; i size*size;i+)pthread_join( peeri, NULL );64Multithreaded Server.void main( int argc, char *argv ) int server_socket, client_socket, clilen; struct sockaddr_in serv_addr, cli_addr; int one, port_id;#ifdef _POSIX_THREADSpthread_t service_thr;#endif port_id = 4000;/* default port_id */if( (server_socket = socket( AF_INET, SOCK_STREAM, 0 ) 0 ) printf(Error: Unable to open socket in parmon server.n);exit( 1 ); memset( (char*) &serv_addr, 0, sizeof(serv_addr); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons( port_id ); setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one); 65Multithreaded Server. if( bind( server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr) %dn,errno );exit( 1 ); listen( server_socket, 5); while( 1 ) clilen = sizeof(cli_addr);client_socket = accept( server_socket, (struct sockaddr *)&serv_addr, &clilen );if( client_socket 0 ) IDENTI|FY USER REQUEST .Do NECESSARY Processing .Send Results to Server CLOSE Connect and Terminate THREAD close( client_socket );#ifdef POSIX_THREADS pthread_exit( (void *)0);#endif 67The Value of MTProgram structureParallelismThroughputResponsivenessSystem resource usageDistributed objectsSingle source across platforms (POSIX)Single binary for any number of CPUs68To thread or not to threadTo thread or not to threadKKImprove efficiency onImprove efficiency on uniprocessor uniprocessor systemssystemsKKUse multiprocessor HardwareUse multiprocessor HardwareKKImprove ThroughputImprove ThroughputccSimple to implement Asynchronous I/OSimple to implement Asynchronous I/OKKLeverage special features of the OSLeverage special features of the OS69To thread or not to threadTo thread or not to threadKKIf all operations are CPU intensive do If all operations are CPU intensive do not go far on multithreadingnot go far on multithreadingKKThread creation is very cheap, it is Thread creation is very cheap, it is not freenot freeccthread that has only five lines of code thread that has only five lines of code would not be usefulwould not be useful70DOS - The Minimal OSUserSpaceKernelSpaceDOSDataStack & Stack PointerProgram CounterUserCodeGlobalDataDOSCodeHardwareDOS71Multitasking OSsProcessUserSpaceKernelSpaceHardwareUNIXProcess Structure(UNIX, VMS, MVS, NT, OS/2 etc.)72Multitasking SystemsHardwareThe KernelP1P2P3P4Processes(Each process is completely independent)73Multithreaded ProcessUserCodeGlobalDataThe KernelProcess Structure(Kernel state and address space are shared)T1s SP T3sPC T1sPC T2sPCT1s SPT2s SP74Kernel StructuresProcess IDUID GID EUID EGID CWD.PrioritySignal MaskRegistersKernel StackCPU StateFile DescriptorsSignal Dispatch TableMemory MapProcess IDUID GID EUID EGID CWD.File DescriptorsSignal Dispatch TableMemory MapTraditional UNIX Process StructureSolaris 2 Process StructureLWP 2LWP 175Scheduling Design Options M:1HP-UNIX 1:1DEC, NT, OS/1, AIX. IRIXM:M2-level76SunOS Two-Level Thread ModelProc 1Proc 2Proc 3Proc 4Proc 5TraditionalprocessUserLWPsKernelthreadsKernelHardwareProcessors77Thread Life Cyclemain()main() . pthread_create( func, arg); thr_create( .func.,arg.); . . void * func() .pthread_exit()T2T1pthread_create(.func.)POSIXSolaris78Waiting for a Thread to Exitmain()main() . pthread_join(T2); thr_join( T2,&val_ptr); . . void * func() .pthread_exit()T2T1pthread_join()POSIXSolaris79Scheduling States: Simplified View of Thread State TransitionsRUNNABLESLEEPINGSTOPPEDACTIVEStopContinuePreemptStopStopSleepWakeup80PreemptionThe process of rudely interrupting a thread and forcing it to relinquish its LWP (or CPU) to another.CPU2 cannot change CPU3s registers directly. It can only issue a hardware interrupt to CPU3. It is up to CPU3s interrupt handler to look at CPU2s request and decide what to do.Higher priority threads always preempt lower priority threads.Preemption ! = Time slicingAll of the libraries are preemptive81EXIT Vs. THREAD_EXITThe normal C function exit() always causes the process to exit. That means all of the process - All the threads.The thread exit functions:UI: thr_exit()POSIX: pthread_exit()OS/2: DosExitThread() and _endthread()NT: ExitThread() and endthread()all cause only the calling thread to exit, leaving the process intact and all of the other threads running. (If no other threads are running, then exit() will be called.)82CancellationCancellation is the means by which a thread can tell another thread that it should exit.main()main()main().pthread_cancel (T1); DosKillThread(T1);TerminateThread(T1)There is no special relation between the killer of a thread and the victim. (UI threads must “roll their own using signals)(pthread exit)(pthread cancel()T1T2POSIXOS/2Windows NT83Cancellation State and TypecStatecPTHREAD_CANCEL_DISABLE (Cannot be cancelled)cPTHREAD_CANCEL_ENABLE (Can be cancelled, must consider type)cTypecPTHREAD_CANCEL_ASYNCHRONOUS (any time what-so-ever) (not generally used)cPTHREAD_CANCEL_DEFERREDc(Only at cancellation points)c(Only POSIX has state and type)c(OS/2 is effectively always “enabled asynchronous)c(NT is effectively always “enabled asynchronous)84Cancellation is Always Complex!cIt is very easy to forget a lock thats being held or a resource that should be freed.cUse this only when you absolutely require it.cBe extremely meticulous in analyzing the possible thread states.cDocument, document, document!85Returning StatuscPOSIX and UIcA detached thread cannot be “joined. It cannot return status.cAn undetached thread must be “joined, and can return a status.cOS/2cAny thread can be waited forcNo thread can return statuscNo thread needs to be waited for.cNTcNo threads can be waited forcAny thread can return status86Suspending a Threadmain() . thr_suspend(T1); . thr_continue(T1); .continue()T2T1suspend()Solaris:* POSIX does not support thread suspension87Proposed Uses of Suspend/ContinuecGarbage CollectorscDebuggerscPerformance AnalyserscOther Tools?cThese all must go below the API, so they dont count.cIsolation of VM system “spooling (?!)cNT Services specify that a service should b suspendable (Questionable requirement?)cBe Careful88Do NOT Think about Scheduling!cThink about Resource AvailabilitycThink about SynchronizationcThink about PrioritiesIdeally, if youre using suspend/ continue, youre making a mistake!89SynchronizationcWebsters: “To represent or arrange events to indicate coincidence or coexistence.cLewis : “To arrange events so that they occur in a specified order.c* Serialized access to controlled resources.cSynchronization is not just an MP issue. It is not even strictly an MT issue!90* Threads Synchronization :Threads Synchronization :wwOn On shared shared memory memory : : shared shared variables variables - - semaphoressemaphoreswwOn distributed memory :On distributed memory :within a task : within a task : semaphoressemaphoresAcross the tasks : Across the tasks : By passing messagesBy passing messages91Unsynchronized Shared Data is a Formula for DisasterThread1Thread2temp = Your - BankBalance;dividend = temp * InterestRate;newbalance = dividend + temp;Your-Dividend += dividend; Your-BankBalance+= deposit;Your-BankBalance = newbalance;92Atomic ActionscAn action which must be started and completed with no possibility of interruption.cA machine instruction could need to be atomic. (not all are!)cA line of C code could need to be atomic. (not all are)cAn entire database transaction could need to be atomic.cAll MP machines provide at least one complex atomic instruction, from which you can build anything.cA section of code which you have forced to be atomic is a Critical Section.93Critical SectionCritical Section(Good Programmer!)(Good Programmer!)reader()reader()- - - - - - - - - - - - - - - - - - -lock(DISK);lock(DISK);.unlock(DISK);unlock(DISK);- - - - - - - - - - - - - - - - - - -writer()writer()- - - - - - - - - - - - - - - - - - -lock(DISK);lock(DISK);.unlock(DISK);unlock(DISK);- - - - - - - - - - - - - - - - - - -Shared DataT1T294Critical SectionCritical Section(Bad Programmer!)(Bad Programmer!)reader()reader()- - - - - - - - - - - - - - - - - - -lock(DISK);lock(DISK);.unlock(DISK);unlock(DISK);- - - - - - - - - - - - - - - - - - -writer()writer()- - - - - - - - - - - - - - - - - - -.- - - - - - - - - - - - - - - - - - -Shared DataT1T295Lock Shared Data!cGlobalscShared data structurescStatic variables(really just lexically scoped global variables)96Mutexesitem = 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);cPOSIX and UI : Owner not recorded, block in priority order.cOS/2 and NT. Owner recorded, block in FIFO order.Thread 1Thread297Synchronization Variables in Shared Memory (Cross Process)Process 1Process 2SSShared MemorySS SynchronizationVariableThread98SynchronizationProblems99Deadlockslock( M1 );lock( M2 );lock( M2 );lock( M1 );Thread 1Thread 2Thread1 is waiting for the resource(M2) locked by Thread2 andThread2 is waiting for the resource (M1) locked by Thread1100Avoiding DeadlockscEstablish a hierarchy : Always lock Mutex_1 before Mutex_2, etc.,.cUse the trylock primitives if you must violate the hierarchy. while (1) pthread_mutex_lock (&m2); if( EBUSY |= pthread mutex_trylock (&m1) break; else pthread _mutex_unlock (&m1); wait_around_or_do_something_else(); do_real work();/* Got em both! */ cUse lockllint or some similar static analysis program to scan your code for hierarchy violations.101Race ConditionsA race condition is where the results of a program are different depending upon the timing of the events within the program.Some race conditions result in different answers and are clearly bugs.Thread 1Thread 2mutex_lock (&m)mutex_lock (&m)v = v - 1;v = v * 2;mutex_unlock (&m)mutex_unlock (&m)- if v = 1, the result can be 0 or 1based on which thread gets chance to enter CR first102Operating System Issues103Library GoalscMake it fast!cMake it MT safe!cRetain UNIX semantics!104Are Libraries Safe ?getc() OLD implementation: extern int get( FILE * p ) /* code to read data */ getc() NEW implementation: extern int get( FILE * p ) pthread_mutex_lock(&m); /* code to read data */pthread_mutex_unlock(&m); 105ERRNOIn UNIX, the distinguished variable errno is used to hold the error code for any system calls that fail.Clearly, should two threads both be issuing system calls around the same time, it would not be possible to figure out which one set the value for errno.Therefore errno is defined in the header file to be a call to thread-specific data.This is done only when the flag_REENTRANT (UI)_POSIX_C_SOURCE=199506L (POSIX) is passed to the compiler, allowing older, non-MT programs to continue to run.There is the potential for problems if you use some libraries which are not reentrant. (This is often a problem when using third party libraries.)106Are Libraries Safe?cMT-SafeThis function is safecMT-HotThis function is safe and fastcMT-UnsafeThis function is not MT-safe, but was compiled with _REENTRANTcAlternative Call This function is not safe, but there is a similar function (e.g. getctime_r()cMT-IllegalThis function wasnt even compiled with _REENTRANT and therefore can only be called from the main thread.107Threads Debugging InterfacecDebuggerscData inspectorscPerformance monitorscGarbage collectorscCoverage analyzersNot a standard interface!108The APIs109Different Thread SpecificationsFunctionalityUI ThreadsPOSIX ThteadsNT ThreadsOS/2 Threads Design PhilosophyBaseNear-BaseComplexComplexPrimitivesPrimitivesPrimitivesPrimitivesScheduling ClassesLocal/ GlobalLocal/GlobalGlobalGlobalMutexesSimpleSimpleComplexComplexCounting Semaphores SimpleSimpleBuildableBuildableR/W Locks SimpleBuildableBuildableBuildableCondition VariablesSimpleSimpleBuildableBuildableMultiple-ObjectBuildableBuildableComplexComplexSynchronizationThread SuspensionYesImpossibleYes YesCancellationBuildableYesYesYesThread-Specific DataYesYesYesYesSignal-Handling PrimitivesYesYesn/an/aCompiler ChangesRequiredNoNoYesNoVendor Libraries MT-safe? MoatMostAll?All?ISV Libraries MT-safe? SomeSomeSomeSome110POSIX and Solaris API Differences thread cancellationscheduling policiessync attributes thread attributescontinuesuspend semaphore vars concurrency setting reader/ writer vars daemon threadsjoinexitkey creationpriorities sigmask create thread specific data mutex varskillcondition vars POSIX APISolaris API111Error Return ValuescMany threads functions return an error value which can be looked up in errno.h.cVery few threads functions set errno(check man pages).cThe “lack of resources errors usually mean that youve used up all your virtual memory, and your program is likely to crash very soon.112Attribute ObjectsUI, OS/2, and NT all use flags and direct arguments to indicate what the special details of the objects being created should be. POSIX requires the use of “Attribute objects:thr_create(NULL, NULL, foo, NULL, THR_DETACHED);Vs:pthread_attr_t attr;pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);pthread_create(NULL, &attr, foo, NULL);113Attribute ObjectsAlthough a bit of pain in the * compared to passing all the arguments directly, attribute objects allow the designers of the threads library more latitude to add functionality without changing the old interfaces. (If they decide they really want to, say, pass the signal mask at creation time, they just add a function pthread_attr_set_signal_mask() instead of adding a new argument to pthread_create().)There are attribute objects for:Threadsstack size, stack base, scheduling policy, scheduling class, scheduling scope, scheduling inheritance, detach state.MutexesCross process, priority inheritanceCondition VariablesCross process114Attribute ObjectsAttribute objects must be:AllocatedInitializedValues set (presumably)UsedDestroyed (if they are to be freed)pthread_attr_t attr;pthread_attr_init (&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)pthread_create(NULL, &attr, foo, NULL);pthread_attr_destroy (&attr);115Thread Attribute Objectspthread_attr_t;Thread attribute object type: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, int state)Can the thread be joined?:pthread_attr_getscope(pthread_attr_t *attr, in *scope)pthread_attr_setscope(pthread_attr_t *attr, int scope)116Thread Attribute Objectspthread_attr_getinheritpolicy(pthread_attr_t *attr, int *policy)pthread_attr_setinheritpolicy(pthread_attr_t *attr, int policy)Will the policy in the attribute object be used?pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy)pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)Will the scheduling be RR, FIFO, or OTHER?pthread_attr_getschedparam(pthread_attr_t *attr, struct sched param *param)pthread_attr_setschedparam(pthread attr_t *attr, struct sched param *param);What will the priority be?117Thread Attribute Objectspthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched)pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)Will the policy in the attribute object be used?pthread_attr_getstacksize(pthread_attr_t *attr, int *size)pthread_attr_setstacksize(pthread_attr_t *attr, int size)How big will the stack be?pthread_attr_getstackaddr (pthread_attr_t *attr, size_t *base)pthread_attr_setstackaddr(pthread_attr_t *attr, size_t base)What will the stacks base address be?118Mutex Attribute Objectspthread_mutexattr_t;mutex attribute object typepthread_mutexattr_init(pthread_mutexattr_t *attr)pthread_mutexattr_destroy(pthread_mutexattr_t *attr)pthread_mutexattr_getshared(pthread_mutexattr_t*attr, int shared)pthread_mutexattr_setpshared (pthread_mutex attr_t *attr, int shared)Will the mutex be shared across processes?119Mutex Attribute Objectspthread_mutexattr_getprioceiling(pthread_mutexattr_t*attr, int *ceiling)pthread_mutexattr_setprioceiling(pthread_mutexattr_t*attr, int *ceiling)What is the highest priority the thread owning this mutex can acquire?pthread_mutexattr_getprotocol (pthread_mutexattr_t*attr, int *protocol)pthread_mutexattr_setprotocol (pthread_mutexattr_t*attr, int protocol)Shall the thread owning this mutex inherit priorities from waiting threads?120Condition Variable Attribute Objectspthread_condattr_t;CV attribute object typepthread_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, int shared)Will the mutex be shared across processes?121Creation and Destruction (UI & POSIX)int thr_create(void *stack_base, size_t stacksize, void *(*start_routine) (void *), void* arg, long flags, thread_t thread);void thr_exit (void *value_ptr);int thr_join (thread_t thread, void *value_ptr);int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine) (void *), void *arg);void pthread_exit (void *value_ptr);int pthread_join (pthread_t thread, void *value_ptr);int pthread_cancel (pthread_t thread);122Suspension (UI & POSIX)int thr_suspend(thread_t target)int thr_continue(thread_t target)123Changing Priority (UI & POSIX)int thr_setpriority(thread_t thread, int priority)int thr_getpriority(thread_t thread, int *priority)int pthread_getschedparam(pthread_t thread, int*policy, struct sched param*param)int pthread_setschedparam(pthread_t thread, intpolicy, struct sched param *param)124Readers / Writer Locks (UI)int rwlock_init(rwlock_t *rwlock, int type, void *arg);int rw_rdlock(rwlock_t *rwlock);int rw_wrlock(rwlock_t *rwlock);int rw_tryrdlock(rwlock_t *rwlock);int rw_trywrlock(rwlock_t *rwlock);int rw_unlock(rwlock_t *rwlock);int rw_destroy(rwlock_t *rwlock);125(Counting) Semaphores (UI & POSIX)int sema_init(sema_t *sema, unsigned int sema_count, int type, void *arg)int sema_wait(sema_t *sema)int sema_post(sema_t *sema)int sema_trywait(sema_t *sema)int sema_destroy(sema_t *sema)int sem_init(sem_t *sema, int pshared, unsigned int count)int sem_post(sem_t *sema)int sem_trywait(sem_t *sema)int sem_destroy(sem_t *sema)(POSIX semaphores are not part of pthread. Use the libposix4.so and posix4.h)126Condition Variables (UI & POSIX)int cond_init(contd_t *cond, int type, void *arg)int cond_wait(cond_t *cond, mutex_t *mutex);int cond_signal(cond_t *cond)int cond_broadcast(cond_t *cond)int cond_timedwait(cond_t *cond, mutex_t *mutex, timestruc_t *abstime)int cond_destroy (cond_t *cond)int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *attr)int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)int pthread_cond_signal (pthread_cond_t *cond)int pthread_cond_broadcast(pthread_cond_t *cond, pthread_mutex_t *mutex, struct timespec *abstime)int pthread_cond_destroy(pthread_cond_t *cond)127Signals (UI & POSIX)int thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset);int thr_kill(thread_t target thread, int sig)int sigwait(sigset_t *set)int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);int pthread_kill(thread_t target_thread, int sig)int sigwait(sigset_t *set, int *sig)128Cancellation (POSIX)int pthread_cancel(pthread_thread_t thread)int pthread cleanup_pop(int execute)int pthread_cleanup_push(void (*funtion) (void *), void *arg)int pthread_setcancelstate(int state, int *old_state)int pthread_testcancel(void)129Other APIsthr_self(void)thr_yield()int pthread_atfork (void (*prepare) (void),void (*parent) (void),void (*child) (void)pthread_equal (pthread_thread_t tl, pthread_thread_t t2)pthread_once (pthread_once_t *once_control, void(*init_routine) (void)pthread_self (void)pthread_yield()(Thread IDs in Solaris recycle every 232 threads, or about once a month if you do create/exit as fast as possible.)130Compiling131Solaris LibrariescSolaris has three libraries: libthread.so, libpthread.so, libposix4.socCorresponding new include files: synch.h, thread.h, pthread.h, posix4.hcBundled with all O/S releasescRunning an MT program requires no extra effortcCompiling an MT program requires only a compiler (any compiler!)cWriting an MT program requires only a compiler (but a few MT tools will come in very handy)132Compiling UI under SolariscCompiling is no different than for non-MT programsclibthread is just another system library in /usr/libcExample:%cc -o sema sema.c -lthread -D_REENTRANT%cc -o sema sema.c -mtcAll multithreaded programs should be compiled using the _REENTRANT flagcApplies for every module in a new applicationcIf omitted, the old definitions for errno, stdio would be used, which you dont wantcAll MT-safe libraries should be compiled using the _REENTRANT flag, even though they may be used single in a threaded program.133Compiling POSIX under SolariscCompiling is no different than for non-MT programsclibpthread is just another system library in /usr/libcExample :%cc-o sema sema.c -lpthread -lposix4 -D_POSIX_C_SOURCE=19956LcAll multithreaded programs should be compiled using the _POSIX_C_SOURCE=199506L flagcApplies for every module in a new applicationcIf omitted, the old definitions for errno, stdio would be used, which you dont wantcAll MT-safe libraries should be compiled using the _POSIX_C_SOURCE=199506L flag, even though they may be used single in a threaded program134Compiling mixed UI/POSIX under SolariscIf you just want to use the UI thread functions (e.g., thr_setconcurrency()%cc-o sema sema.c -1thread -1pthread -1posix4 D_REENTRANT -_POSIX_PTHREAD_SEMANTICSIf you also want to use the UI semantics for fork(), alarms, timers, sigwait(), etc.,.135SummaryKKThreads provide a more natural programming paradigmThreads provide a more natural programming paradigmKKImprove efficiency onImprove efficiency on uniprocessor uniprocessor systems systemsKKAllows to take full advantage of multiprocessor HardwareAllows to take full advantage of multiprocessor HardwareKKImprove Throughput: simple to implement asynchronous Improve Throughput: simple to implement asynchronous I/OI/OKKLeverage special features of the OSLeverage special features of the OSKKMany applications are already multithreadedMany applications are already multithreadedKKMT is not a silver bullet for all programming problems.MT is not a silver bullet for all programming problems.KKThrereThrere is already standard for multithreading-POSIX is already standard for multithreading-POSIXKKMultithreading support already available in the form of Multithreading support already available in the form of language syntax-Javalanguage syntax-JavaKKThreads allows to model the real world object (ex: in Java)Threads allows to model the real world object (ex: in Java)136JavaMultithreading in Java137Java - An IntroductioncJava - The new programming language from Sun MicrosystemscJava -Allows anyone to publish a web page with Java code in itcJava - CPU Independent languagecCreated for consumer electronicscJava - James , Arthur Van , and others cJava -The name that survived a patent searchcOak -The predecessor of JavacJava is “C+ - + “138Object Oriented Languages -A comparison139Sun defines Java as:ccSimple and PowerfulSimple and PowerfulccSafeSafeccObject OrientedObject OrientedccRobustRobustccArchitecture Neutral and PortableArchitecture Neutral and PortableccInterpreted and High PerformanceInterpreted and High PerformanceccThreaded Threaded cc Dynamic Dynamic140 Java Integrates Power of Compiled Languagesand Flexibility of Interpreted Languages141Classes and Objects cClasses and ObjectscMethod OverloadingcMethod OverridingcAbstract ClassescVisibility modifiersdefaultpublicprotectedprivate protected , private142ThreadscJava has built in thread support for MultithreadingcSynchronization cThread SchedulingcInter-Thread Communication:currentThreadstartsetPriorityyieldrungetPrioritysleepstopsuspendresumecJava Garbage Collector is a low-priority thread143Ways of Multithreading in JavacCreate a class that extends the Thread classcCreate a class that implements the Runnable interfacec1st Method: Extending the Thread class class MyThread extends Thread public void run() / thread body of execution cCreating thread: MyThread thr1 = new MyThread();cStart Execution: thr1.start();1442nd method: Threads by implementing Runnable interfaceclass ClassName implements Runnable . public void run() / thread body of execution cCreating Object: ClassName myObject = new ClassName();cCreating Thread Object: Thread thr1 = new Thread( myObject );cStart Execution: thr1.start();145Thread Class Members.public class java.lang.Thread extends java.lang.Object implements java.lang.Runnable / Fieldspublic final static int MAX_PRIORITY;public final static int MIN_PRIORITY;public final static int NORM_PRIORITY;/ Constructorspublic Thread();public Thread(Runnable target);public Thread(Runnable target, String name);public Thread(String name);public Thread(ThreadGroup group, Runnable target);public Thread(ThreadGroup group, Runnable target, String name);public Thread(ThreadGroup group, String name);/ Methodspublic static int activeCount();public void checkAccess();public int countStackFrames();public static Thread currentThread();public void destroy();public static void dumpStack();public static int enumerate(Thread tarray);public final String getName();146.Thread Class Members.public final int getPriority(); / 1 to 10 priority-pre-emption at mid.public final ThreadGroup getThreadGroup();public void interrupt();public static boolean interrupted();public final boolean isAlive();public final boolean isDaemon();public boolean isInterrupted();public final void join();public final void join(long millis);public final void join(long millis, int nanos);public final void resume();public void run();public final void setDaemon(boolean on);public final void setName(String name);public final void setPriority(int newPriority);public static void sleep(long millis);public static void sleep(long millis, int nanos);public void start();public final void stop();public final void stop(Throwable obj);public final void suspend();public String toString();public static void yield();147Manipulation of Current Thread/ CurrentThreadDemo.javaclass CurrentThreadDemo public static void main(String arg) Thread ct = Thread.currentThread(); ct.setName( My Thread ); System.out.println(Current Thread : +ct); try for(int i=5; i0; i-) System.out.println( + i); Thread.sleep(1000); catch(InterruptedException e) System.out.println(Interrupted.); Run:Current Thread : ThreadMy Thread,5,main 5 4 3 2 1148Creating new Thread./ ThreadDemo.javaclass ThreadDemo implements Runnable ThreadDemo() Thread ct = Thread.currentThread(); System.out.println(Current Thread : +ct); Thread t = new Thread(this,Demo Thread); t.start(); try Thread.sleep(3000); catch(InterruptedException e) System.out.println(Interrupted.); System.out.println(Exiting main thread.); 149.Creating new Thread.public void run() try for(int i=5; i0; i-) System.out.println( + i); Thread.sleep(1000); catch(InterruptedException e) System.out.println(Child interrupted.); System.out.println(Exiting child thread.); public static void main(String args) new ThreadDemo(); Run:Current Thread : Threadmain,5,main 5 4 3Exiting main thread. 2 1Exiting child thread.150Thread Priority./ HiLoPri.javaclass Clicker implements Runnable int click = 0; private Thread t; private boolean running = true; public Clicker(int p) t = new Thread(this); t.setPriority(p); public void run() while(running) click+; public void start() t.start(); public void stop() running = false; 151.Thread Priorityclass HiLoPri public static void main(String args) Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Clicker Hi = new Clicker(Thread.NORM_PRIORITY+2); Clicker Lo = new Clicker(Thread.NORM_PRIORITY-2); Lo.start(); Hi.start(); try Thread.sleep(10000); catch (Exception e) Lo.stop(); Hi.stop(); System.out.println(Lo.click + vs. + Hi.click); Run1: (on Solaris)0 vs. 956228Run2: (Window 95)304300 vs. 4066666152The Java monitor modelMethod 1Method 2Block 1KeyThreadsMonitor (synchronised) solves race-condition problem153Threads Synchronisation./ Synch.java: race-condition without synchronisationclass Callme / Check synchronized and unsynchronized methods /* synchronized */ void call(String msg) System.out.print(+msg); try Thread.sleep(1000); catch(Exception e) System.out.println(); class Caller implements Runnable String msg; Callme Target; public Caller(Callme t, String s) Target = t; msg = s; new Thread(this).start(); 154.Threads Synchronisation. public void run() Target.call(msg); class Synch public static void main(String args) Callme Target = new Callme(); new Caller(Target, Hello); new Caller(Target, Synchronized); new Caller(Target, World); Run 1: With unsynchronized call method (race condition)HelloSynchronizedWorldRun 2: With synchronized call methodHelloSynchronizedWorldRun3: With Synchronized objectsynchronized(Target) Target.call(msg); The output is the same as Run2155Queue (no inter-threaded communication)./ pc.java: produce and consumerclass Queue int n; synchronized int get() System.out.println(Got : +n); return n; synchronized void put(int n) this.n = n; System.out.println(Put : +n); class Producer implements Runnable Queue Q; Producer(Queue q) Q = q; new Thread( this, Producer).start(); 156Queue (no inter-threaded communication).public void run() int i = 0; while(true) Q.put(i+); class Consumer implements Runnable Queue Q; Consumer(Queue q) Q = q; new Thread( this, Consumer).start(); public void run() while(true) Q.get(); 157.Queue (no inter-threaded communication).class PC public static void main(String args) Queue Q = new Queue(); new Producer(Q); new Consumer(Q); Run:Put: 1Got: 1Got: 1Got: 1Put: 2Put: 3Got: 3C158Queue (interthread communication)./ PCnew.java: produce-consumenr with interthread communicationclass Queue int n; boolean ValueSet = false; synchronized int get() try if(!ValueSet) wait(); catch(InterruptedException e) System.out.println(Got : +n); ValueSet = false; notify(); return n; 159Queue (interthread communication).synchronized void put(int n) try if(ValueSet) wait(); catch(InterruptedException e) this.n = n; System.out.println(Put : +n); ValueSet = true; notify(); class Producer implements Runnable Queue Q; Producer(Queue q) Q = q; new Thread( this, Producer).start(); 160Queue (interthread communication).public void run() int i = 0; while(true) Q.put(i+); class Consumer implements Runnable Queue Q; Consumer(Queue q) Q = q; new Thread( this, Consumer).start(); public void run() while(true) Q.get(); 161.Queue (no interthread communication).class PCnew public static void main(String args) Queue Q = new Queue(); new Producer(Q); new Consumer(Q); Run:Put : 0Got : 0Put : 1Got : 1Put : 2Got : 2Put : 3Got : 3Put : 4Got : 4C162Deadlock./ DeadLock.javaclass A synchronized void foo(B b) String name = Thread.currentThread().getName(); System.out.println(name + entered A.foo); try Thread.sleep(1000); catch(Exception e) System.out.println(name + trying to call B.last(); b.last(); synchronized void last() System.out.println(Inside A.last); 163Deadlock.class B synchronized void bar(A a) String name = Thread.currentThread().getName(); System.out.println(name + entered B.bar); try Thread.sleep(1000); catch(Exception e) System.out.println(name + trying to call A.last(); a.last(); synchronized void last() System.out.println(Inside B.last); 164.Deadlock.class DeadLock implements Runnable A a = new A(); B b = new B(); DeadLock() Thread.currentThread().setName(Main Thread); new Thread(this).start(); a.foo(b); System.out.println(Back in the main thread.); public void run() Thread.currentThread().setName(Racing Thread); b.bar(a); System.out.println(Back in the other thread); public static void main(String args) new DeadLock(); Run:Main Thread entered A.fooRacing Thread entered B.barMain Thread trying to call B.last()Racing Thread trying to call A.last()C165Grand Challenges Grand Challenges (Is PP Practical?)(Is PP Practical?),Need Need OS OS and and Compiler Compiler support support to to use use multiprocessor multiprocessor machines.machines.,Ideal Ideal would would be be for for the the user user to to be be unawareunaware if if the the problem problem is is running running on on sequential sequential or or parallel parallel hardware hardware - - a a long long way way to to go.go.,WithWith HighspeedHighspeed NetworksNetworks and and improved improved microprocessor microprocessor performance,performance, multiple multiple stand-alone stand-alone machines machines can can also also be be used used as as a a parallel parallel machine machine - - a a Popular Popular Trend. Trend. (appealing (appealing vehicle for parallel computing)vehicle for parallel computing),Language standards have to evolve. (Portability).Language standards have to evolve. (Portability).,Re-orientation of thinkingRe-orientation of thinkingccSequential Sequential PParallelarallel166Grand Challenges Grand Challenges (Is PP Practical?)(Is PP Practical?),Language standards have to evolve. Language standards have to evolve. (Portability).(Portability).,Re-orientation of thinkingRe-orientation of thinkingccSequential Sequential PParallelarallel167Breaking High Performance Computing BarriersBreaking High Performance Computing Barriers210021002100210021002100210021002100SingleProcessorSharedMemoryLocalParallelClusterGlobalParallelClusterGFLOPS168Thank You .Thank You .169精品资料,祝您成功精品资料,祝您成功
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号