BSD进程间通信编程入门,主要讲socket编程.pdf
《BSD进程间通信编程入门,主要讲socket编程.pdf》由会员分享,可在线阅读,更多相关《BSD进程间通信编程入门,主要讲socket编程.pdf(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Introductory 4.4BSD IPCPSD:20-1An Introductory 4.4BSDInterprocess Communication TutorialStuart SechrestComputer Science Research GroupComputer Science DivisionDepartment of Electrical Engineering and Computer ScienceUniversity of California,BerkeleyABSTRACTBerkeley UNIX 4.4BSD offers several choices
2、 for interprocess communication.To aid the pro-grammer in developing programs which are comprised of cooperating processes,the different choices arediscussed and a series of example programs are presented.These programs demonstrate in a simple waythe use of pipes,socketpairs,sockets and the use of d
3、atagram and stream communication.The intent of thisdocument is to present a few simple example programs,not to describe the networking system in full.1.GoalsFacilities for interprocess communication(IPC)and networking were a major addition to UNIX inthe Berkeley UNIX 4.2BSD release.These facilities
4、required major additions and some changes to the sys-tem interface.The basic idea of this interface is to make IPC similar to file I/O.In UNIX a process has aset of I/O descriptors,from which one reads and to which one writes.Descriptors may refer to normal files,to devices(including terminals),or t
5、o communication channels.The use of a descriptor has three phases:its creation,its use for reading and writing,and its destruction.By using descriptors to write files,ratherthan simply naming the target file in the write call,one gains a surprising amount of flexibility.Often,theprogram that creates
6、 a descriptor will be different from the program that uses the descriptor.For examplethe shell can create a descriptor for the output of the ls command that will cause the listing to appear in afile rather than on a terminal.Pipes are another form of descriptor that have been used in UNIX for someti
7、me.Pipes allow one-way data transmission from one process to another;the two processes and the pipemust be set up by a common ancestor.The use of descriptors is not the only communication interface provided by UNIX.The signal mech-anism sends a tiny amount of information from one process to another.
8、The signaled process receives onlythe signal type,not the identity of the sender,and the number of possible signals is small.The signalsemantics limit the flexibility of the signaling mechanism as a means of interprocess communication.The identification of IPC with I/O is quite longstanding in UNIX
9、and has proved quite successful.Atfirst,however,IPC was limited to processes communicating within a single machine.With Berkeley UNIX4.2BSD this expanded to include IPC between machines.This expansion has necessitated some change inthe way that descriptors are created.Additionally,new possibilities
10、for the meaning of read and write havebeen admitted.Originally the meanings,or semantics,of these terms were fairly simple.When you wrotesomething it was delivered.When you read something,you were blocked until the data arrived.Other pos-sibilities exist,however.One can write without full assurance
11、of delivery if one can check later to catchoccasional failures.Messages can be kept as discrete units or merged into a stream.One can ask to read,UNIX is a trademark of AT&T Bell Laboratories.PSD:20-2 Introductory 4.4BSD IPCbut insist on not waiting if nothing is immediately available.These new poss
12、ibilities are allowed in theBerkeley UNIX IPC interface.Thus Berkeley UNIX 4.4BSD offers several choices for IPC.This paper presents simple examplesthat illustrate some of the choices.The reader is presumed to be familiar with the C programming languageKernighan&Ritchie 1978,but not necessarily with
13、 the system calls of the UNIX system or with pro-cesses and interprocess communication.The paper reviews the notion of a process and the types of com-munication that are supported by Berkeley UNIX 4.4BSD.A series of examples are presented that createprocesses that communicate with one another.The pr
14、ograms show different ways of establishing channelsof communication.Finally,the calls that actually transfer data are reviewed.To clearly present how com-munication can take place,the example programs have been cleared of anything that might be construed asuseful work.They can,therefore,serve as mod
15、els for the programmer trying to construct programs whichare comprised of cooperating processes.2.ProcessesA program is both a sequence of statements and a rough way of referring to the computation thatoccurs when the compiled statements are run.A process can be thought of as a single line of contro
16、l in aprogram.Most programs execute some statements,go through a few loops,branch in various directionsand then end.These are single process programs.Programs can also have a point where control splits intotwo independent lines,an action called forking.In UNIX these lines can never join again.A call
17、 to thesystem routine fork(),causes a process to split in this way.The result of this call is that two independentprocesses will be running,executing exactly the same code.Memory values will be the same for all valuesset before the fork,but,subsequently,each version will be able to change only the v
18、alue of its own copy ofeach variable.Initially,the only difference between the two will be the value returned by fork().The par-ent will receive a process id for the child,the child will receive a zero.Calls to fork(),therefore,typicallyprecede,or are included in,an if-statement.A process views the
19、rest of the system through a private table of descriptors.The descriptors can rep-resent open files or sockets(sockets are communication objects that will be discussed below).Descriptorsare referred to by their index numbers in the table.The first three descriptors are often known by specialnames,st
20、din,stdout and stderr.These are the standard input,output and error.When a process forks,itsdescriptor table is copied to the child.Thus,if the parents standard input is being taken from a terminal(devices are also treated as files in UNIX),the childs input will be taken from the same terminal.Whoev
21、erreads first will get the input.If,before forking,the parent changes its standard input so that it is readingfrom a new file,the child will take its input from the new file.It is also possible to take input from a socket,rather than from a file.3.PipesMost users of UNIX know that they can pipe the
22、output of a program prog1 to the input ofanother,prog2,by typing the command prog1|pro g2.This is called piping the output of one pro-gram to another because the mechanism used to transfer the output is called a pipe.When the user types acommand,the command is read by the shell,which decides how to
23、execute it.If the command is simple,for example,prog1,the shell forks a process,which executes the program,prog1,and then dies.Theshell waits for this termination and then prompts for the next command.If the command is a compoundcommand,prog1|pro g2,the shell creates two processes connected by a pip
24、e.One process runs the pro-gram,prog1,the other runs prog2.The pipe is an I/O mechanism with two ends,or sockets.Data that iswritten into one socket can be read from the other.Since a program specifies its input and output only by the descriptor table indices,which appear asvariables or constants,th
25、e input source and output destination can be changed without changing the text ofthe program.It is in this way that the shell is able to set up pipes.Before executing prog1,the process canclose whatever is at stdout and replace it with one end of a pipe.Similarly,the process that will executeprog2 c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BSD 进程 通信 编程 入门 主要 socket
限制150内