资源描述:
《《并行程序设计导论》 第四章PPT.ppt》由会员上传分享,免费在线阅读,更多相关内容在PPT专区-天天文库。
1、Copyright©2010,ElsevierInc.AllrightsReservedChapter4SharedMemoryProgrammingwithPthreadsAnIntroductiontoParallelProgrammingPeterPachecoCopyright©2010,ElsevierInc.AllrightsReservedRoadmapProblemsprogrammingsharedmemorysystems.Controllingaccesstoacriticalsection
2、.Threadsynchronization.ProgrammingwithPOSIXthreads.Mutexes.Producer-consumersynchronizationandsemaphores.Barriersandconditionvariables.Read-writelocks.Threadsafety.#ChapterSubtitleASharedMemorySystemCopyright©2010,ElsevierInc.AllrightsReservedProcessesandThre
3、adsAprocessisaninstanceofarunning(orsuspended)program.Threadsareanalogoustoa“light-weight”process.Inasharedmemoryprogramasingleprocessmayhavemultiplethreadsofcontrol.Copyright©2010,ElsevierInc.AllrightsReservedPOSIX®ThreadsAlsoknownasPthreads.AstandardforUnix
4、-likeoperatingsystems.AlibrarythatcanbelinkedwithCprograms.Specifiesanapplicationprogramminginterface(API)formulti-threadedprogramming.Copyright©2010,ElsevierInc.AllrightsReservedCaveatThePthreadsAPIisonlyavailableonPOSIXRsystems—Linux,MacOSX,Solaris,HPUX,…Co
5、pyright©2010,ElsevierInc.AllrightsReservedHelloWorld!(1)Copyright©2010,ElsevierInc.AllrightsReserveddeclaresthevariousPthreadsfunctions,constants,types,etc.HelloWorld!(2)Copyright©2010,ElsevierInc.AllrightsReservedHelloWorld!(3)Copyright©2010,ElsevierInc.Allr
6、ightsReservedCompilingaPthreadprogramCopyright©2010,ElsevierInc.AllrightsReservedgcc−g−Wall−opth_hellopth_hello.c−lpthreadlinkinthePthreadslibraryRunningaPthreadsprogramCopyright©2010,ElsevierInc.AllrightsReserved./pth_hello./pth_hello1Hellof
7、romthemainthreadHellofromthread0of1./pth_hello4HellofromthemainthreadHellofromthread0of4Hellofromthread1of4Hellofromthread2of4Hellofromthread3of4GlobalvariablesCanintroducesubtleandconfusingbugs!Limituseofglobalvariablestosituationsinwhichthey’rereallyneeded.
8、Sharedvariables.Copyright©2010,ElsevierInc.AllrightsReservedStartingtheThreadsProcessesinMPIareusuallystartedbyascript.InPthreadsthethreadsarestartedbytheprogramexecutable.Copyright©2010,