Return-Path: owner-linux-doc@vger.rutgers.edu
Received: from kantti.helsinki.fi (root@kantti.Helsinki.FI [128.214.4.16]) by keos.Helsinki.FI (8.6.9/H46) with ESMTP id HAA04698 for <wirzeniu@cs.Helsinki.FI>; Mon, 20 Mar 1995 07:00:33 +0200
Received: from vger.rutgers.edu (vger.rutgers.edu [128.6.190.2]) by kantti.helsinki.fi (8.6.10+Emil1.1/8.6.5) with SMTP id HAA23844 for <wirzeniu@cc.helsinki.fi>; Mon, 20 Mar 1995 07:00:30 +0200
Received: (from daemon@localhost) by vger.rutgers.edu (8.6.10/8.6.10) id XAA03141 for linux-doc-outgoing; Sun, 19 Mar 1995 23:01:36 -0500
Message-Id: <m0rqXLI-000DUCC@yage.tembel.org>
Subject: Re: simple select() application?
To: ecarp@netcom.com
Date: Sun, 19 Mar 1995 21:37:15 -0500 (EST)
Cc: linux-kernel@vger.rutgers.edu, linux-doc@vger.rutgers.edu
In-Reply-To: <199503170042.RAA16891@khijol.intele.net> from "Ed Carp [khijol Sysadmin]" at Mar 16, 95 05:42:05 pm
X-Disclaimer: I speak for no one and the Illuminati speak for me.
X-Dogma: Microsoft is not the answer.
	 Microsoft is the question.
	 No is the answer.
X-PGP-Finger: mjshield@nyx.cs.du.edu
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Content-Length: 4472
From: shields@tembel.org (Michael Shields)
Sender: owner-linux-doc@vger.rutgers.edu
Precedence: bulk
Status: RO
X-Status: 

Ed Carp wrote on linux-kernel:
> I know this sounds like a stupid question, but could someone send me a SIMPLE
> sample application that uses select?  I don't know if it's just my brain or
> what, but the select() manpage makes absolutely no sense to me, and it's
> been a long time since I've written any code that relies on select.

Then the real flaw is in the manpage.  Here's a better version.

(I don't know how the copyright falls now; I completely replaced
DESCRIPTION and NOTES.  I'm willing to sign whatever standard waivers
there are.)

---- cut here -----
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
.\"
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one
.\" 
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\" 
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Modified Sat Jul 24 00:41:28 1993 by Rik Faith (faith@cs.unc.edu)
.\"
.\" Rewritten 1995-03-19 Michael Shields <shields@tembel.org>.
.\"
.TH SELECT 2 "1995-03-19" "Linux 1.2" "Linux Programmer's Manual"
.SH NAME
select \- synchronous I/O multiplexing 
.SH SYNOPSIS
.B #include <sys/time.h>
.br
.B #include <sys/types.h>
.br
.B #include <unistd.h>
.sp
.BI "int select(int " maxfd ", fd_set *" readfds ", fd_set *"  writefds
.BI ", fd_set *" exceptfds ", struct timeval *" timeout );
.sp
.BI "FD_CLR(int " fd ", fd_set *" set );
.br
.BI "FD_ISSET(int " fd ", fd_set *" set );
.br
.BI "FD_SET(int " fd ", fd_set *" set );
.br
.BI "FD_ZERO(fd_set * " set );
.SH DESCRIPTION
.B select
waits for a number of file descriptors to change status.
.PP
Three orthogonal sets of descriptors are watched.  Those listed in
.I readfds
will be watched to see if characters become
available for reading, those in
.I writefds
will be watched to see if it is ok to immediately write on them, and
those in
.I exceptfds
will be watched for exceptions.  On exit, the sets are modified in place
to indicate which descriptors actually changed status.
.PP
Four macros are provided to manipulate the sets.
.B FD_ZERO
will clear a set.
.B FD_SET
and
.B FD_CLR
add or remove a given descriptor from a set.
.B FD_ISSET
tests to see if a descriptor is part of the set; this is useful after
.B select
returns.
.PP
.I maxfd
is the highest-numbered descriptor in any of the three sets.
.PP
.I timeout
is an outer bound on the amount of time elapsed before
.B select
returns.  It may be
.BR NULL ,
which will cause
.B select
to sleep indefinitely until something happens.
.SH RETURN VALUE
On success,
.B select 
returns the number of descriptors contained in the descriptor sets.  On
error, \-1 is returned, and
.I errno
is set appropriately.
.SH ERRORS
.TP 0.8i
.B EBADF
An invalid file descriptor was given in one of the sets.
.TP
.B EINTR 
A non blocked signal was caught.
.TP
.B EINVAL 
.I maxfd
is negative.
.TP
.B ENOMEM
select was unable to allocate memory for internal tables.
.SH NOTES
Some code calls
.B select
with all three sets empty,
.B maxfd
zero, and a non-null timeout as a fairly portable way to sleep with
subsecond precision.
.PP
On Linux,
.I timeout
is modified to reflect the amount of time not slept; most other
implementations do not do this.  This causes problems both when Linux
code which reads
.I timeout
is ported to other operating systems, and when code is ported to Linux
that reuses a struct timeval for multiple
.BR select s
in a loop without reinitializing it.  Consider
.I timeout
to be undefined after
.B select
returns.
.SH SEE ALSO
.BR accept (2),
.BR connect (2),
.BR read (2),
.BR recv (2),
.BR send (2),
.BR write (2)
-- 
Shields.
