📄 5.t
字号:
.\" Copyright (c) 1986, 1993.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)5.t 8.1 (Berkeley) 8/14/93.\".\".ds RH "Advanced Topics.bp.nr H1 5.nr H2 0.LG.B.ce5. ADVANCED TOPICS.sp 2.R.NL.PPA number of facilities have yet to be discussed. For most usersof the IPC the mechanisms alreadydescribed will suffice in constructing distributedapplications. However, others will find the need to utilize someof the features which we consider in this section..NH 2Out of band data.PPThe stream socket abstraction includes the notion of \*(lqoutof band\*(rq data. Out of band data is a logically independent transmission channel associated with each pair of connectedstream sockets. Out of band data is delivered to the userindependently of normal data.The abstraction defines that the out of band data facilitiesmust support the reliable delivery of at least oneout of band message at a time. This message may contain at least onebyte of data, and at least one message may be pending deliveryto the user at any one time. For communications protocols whichsupport only in-band signaling (i.e. the urgent data isdelivered in sequence with the normal data), the system normally extractsthe data from the normal data stream and stores it separately.This allows users to choose between receiving the urgent datain order and receiving it out of sequence without having tobuffer all the intervening data. It is possibleto ``peek'' (via MSG_PEEK) at out of band data.If the socket has a process group, a SIGURG signal is generatedwhen the protocol is notified of its existence.A process can set the process groupor process id to be informed by the SIGURG signal via theappropriate \fIfcntl\fP call, as described below forSIGIO.If multiple sockets may have out of band data awaitingdelivery, a \fIselect\fP call for exceptional conditionsmay be used to determine those sockets with such data pending.Neither the signal nor the select indicate the actual arrivalof the out-of-band data, but only notification that it is pending..PPIn addition to the information passed, a logical mark is placed inthe data stream to indicate the point at which the outof band data was sent. The remote login and remote shellapplications use this facility to propagate signals betweenclient and server processes. When a signalflushs any pending output from the remote process(es), alldata up to the mark in the data stream is discarded..PPTo send an out of band message the MSG_OOB flag is supplied toa \fIsend\fP or \fIsendto\fP calls,while to receive out of band data MSG_OOB should be indicatedwhen performing a \fIrecvfrom\fP or \fIrecv\fP call.To find out if the read pointer is currently pointing atthe mark in the data stream, the SIOCATMARK ioctl is provided:.DSioctl(s, SIOCATMARK, &yes);.DEIf \fIyes\fP is a 1 on return, the next read will return dataafter the mark. Otherwise (assuming out of band data has arrived), the next read will provide data sent by the client priorto transmission of the out of band signal. The routine usedin the remote login process to flush output on receipt of aninterrupt or quit signal is shown in Figure 5.It reads the normal data up to the mark (to discard it),then reads the out-of-band byte..KF.DS#include <sys/ioctl.h>#include <sys/file.h> ...oob(){ int out = FWRITE, mark; char waste[BUFSIZ]; /* flush local terminal output */ ioctl(1, TIOCFLUSH, (char *)&out); for (;;) { if (ioctl(rem, SIOCATMARK, &mark) < 0) { perror("ioctl"); break; } if (mark) break; (void) read(rem, waste, sizeof (waste)); } if (recv(rem, &mark, 1, MSG_OOB) < 0) { perror("recv"); ... } ...}.DE.ceFigure 5. Flushing terminal I/O on receipt of out of band data..sp.KE.PPA process may also read or peek at the out-of-band datawithout first reading up to the mark.This is more difficult when the underlying protocol deliversthe urgent data in-band with the normal data, and only sendsnotification of its presence ahead of time (e.g., the TCP protocolused to implement streams in the Internet domain).With such protocols, the out-of-band byte may not yet have arrivedwhen a \fIrecv\fP is done with the MSG_OOB flag.In that case, the call will return an error of EWOULDBLOCK.Worse, there may be enough in-band data in the input bufferthat normal flow control prevents the peer from sending the urgent datauntil the buffer is cleared.The process must then read enough of the queued datathat the urgent data may be delivered..PPCertain programs that use multiple bytes of urgent data and musthandle multiple urgent signals (e.g., \fItelnet\fP\|(1C))need to retain the position of urgent data within the stream.This treatment is available as a socket-level option, SO_OOBINLINE;see \fIsetsockopt\fP\|(2) for usage.With this option, the position of urgent data (the \*(lqmark\*(rq)is retained, but the urgent data immediately follows the markwithin the normal data stream returned without the MSG_OOB flag.Reception of multiple urgent indications causes the mark to move,but no out-of-band data are lost..NH 2Non-Blocking Sockets.PPIt is occasionally convenient to make use of socketswhich do not block; that is, I/O requests whichcannot complete immediately andwould therefore cause the process to be suspended awaiting completion arenot executed, and an error code is returned.Once a socket has been created viathe \fIsocket\fP call, it may be marked as non-blockingby \fIfcntl\fP as follows:.DS#include <fcntl.h> ...int s; ...s = socket(AF_INET, SOCK_STREAM, 0); ...if (fcntl(s, F_SETFL, FNDELAY) < 0) perror("fcntl F_SETFL, FNDELAY"); exit(1);} ....DE.PPWhen performing non-blocking I/O on sockets, one must becareful to check for the error EWOULDBLOCK (stored in theglobal variable \fIerrno\fP), which occurs whenan operation would normally block, but the socket itwas performed on is marked as non-blocking.In particular, \fIaccept\fP, \fIconnect\fP, \fIsend\fP, \fIrecv\fP,\fIread\fP, and \fIwrite\fP canall return EWOULDBLOCK, and processes should be preparedto deal with such return codes.If an operation such as a \fIsend\fP cannot be done in its entirety,but partial writes are sensible (for example, when using a stream socket),the data that can be sent immediately will be processed,and the return value will indicate the amount actually sent..NH 2Interrupt driven socket I/O.PPThe SIGIO signal allows a process to be notifiedvia a signal when a socket (or more generally, a filedescriptor) has data waiting to be read. Use ofthe SIGIO facility requires three steps: First,the process must set up a SIGIO signal handlerby use of the \fIsignal\fP or \fIsigvec\fP calls. Second,it must set the process id or process group id which is to receivenotification of pending input to its own process id,or the process group id of its process group (note thatthe default process group of a socket is group zero).This is accomplished by use of an \fIfcntl\fP call.Third, it must enable asynchronous notification of pending I/O requestswith another \fIfcntl\fP call. Sample code toallow a given process to receive information onpending I/O requests as they occur for a socket \fIs\fPis given in Figure 6. With the addition of a handler for SIGURG,this code can also be used to prepare for receipt of SIGURG signals..KF.DS#include <fcntl.h> ...int io_handler(); ...signal(SIGIO, io_handler);/* Set the process receiving SIGIO/SIGURG signals to us */if (fcntl(s, F_SETOWN, getpid()) < 0) { perror("fcntl F_SETOWN"); exit(1);}/* Allow receipt of asynchronous I/O signals */if (fcntl(s, F_SETFL, FASYNC) < 0) { perror("fcntl F_SETFL, FASYNC"); exit(1);}.DE.ceFigure 6. Use of asynchronous notification of I/O requests..sp.KE.NH 2Signals and process groups.PPDue to the existence of the SIGURG and SIGIO signals each socket has anassociated process number, just as is done for terminals.This value is initialized to zero,but may be redefined at a later time with the F_SETOWN\fIfcntl\fP, such as was done in the code above for SIGIO.To set the socket's process id for signals, positive argumentsshould be given to the \fIfcntl\fP call. To set the socket'sprocess group for signals, negative arguments should be passed to \fIfcntl\fP. Note that the process number indicateseither the associated process id or the associated processgroup; it is impossible to specify both at the same time.A similar \fIfcntl\fP, F_GETOWN, is available for determining thecurrent process number of a socket..PPAnother signal which is useful when constructing server processesis SIGCHLD. This signal is delivered to a process when anychild processes have changed state. Normally servers usethe signal to \*(lqreap\*(rq child processes that have exitedwithout explicitly awaiting their terminationor periodic polling for exit status.For example, the remote login server loop shown in Figure 2may be augmented as shown in Figure 7..KF.DSint reaper(); ...signal(SIGCHLD, reaper);listen(f, 5);for (;;) { int g, len = sizeof (from); g = accept(f, (struct sockaddr *)&from, &len,); if (g < 0) { if (errno != EINTR) syslog(LOG_ERR, "rlogind: accept: %m"); continue; } ...} ...#include <wait.h>reaper(){ union wait status; while (wait3(&status, WNOHANG, 0) > 0) ;}.DE.sp.ceFigure 7. Use of the SIGCHLD signal..sp.KE.PPIf the parent server process fails to reap its children,a large number of \*(lqzombie\*(rq processes may be created..NH 2Pseudo terminals.PPMany programs will not function properly without a terminalfor standard input and output. Since sockets do not providethe semantics of terminals,it is often necessary to have a process communicating overthe network do so through a \fIpseudo-terminal\fP. A pseudo-terminal is actually a pair of devices, master and slave,which allow a process to serve as an active agent in communicationbetween processes and users. Data written on the slave sideof a pseudo-terminal is supplied as input to a process readingfrom the master side, while data written on the master side areprocessed as terminal input for the slave.In this way, the process manipulatingthe master side of the pseudo-terminal has control over theinformation read and written on the slave sideas if it were manipulating the keyboard and reading the screenon a real terminal.The purpose of this abstraction is topreserve terminal semantics over a network connection\(emthat is, the slave side appears as a normal terminal toany process reading from or writing to it..PPFor example, the remote
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -