基于多线程的高性能服务器程序的设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1Designofhigh-performanceserverprogramwhichbasedonmultithreadsAbstract:Alongwiththenetworkapplication'srapiddevelopment,thehigh-performanceserverprogramhighlightsitsimportantstatusmoreandmore.Inthearticlehasdesignedahigh-performanceserverprogrambasedonmulti-threads,thustoputsforwardgoodsolutionstoprocessclientconcurrentrequestproblem.FirstlyintroducingtheprincipleofIOCPmodel,then,usingtheSelectmodelandtheIOCPmodelrespectively,putadifferentdesignmethodforthehigh-performanceserverprogramdesign,throughcomparingthetime,CPUutilizationandmemoryutilizationratioandmanyotherparameters,concludedthatIOCPprovidesthebestperformanceofthesystemindesignontheserver.Finallyputsforwarddesignschemeofhigh-performanceserverprogram.Keywords:Selectmodel,IOCPmodel,Concurrencycontrol,Event;flowcontrol1IntroductionAtpresenttherearemanyserverprogramsuse“onetoone”disposingmode,namelyoneclienttakesathread,designsonthearchitectureofserverprogramswhichbasedonthiskindofpatternisverysimple,butsinceusesis“aclient/thread”pattern,thereforethiskindofserverprogramappearsverylowintheperformanceregardingoverathousandclient's2requestconnections,andisalsocostlyonsystemresources.Becausetheoperatingsystemmustcarryonthedispatchforthemassivethreads,itwilllosethemassivesystemresources,andmutexcontrolwillalsobeverydifficult,thedeadlockoccurfrequencywillgreatlyincrease.Nowadaysthehighperformanceserverprogram'sdesignbecomesmoreandmoreimportant,thehighperformanceserverprogram'smainfunctionisthatitcanprocessmassiveconcurrentclient'srequestefficiently,andprocessclient'sdatatimely,simultaneouslygivesthepartoftheresponseinformationbacktotheclientside.Todesignagoodperformanceofserverprogram,mustconsidermanyfactors,likeCPUutilizationratio,Memoryexpensesandsoon.Tobeefficientprocessingconcurrentusersrequest,theserverprogramneedstosolvetwomainproblems:first,handlingtheconcurrentclient'sconnectrequests;second,forconcurrentdataprocessing.Tosolvetheseproblems,itcancreateanequalamountofworkthreadaccordingtothequantityofprocessorcore,andcombineIOCP(Input/OutputCompletionPort,IOCP)model,andtheoperatingsystemofthethreadschedulingmechanism.Inthearticleusingthemulti-threadsandunifiestheIOCPmodeltocarryonthethoroughdiscussiontothehighperformanceserverprogramdesign.32theoryofIOCPmodelIOCP(I/OCompletionPort,Input/OutputCompletionPort)iskindofmechanismthatcanrationallyutilizeandmanagemulti-threads.Byfar,ItisthemostcomplexI/OmodelintheWindowsPlatform.Whentheapplicationmustmanagemultiplesocketsatonetime,thecompletionportmodelprovidesthebestsystemperformanceandthebestretractility,andalsoitisverysuitableforprocessinghundreds,eventhousandsofcustomers.IOCPmodelprovidesanefficientandcomplexKernelObjectWhichcan,byspecifyingthenumberofthreads,overlapinthesocketsendandreceivedeliveryoftherequestprocessingI/O.Itscoreideacanbesimplysummarizedasfollows:Deliveryalltheusers’requeststoamessagequeue,thenuseanumberofpre-createdworkingthreadstoremovethemessagefromthemessagequeueanddealwiththemonebyone.ItcanbeintheserviceofanyI/Ooperationsofanyuser,andonlyasmallnumberofthreadscanhandlealargenumberofI/Orequests,toavoidtheCPUtospendtimeinalargenumberofthreadschedulingandimproveresourceutilization.3Thedesignoftheserverprogram43.1usingIOCPmodeltosettletheconcurrentusersproblem3.1.1concurrentprocessingofmulti-threadsItisknowntoallthatthelargeserversprogramsofhighlyconcurrenthasfacedmanyproblemslikecomplexschema,numerousthreadshardtomanage,concurrentperformancehardtopromote.Therefore,variousplatformshasprovidedsystem-level'sadvancedfacilitiestoassistdeveloperstosolvetheproblem,suchastheplatformofLinuxepoll.JustlikeWindowsplatform,thereisakernelobjectnamedIOCP(completionport),throughwhichwecaneasilycreatehigh-concurrency,highperformance,scalablenetworkserverprograms.Figure2givesthediagramthatusingIOCPmodeltosolvetheissuesthatuserssimultaneouslyrequestedconnectingserver.Figure2IOCPmodeldiagramAdheringtotheprinciplesoflowcoupling,whendesigntheserverprogramswehascreatedfourseparatethreadstodispatchprocessingtheconnectrequestofconcurrentclients.PuttingtousethemechanismofthosefourthreadstohandleuserconnectionrequestscanbeagoodIOCPServerprogramconcurrentusersConnectrequestnConnectrequestoneThread1Thread2Thread3Thread45solutiontodisposeofnumerousconcurrentusers’connectionrequests.3.1.2BufferlistsstrategytosolveconcurrentclientsproblemTofurtheraddressthisissue,wehaveadoptedbufferlistsstrategywhendesigningtheserverprograms.Theschematicdiagramofthisstrategyshowsbelow.Figure3diagramofbufferlistsOncetheclienthasestablished,weinserteditintothebufferlists,sothatotherthreadscanreceivedatasendbytheclient.Inthismode,thesefourthreadsarejusttoconnecttocustomers,ratherthantodealwithcustomerrequests,therebyincreasingthespeedofserverprocessingclientrequests.What’smore,thethreadsdon’thavetowaitforclientdata,whichcausedthemblockinginestablishingconnectionthread.3.1.3MutuallyexclusivecontrolOfcourse,inthosefourthreadsmaycausemutex,soweusecriticalsectiontosolvethemutexproblem.Throughthecriticalsectiontocontroltheoperationsonsharing

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功