高級操作系統(tǒng)全chapter3進程間通信2016ados_第1頁
高級操作系統(tǒng)全chapter3進程間通信2016ados_第2頁
高級操作系統(tǒng)全chapter3進程間通信2016ados_第3頁
高級操作系統(tǒng)全chapter3進程間通信2016ados_第4頁
高級操作系統(tǒng)全chapter3進程間通信2016ados_第5頁
已閱讀5頁,還剩149頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

高級AdvancedOperatingDistributedSystemsConceptsanddesign朱青QingZhu,DepartmentofComputerScience,RenminChapterInterprocessCommunication進程間的通Teacher:QingZhu 朱青DepartmentofComputerScience,InformationSchool,RenminUniversity2DistributedSystems–ConceptsandDesign,GeorgeCouloris, Dollimore,andTimKindbergEdition3,4,?PearsonEducation2001,2005ThelectureisbasedonthisChapter 進程間的3.0進程及消息3.1進程間的3.2客戶/服務(wù)器計算模3.4應(yīng)用4進程及消息進程進程狀態(tài)消息進程通信經(jīng)典操作系統(tǒng)簡是計算機系統(tǒng)中的一個系統(tǒng)軟件,是一些程序模能以有效、合理的方式組織和管理計算機的軟硬使得用戶能夠靈活、方便、有效的使用計算機,6什么是操作系操作系統(tǒng)做什么#include#includeintmain(intargc,char{ oworld");return0;}操作系統(tǒng)提供一系列服務(wù)多任務(wù)設(shè)備控制用戶界硬件控安9OperatingExploitsthehardwareresourcesofoneormoreprocessorstoprovideasetofservicestosystemusersandI/OdevicesBasicTheprocessorControlstheactionsofExecutesdata-processing&logicoperationsI/OMovedatabetweenmemoryandexternaldevices(storage,hardware,terminals,…)

TheIsvolatile–contentsarelostwhenpoweredoffRandomThesystemProvidesamongprocessors,mainmemory,andI/Omodules進程的進程(Process)的定進程的進程(Process)的定進程控制塊(ProcessControl>進程控制塊(PCB)概念 系統(tǒng)利用PCB來控制和管理進程,所PCB是系統(tǒng)感知進程存在的唯一標進程與PCB是一一對應(yīng)程序與進程更 描述并發(fā),而程序不進程是由程序和數(shù)據(jù)+PCB一個程序可對應(yīng)多個進程,反之亦進程的基本狀態(tài)及 等待發(fā)就緒

某 已經(jīng)發(fā)

等待運行態(tài)進程占有CPU,并在CPU上運就緒態(tài) 等待態(tài)(Blocked):阻塞態(tài)、掛起態(tài)、凍結(jié)態(tài)、睡眠 五狀態(tài)進程模準備退出:父進程可中止子進消息傳進程交互的兩個基本要同步:互斥進程間需同步。同步指對進程執(zhí)行時序通信:合作進程間交換信消息傳進程通信的一種常用方適用范圍廣,可用于多核、SMP由原語對“send(發(fā)送)和receive(接收)”提receive(源信息實現(xiàn)形式有多 MessageTable5.5DesignCharacteristicsofMessageSystemsforInterprocessCommunicationand消息傳遞的同只有當一個進程發(fā)送消息之后,接收者才能調(diào)用send原語的兩種可能結(jié)發(fā)送者進程被阻塞,直到這個消息被接調(diào)用receive原語接收者進程接收消息時,消息已發(fā)出,接收三種組合方“阻塞send,阻塞receive”方式,即“會(如打用的組合(如服務(wù)器提供資源,收發(fā))要求任何一方等待。:信息可能會丟失(如貼/看?。┱`可能會導(dǎo)致進程重復(fù)傳遞消息,消耗系統(tǒng)資源。且必須使用應(yīng)答消息以消息丟失或發(fā)送者進程失效,會導(dǎo)send原語receive直接尋址發(fā)送者在發(fā)送時給出了接收者進程消息傳遞的間接尋址發(fā)送者將消息發(fā)送到共享的信箱中臨時保管,接收者從信箱中獲得這些消耦合方式:“一對一”、“多對一”、進程與信箱的關(guān)聯(lián):靜態(tài)方式(端口)和動態(tài)方式(使用連接原語connect和IndirectProcess單機系分布式系定長格變長格消息頭——消息類型、目標ID、消息體——消息目標目標優(yōu)先級原同send——阻塞、無阻receive——阻塞、無阻尋間靜動所有消息傳遞實現(xiàn)通信/格內(nèi)長度——排隊規(guī)優(yōu)先使用“無阻塞send,阻塞receive”組一組進程共 個信箱box,信箱被初始化為一 每個進程在進入臨界區(qū)前,首先嘗試接收消息,每次只有接收到消息的那個進程才可以進入臨界區(qū))消息函數(shù)似在進程間傳遞一個可使用臨界區(qū)的Buffer問題描述(生產(chǎn)者/消費者問題若干進程通過無限/有限的共享緩沖區(qū)交換數(shù)一組“生產(chǎn)者”進程不斷另一組“消費者共享緩沖區(qū)無限/共有N4生產(chǎn)semaphoren=0;*緩沖區(qū)中semaphores=1;/*互斥*/voidproducer(){while(true){}

voidconsumer(){while(true){}}voidmain(){ 有限緩沖區(qū)的生產(chǎn)者/消費生產(chǎn)者N/*programmutualexclusion*/constintn=/*進程數(shù)*/;voidP(inti*進程i*/messagemsg;while(true){

voidmain(){send(box,null);parbegin(P(1),…,P(n));}receive(box,msg);/*信箱空時被阻塞/*臨界區(qū)send(box,/*其余部分*/;}消息傳遞實現(xiàn)生產(chǎn)者-/*互斥程序constintcapacity=/*緩沖區(qū)容量*/;messagenull=/*空消息*/;voidproducer(){messagepmsg;while(true){receive(mayproduce,pmsg);send(sume,}}/*互斥程序constintcapacity=/*緩沖區(qū)容量*/;messagenull=/*空消息*/;voidcomsumer(){messagecmsg; send(mayproduce,null);}}消息傳遞實現(xiàn)生產(chǎn)者-voidmain({for(inti=1;i<capacity;i++)send(mayproduce,null);}/*初始時:mayconsume信箱為空,mayproduce信箱則被null信號填滿Chapter 進程間的3.0進程及其狀態(tài)轉(zhuǎn)3.1進程間的3.2客戶/服務(wù)器計算模3.4應(yīng)用RMI,RMI,RPC,ExternaldataExternaldataRequest-replyRequest-replyOperatingsystem,networkRMI(remotemethodinvocation),RPC(remoteprocedureInterprocessCommunication(進程間的通信Point-to-pointmessagepassingistypicallybasedontwomessagecommunicationoperations(subroutines).在點Send:Aprocesscopiesamessage(asequencebytes)intothe(ing)bufferofaremoteReceive:Aprocesscopiesamessagefromthe(bufferofitssystemintoitslocal接收:進程消息從系統(tǒng)的緩沖區(qū)到本地內(nèi)存Typically,ReceivemustbecalledbeforethecorrespondingSend.在發(fā)送之前,相應(yīng)的接收需被調(diào)用Unbufferedcommunication:IfareceivehasnotcalledbeforethemessagearrivesitisAlternatively,someingmessagesarekepttemporarilyinabuffer(mailbox)assumingthatasuitablereceivewillbecalled?soon“.Otherwisethereisatime-outorabufferFailuremodelCommunicationSynchronousCommunication:ThesendingandreceivingprocessessynchronizeeachotherateveryTheSendandtheReceiveoperationsareTheSendprocessisblockeduntilthecorrespondingReceiveisissued.發(fā)送進程阻塞直到發(fā)生了相應(yīng)的接收TheReceiveprocessisblockeduntilamessageAsynchronousCommunication異步Thesendingprocessisallowedtoproceedassoonasthemessagehasbeensenttotheoutgoing(local)TheSendoperationisnon-TheReceiveoperationmaybeeitherblockingInterprocessMessageDestination:(Internetaddress,localport)Reliability:reliablecommunicationintermsofvalidityandintegrity.Ordering:Someapplicationsrequirethatmessagesbedeliveredinsenderorder.ThedeliveryofmessagesoutofsenderorderinwhichisregardedasafailurebysuchFigureSocketsand

any

agreed

Internetaddress=

other

Internetaddress=SocketsareendpointsforcommunicationTheyoriginatefromUNIXbutarealsofoundin套接字源于UNIX,也在其他OSTheyareusedbyUDPandTCP它被用于UDPandEachsocketisassociatedwithaparticularInterprocesscommunicationconsistsoftransmittingamessagebetweenasocketinoneprocessandasocketinanotherprocess.AsocketmustbeboundtoalocalportandanInternetaddressofthecomputeronwhichitruns.套接字必須綁定到計算機的一個InternetAprocessmayusemultipleportnumbersforPortscannotbesharedbydifferentprocesses.不同進程 Exception:IPmulticastsUDP)IPUDPisunreliableanddoesnotsupportacknowledgementsUDP是不具有可靠性,不支持確認和重Ifamessagedoesnotarrive,afailure如果消息無法到達,故障發(fā)Mostenvironmentssupportamessagesizeof大多數(shù)環(huán)境支持的消息包大小為Largermessagesmustbe大消息必須被分Thereceivingprocessneedstospecifyanarrayforthe接收消息需要列入消息數(shù)Themessageistruncateditistoo太長的消息需要被截UDPusesnon-blockingsendsandblockingUDP數(shù)據(jù)報使用非阻塞send和阻塞Amessageis cedinaqueueforthesocketandcanbecollectedbyafuturereceive.消息被放置在socketTime-outscanbesetonThemessageisdiscardedifthenosocketisboundtothereceivingport如果套接字不被綁定到接收端消AreceivegetsanymessageaddressedtothisTCPisreliableandusesanacknowledgementSlidingwindowTheTCPstreamistransmittedasoneormoreIPpackets(implementationdependent).TCPtriestomatchthespeedoftheprocesseswritingtoorreadingfromastream:FlowAsendermaybeblockedDuetomessageidentifierstherecipientcandetectandrejectduplicatesandreordermessageifnecessary.由于消息標示接收者能探測和 ApairofcommunicatingprocessmustestablishconnectionbeforeusingaOncetheconnectionexistsnoInternetaddressesandportsarenecessarytowritetoorreadfromthe一旦連接建立,對于流的讀寫,Internet地址和端口可以不需TheprocessesmustagreeonthecontentoftheFailureModelsforUDPandUDPusesachecksumtoensurethatamessageisUDP使用校驗確保消息不再破Somemessagesmaygetlost(omission一些消息可能被丟失(冗長失效MessagesmayarriveoutofsenderTCPuseschecksumstodetectandrejectcorruptTCP使用校驗診斷 TCPusessequencenumberstodetectandduplicatepackets.TCP使用序列號診斷 重復(fù)的包TCPusestimeoutsandretransmissionstodealwithAfteralongtimewithoutacknowledgementTCPdeclaretheconnectiontobe長時間沒有被確認的TCP將宣布連接中Theprocessescannotdistinguishbetweennetworkfailureandfailureoftheprocessattheotherendoftheconnection.Inthiscaseaprocess lwhetheritsrecentmessageshavebeenFigureMiddlewareRMIandRPC

Figure sendsamessagetotheserverandgetsaimport.*;importpublic publicstaticvoid//argsgivemessagecontentsandserveraSocket=null;try{aSocket=new);byte[]maHost=FigureUDPserverrepeatedlyreceivesarequestandsendsitbacktoimport.*;publicclasspublicstaticvoidaSocket=null;aSocket=new6789);byte[]buffer=newrequest=newFigure makesconnectiontoserver,sendsrequestandreceivesimport.*;publicclass publicstaticvoidmain(Stringargs[]){//argumentssupplymessageandhostnameofSockets=null;intserverPorts=newin=new(}.getInputStream(Figure4.6TCPservermakesaconnectionforeachandechoesthe’simportimportpublicclassTCPServerpublicstaticvoidmain(Stringargs[]){try{intserverPort=7896;ServerSocketlistenSocket=newServerSocket(serverPort);while(true){ Socket=listenSocket.accept();Connectionc=new }}catch(IOExceptione){System.out.println("Listen}}//thisfigurecontinuesonthenextFigure4.6classConnectionextendsThreadDataInputStreamin;SocketSocket;publicConnection(SocketaSocket)trySocket=ain=new out=newDataOutputStream( }catch(IOExceptione)}publicvoidtry //anechoStringdata=}catch(EOFExceptione)}catch(IOExceptione)}finally{try{Socket.close();}catch(IOExceptione){/*close}}FigureSocketsusedfordatagrams用于數(shù)據(jù)包的套Sendinga Receivingasendto(s,sendto(s,"message", ServerAddressandAddressaresocketFigureRequestinga Listeningandacceptingabind(s,write(s,"message",sNew=accept(s, write(s,"message",ServerAddress AddressaresocketChapter 進程間的3.0進程及其狀態(tài)轉(zhuǎn)3.1進程間的3.2客戶/服務(wù)器計算模3.4應(yīng)用客戶/服務(wù)器計算1網(wǎng)絡(luò)計算(1)以大型機為中心的計算模(分時共享模式(time- 從主機返回的所有結(jié)果,都顯示在屏幕網(wǎng)絡(luò)計算模(2)以服務(wù)(資源共享模式resource-形式:共享共同的應(yīng)用、數(shù)據(jù),以 盤、等。網(wǎng)絡(luò)計算模(3)小型化和客戶機/服務(wù)器計算end)服務(wù)器部客戶機和服務(wù)器工作在不同的邏輯實體中,協(xié)調(diào)工作通信時,客戶即可索取信息,它依賴于服務(wù)器執(zhí)行客不能完成或不能有效完成的工服務(wù)器方隨時等待客戶機提交申請的信息,進行信息技術(shù)特點和計算機能力來執(zhí)行一個特定C/S(1)客戶機的特點B)一個C/S模式下,可包括多個客戶C)客戶機使用標準語言進行信D)客戶機可使用緩沖或優(yōu)化E)客戶機通過一個進程間通訊機制C/S(2)C/S客戶/服務(wù)器的A)桌面上的B)優(yōu)化C)優(yōu)化網(wǎng) Chapter 進程間的3.0進程及其狀態(tài)轉(zhuǎn)3.1進程間的3.2客戶/服務(wù)器計算模3.4應(yīng)用實例Socket網(wǎng)絡(luò)概TCP/IP使用TCP Socket中的系統(tǒng)1.終端登錄與網(wǎng)絡(luò)登終端登錄:login: 網(wǎng)絡(luò)登錄 文件I/O和網(wǎng)絡(luò)文件六種輸入/輸出服務(wù)open,creat,close,read,write和網(wǎng)絡(luò)I/O:(比文件I/O復(fù)雜1)客戶/服務(wù)器關(guān)系是非對稱的,在初始化2)網(wǎng)絡(luò)I/O有面向連接與非連接的。前者很網(wǎng)絡(luò)3網(wǎng)絡(luò)中名字比標識更重要。文件繼承了fd4)UNIX的文件I/O是面向流的,而網(wǎng)絡(luò)有面向流與機制。如:XNS,IPX/SPX,TCP/IP。廣域網(wǎng)局域網(wǎng)集中模式(信息的處理和控制是集中的對等模式(信息的處理和控制都是分布的TCP/IP概網(wǎng)HostHostwithHostwith

網(wǎng)絡(luò)模

網(wǎng)絡(luò)應(yīng)用層:包含所有的協(xié)議,如虛擬終端協(xié) NET協(xié)議:允許用戶登錄到另一臺UNIX機器FTP協(xié)議:用于傳輸文SMTP協(xié)議:用來傳送,常見的服務(wù)器端程序netscape等公司制作的程序,也有免費使用的sendmail程;還有系統(tǒng)服務(wù)DNS協(xié)議,組傳送協(xié)議NNTP,用WWW的超文本傳輸協(xié)議HTTP等網(wǎng)絡(luò)傳輸層:主要功能使源和目的主機之間可以進行會話。該層(a)IP協(xié)議族(Intntotool)物理層TCP/IPTCP/IPTCP(傳輸控制協(xié)議)o議。不保證數(shù)據(jù)報一定能到達目的地。Socket描述與Socket(進程間通 )支持下列通信協(xié)socket索引值。socket描述符指向socket有BSDUNIX中socket調(diào)用,可以新建一個socket用,完成建立通信的過程 2socket客戶/服務(wù)器模TCPConnection

3-way

服務(wù)端進程(到服務(wù)器(1)使用socket調(diào)用得到一個描述(3)服務(wù)端使用listen調(diào)用等待服務(wù)請求服務(wù)端進程(5)服務(wù)端使用新的socket進行再到accept調(diào)用處等待另接??蛻舳诉M(1)使用socket調(diào)用得到一個socket描述(3)通過socket描述符進行讀寫操管套調(diào)用類型(無連接協(xié)議UDPConnection Server創(chuàng)建一個調(diào)用recvfrom接收客客戶端創(chuàng)建一個再使用sendto向服務(wù)端進程發(fā)出請求使用recvfrom得到返connect(建立連接write、read及send和recv和bind(將一個名字綁定到一個Socket調(diào)用socket用來建立一個通信的端點int ,inttype,int頭文件:#include#includeSocket的系統(tǒng) 參數(shù)type指通訊類型為SOCK_DGRAM的socket支持UDPConnect系統(tǒng)的格intconnect(ints,conststructsockaddr*name,int頭文件:#include#include第一個參數(shù)s是一個打開的socketConnect系統(tǒng)調(diào)如果該socket的類型是SOCK_STREAM,與另一socket建立連接 Connect系統(tǒng)調(diào)該結(jié)構(gòu)如structsockaddru_charsa_len;/*總長度u_charsa_family;/*地址簇charsa_data[14];/*實際,地址值第三個參數(shù) 地址的長度3write、read及close調(diào)在網(wǎng)絡(luò)程序中使用wi、ad和los.在使用TCwt來sok度一般將發(fā)送的數(shù)據(jù)到witread第一個參數(shù)也是socket描述第二個參數(shù)是存放讀入數(shù)據(jù)的用戶緩沖地址,第三個參數(shù)緩沖的長度也使用一個接收數(shù)據(jù)緩沖,當用戶進程使用read調(diào)用 用戶緩沖大小的數(shù)據(jù)。當客戶端或服務(wù)端使用UDP協(xié)議程序也可以使用read來接收UDP數(shù)據(jù)報,但當消息的長度于用戶緩沖時 僅為read調(diào) 用戶緩沖大小的數(shù),剩余數(shù)據(jù)將被Close該描述符有多個(與打開文件相同),則將該socket的數(shù)減一,如果數(shù)減為0,Send和send和sendto用來從一個socket發(fā)送一個消o格式intsend(ints,constvoid*msg,intlen,unsignedintflags);intsendto(ints,constvoid*msg,intlen,unsignedintflags,conststructsockaddr*to,頭文件:#include#include調(diào)用send和sendto用來發(fā)送一個消息到另一send調(diào)用只用于處于連接狀態(tài)的參數(shù)s是一個socket描述符。在sendto Recv和格式如intrecv(ints,void*buf,intlen,unsignedintintrecvfrom(ints,void*buf,intlen,unsignedintflags,structsockaddr*from,int*fromlen);頭文件:#include#include參數(shù)s為要接收數(shù)據(jù)的socket參數(shù)len用戶緩沖的大小系統(tǒng)調(diào)用recvfrom用來從一個socket接收消,不管這個socket是否面向連接參數(shù)from非空,并且該socket不是面向連接,則recvfrom調(diào)用返回時,參數(shù)from中存放是消息的源地址shutdown調(diào)用該調(diào)用將關(guān)閉一個全雙工連接的端格式如下:intshutdown(intsint頭文件:#include如果參數(shù)how為1,則繼續(xù)發(fā)送數(shù)據(jù)。如how為2, 在該socket上接收或發(fā)送數(shù)據(jù)Bind調(diào)bind調(diào)用將一個名字綁定到一個socket格式如下:intbind(intsstruct*my_addr,int頭文件:#include#includebind調(diào)用為一個未命名的socket分配即:bind調(diào)用是給套接字smy_addr,這個my_addr地址的長度為。對于不同通信域的地址聯(lián)編的規(guī)則是不,這里還是以internet域為主

listen調(diào)用該調(diào)用在一個socket偵聽連格式如下:intlisten(intsint頭文件:#include或SOCK_SEQPACKET類型的socket上。accept調(diào)用在指定的socket上接受接請求格式如下:intaccept(intsstructsockaddr*addr頭文件:#include#include參數(shù)s是要接受連接請求的socket,這個socketsocket調(diào)用創(chuàng)建第二個參數(shù)addr是用來存放返回值的,在使用IP地址,使用sockaddr_in結(jié)構(gòu),accept返回時,該結(jié)的是連接實體的第三個參數(shù)addrlen在調(diào)用前用來addr的大小,調(diào)用后返回精確的addr的長

DNS(服務(wù)通過因特網(wǎng)名字得到名字所代表的IP$IP四層UNIXIPSocketAPIintsocket(intfamily,inttype,intCreatesanetworkplugpointthatenablesthe/serverintconnect(intsockfd,conststructsockaddr*servaddr,socklen_taddrlen)Enablesatoconnecttoabind(intsockfd,conststructsockaddr*myaddr,socklen_tAllowsaservertospecifytheIPaddress/port_numberassociatedwithaSocketAPIintlisten(intsockfd,intAllowstheservertospecifythatasocketcanbeusedtointaccept(intsockfd,structsockaddr*_addr,socklen_tAllowsaservertowaittillanewconnectionrequestintclose(intTerminatesanyconnectionassociatedwithasocketandreleasestheSocketAPIintread(intsockfd,void*buf,intReaddatafromaTCPintwrite(intsockfd,void*buf,intWritedatatoaTCPintsendto(intsockfd,void*msg,…SendaUDPmessageonasockettospecifiedintrecvfrom(intsockfd,void*buf,…RecvaUDPmessageonasocketalongwithaddressofsendingSimilarlycheckmanpagesforsendmsg(),ConvertingBetweenAddressFromASCIIto“4”32-bitnetworkbyteorderedinet_aton(…)withinet_pton(…)withIPv4andFromnumericto32-bitvalueinet_ntoa(…)withinet_ntop(…)withIPv4andNote–inet_addr(…)cannothandlebroadcastaddressGettingIPaddress/portfromintgetsockname(intsockfd,structsockaddr*localaddr,socklen_t*addrlen)GetthelocalIP/portboundtointgetpeername(intsockfd,structsockaddr*remoteaddr,socklen_t*addrlen)GettheIP/portofremoteNameIPstructhostent*gethostbyaddr(void*addr,size_tlen,intConvertsfromIPaddr structhostent*gethostbyname(charConverts nametoIPChapter 進程間的3.0進程及其狀態(tài)轉(zhuǎn)3.1進程間的3.2客戶/服務(wù)器計算模3.4應(yīng)用實例應(yīng)用實 o,world!”.簡單的流服務(wù)(Ex37.c/*server#include#include#include#include#incl

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論