⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tutor.me

📁 早期freebsd实现
💻 ME
📖 第 1 页 / 共 3 页
字号:
.)z.ppForming a connection is asymmetrical; one process, such as theprogram in Figure 7a, requests a connection with a particular socket,the other process accepts connection requests.Before a connection can be accepted a socket must be created and an addressbound to it.  Thissituation is illustrated in the top half of Figure 8.  Process 2has created a socket and bound a port number to it.  Process 1 has created anunnamed socket.The address bound to process 2's socket is then made known to process 1 and, perhaps to several other potential communicants as well.If there are several possible communicants,this one socket might receive several requests for connections.As a result, a new socket is created for each connection.  This new socketis the endpoint for communication within this process for this connection.A connection may be destroyed by closing the corresponding socket..ppThe program in Figure 7b is a rather trivial example of a server.  It creates a socket to which it binds a name, which it then advertises.(In this case it prints out the socket number.)  The program then calls\fIlisten()\fP for this socket.  Since several clients may attempt to connect more or lesssimultaneously, a queue of pending connections is maintained in the systemaddress space.  \fIListen()\fPmarks the socket as willing to accept connections and initializes the queue.When a connection is requested, it is listed in the queue.  If thequeue is full, an error status may be returned to the requester.The maximum length of this queue is specified by the second argument of\fIlisten()\fP; the maximum length is limited by the system.  Once the listen call has been completed, the program entersan infinite loop.  On each pass through the loop, a new connection isaccepted and removed from the queue, and, hence, a new socket for the connection is created.  The bottom half of Figure 8 shows the result ofProcess 1 connecting with the named socket of Process 2, and Process 2accepting the connection.  After the connection is created, theservice, in this case printing out the messages, is performed and theconnection socket closed.  The \fIaccept()\fPcall will take a pending connectionrequest from the queue if one is available, or block waiting for a request.Messages are read from the connection socket.Reads from an active connection will normally block until data is available.The number of bytes read is returned.  When a connection is destroyed,the read call returns immediately.  The number of bytes returned willbe zero..ppThe program in Figure 7c is a slight variation on the server in Figure 7b.It avoids blocking when there are no pending connection requests by calling \fIselect()\fPto check for pending requests before calling \fIaccept().\fPThis strategy is useful when connections may be receivedon more than one socket, or when data may arrive on other connectedsockets before another connection request..ppThe programs in Figures 9a and 9b show a program using stream communicationin the UNIX domain.  Streams in the UNIX domain can be used for this sortof program in exactly the same way as Internet domain streams, except forthe form of the names and the restriction of the connections to a singlefile system.  There are some differences, however, in the functionality of streams in the two domains, notably in the handling of \fIout-of-band\fP data (discussed briefly below).  These differencesare beyond the scope of this paper..(z.ft CW.so ustreamwrite.c.ft.ce 1Figure 9a\ \ Initiating a UNIX domain stream connection.sp 2.ft CW.so ustreamread.c.ft.ce 1Figure 9b\ \ Accepting a UNIX domain stream connection.)z.b.sh 1 "Reads, Writes, Recvs, etc.".r.ppUNIX 4.4BSD has several system calls for reading and writing information.The simplest calls are \fIread() \fP and \fIwrite().\fP \fIWrite()\fPtakes as arguments the index of a descriptor, a pointer to a buffer containing the data and the size of the data.The descriptor may indicate either a file or a connected socket.  ``Connected'' can mean either a connected stream socket (as describedin Section 8) or a datagram socket for which a \fIconnect()\fPcall has provided a default destination (see the \fIconnect()\fP manual page).\fIRead()\fP also takes a descriptor that indicates either a file or a socket.\fIWrite()\fP requires a connected socket since no destination is specified in the parameters of the system call.\fIRead()\fP can be used for either a connected or an unconnected socket.These calls are, therefore, quite flexible and may be used towrite applications that require no assumptions about the source oftheir input or the destination of their output.There are variations on \fIread() \fP and \fIwrite()\fPthat allow the source and destination of the input and output to useseveral separate buffers, while retaining the flexibility to handleboth files and sockets.  These are \fIreadv()\fP and \fI writev(),\fPfor read and write \fIvector.\fP.pp It is sometimes necessary to send high priority data over aconnection that may have unread low priority data at theother end.  For example, a user interface process may be interpretingcommands and sending them on to another process through a stream connection.The user interface may have filled the stream with as yet unprocessed requests when the user typesa command to cancel all outstanding requests.Rather than have the high priority data waitto be processed after the low priority data, it is possible tosend it as \fIout-of-band\fP(OOB) data.  The notification of pending OOB data results in the generation ofa SIGURG signal, if this signal has been enabled (see the manualpage for \fIsignal\fP or \fIsigvec\fP).See [Leffler 1986] for a more complete description of the OOB mechanism.There are a pair of calls similar to \fIread\fP and \fIwrite\fPthat allow options, including sending and receiving OOB information; these are \fI send()\fPand \fIrecv().\fPThese calls are used only with sockets; specifying a descriptor for a file willresult in the return of an error status.  These calls also allow\fIpeeking\fP at data in a stream.That is, they allow a process to read data without removing the data fromthe stream.  One use of this facility is to read ahead in a streamto determine the size of the next item to be read.When not using these options, these calls have the same functions as \fIread()\fP and \fIwrite().\fP.ppTo send datagrams, one must be allowed to specify the destination.The call \fIsendto()\fPtakes a destination address as an argument and is therefore used forsending datagrams.  The call \fIrecvfrom()\fPis often used to read datagrams, since this call returns the addressof the sender, if it is available, along with the data.If the identity of the sender does not matter, one may use \fIread()\fPor \fIrecv().\fP.ppFinally, there are a pair of calls that allow the sending andreceiving of messages from multiple buffers, when the address of therecipient must be specified.  These are \fIsendmsg()\fP and \fIrecvmsg().\fPThese calls are actually quite general and have other uses,including, in the UNIX domain, the transmission of a file descriptor from oneprocess to another..ppThe various options for reading and writing are shown in Figure 10,together with their parameters.  The parameters for each system callreflect the differences in function of the different calls.In the examples given in this paper, the calls \fIread()\fP and \fIwrite()\fP have been used whenever possible..(z.ft CW	/*	 * The variable descriptor may be the descriptor of either a file	 * or of a socket.	 */	cc = read(descriptor, buf, nbytes)	int cc, descriptor; char *buf; int nbytes;	/*	 * An iovec can include several source buffers.	 */	cc = readv(descriptor, iov, iovcnt)	int cc, descriptor; struct iovec *iov; int iovcnt;	cc = write(descriptor, buf, nbytes)	int cc, descriptor; char *buf; int nbytes;	cc = writev(descriptor, iovec, ioveclen)	int cc, descriptor; struct iovec *iovec; int ioveclen;	/*	 * The variable ``sock'' must be the descriptor of a socket.	 * Flags may include MSG_OOB and MSG_PEEK.	 */	cc = send(sock, msg, len, flags)	int cc, sock; char *msg; int len, flags; 	cc = sendto(sock, msg, len, flags, to, tolen)	int cc, sock; char *msg; int len, flags;	struct sockaddr *to; int tolen;	cc = sendmsg(sock, msg, flags)	int cc, sock; struct msghdr msg[]; int flags;	cc = recv(sock, buf, len, flags)	int cc, sock; char *buf; int len, flags;	cc = recvfrom(sock, buf, len, flags, from, fromlen)	int cc, sock; char *buf; int len, flags;	struct sockaddr *from; int *fromlen;	cc = recvmsg(sock, msg, flags)	int cc, socket; struct msghdr msg[]; int flags;.ft.sp 1.ce 1Figure 10\ \ Varieties of read and write commands.)z.b.sh 1 "Choices".r.ppThis paper has presented examples of some of the formsof communication supported byBerkeley UNIX 4.4BSD.  These have been presented in an order chosen forease of presentation.  It is useful to review these options emphasizing thefactors that make each attractive..ppPipes have the advantage of portability, in that they are supported in allUNIX systems.  They also are relativelysimple to use.  Socketpairs share this simplicity and have the additionaladvantage of allowing bidirectional communication.  The major shortcomingof these mechanisms is that they require communicating processes to bedescendants of a common process.  They do not allow intermachine communication..ppThe two communication domains, UNIX and Internet, allow processes with no commonancestor to communicate.Of the two, only the Internet domain allowscommunication between machines.This makes the Internet domain a necessarychoice for processes running on separate machines..ppThe choice between datagrams and stream communication is best made bycarefully considering the semantic and performancerequirements of the application.Streams can be both advantageous and disadvantageous.  One disadvantageis that a process is only allowed a limited number of open streams,as there are usually only 64 entries available in the open descriptortable.  This can cause problems if a single server must talk with a largenumber of clients. Another is that for delivering a short message the stream setup and teardown time can be unnecessarily long.  Weighed against this arethe reliability built into the streams.  This will often be thedeciding factor in favor of streams..b.sh 1 "What to do Next".r.ppMany of the examples presented here can serve as models for multiprocessprograms and for programs distributed across several machines.  In developing a new multiprocess program, it is often easiest to first write the code to create the processes and communication paths.After this code is debugged, the code specific to the application canbe added..ppAn introduction to the UNIX system and programming using UNIX system callscan be found in [Kernighan and Pike 1984].Further documentation of the Berkeley UNIX 4.4BSD IPC mechanisms can be found in [Leffler et al. 1986].More detailed information about particular calls and protocolsis provided in sections2, 3 and 4 of theUNIX Programmer's Manual [CSRG 1986].In particular the following manual pages are relevant:.(b.TSl l.creating and naming sockets	socket(2), bind(2)establishing connections	listen(2), accept(2), connect(2)transferring data	read(2), write(2), send(2), recv(2)addresses	inet(4F)protocols	tcp(4P), udp(4P)..TE.)b.(b.sp.bAcknowledgements.ppI would like to thank Sam Leffler and Mike Karels for their help inunderstanding the IPC mechanisms and all the people whose comments have helped in writing and improving this report..ppThis work was sponsored by the Defense Advanced Research Projects Agency(DoD), ARPA Order No. 4031, monitored by the Naval Electronics SystemsCommand under contract No. N00039-C-0235. The views and conclusions contained in this document are those of theauthor and should not be interpreted as representing official policies,either expressed or implied, of the Defense Research Projects Agencyor of the US Government..)b.(b.sp.bReferences.r.sp.ls 1B.W. Kernighan & R. Pike, 1984,.i "The UNIX Programming Environment."Englewood Cliffs, N.J.: Prentice-Hall..sp.ls 1B.W. Kernighan & D.M. Ritchie, 1978,.i "The C Programming Language,"Englewood Cliffs, N.J.: Prentice-Hall..sp.ls 1S.J. Leffler, R.S. Fabry, W.N. Joy, P. Lapsley, S. Miller & C. Torek, 1986,.i "An Advanced 4.4BSD Interprocess Communication Tutorial."Computer Systems Research Group,Department of Electrical Engineering and Computer Science,University of California, Berkeley..sp.ls 1Computer Systems Research Group, 1986,.i "UNIX Programmer's Manual, 4.4 Berkeley Software Distribution."Computer Systems Research Group,Department of Electrical Engineering and Computer Science,University of California, Berkeley..)b

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -