161183Sbostic.\" Copyright (c) 1983, 1991, 1993 261183Sbostic.\" The Regents of the University of California. All rights reserved. 320255Smckusick.\" 443568Strent.\" %sccs.include.redist.man% 520255Smckusick.\" 6*66449Sbostic.\" @(#)select.2 8.2 (Berkeley) 03/25/94 736759Sbostic.\" 847208Scael.Dd 947208Scael.Dt SELECT 2 1047208Scael.Os BSD 4.2 1147208Scael.Sh NAME 1247208Scael.Nm select 1347208Scael.Nd synchronous I/O multiplexing 1447208Scael.Sh SYNOPSIS 1547208Scael.Fd #include <sys/types.h> 1647208Scael.Fd #include <sys/time.h> 17*66449Sbostic.Fd #include <unistd.h> 1847208Scael.Ft int 1947208Scael.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout" 2047208Scael.Fn FD_SET fd &fdset 2147208Scael.Fn FD_CLR fd &fdset 2247208Scael.Fn FD_ISSET fd &fdset 2347208Scael.Fn FD_ZERO &fdset 2447208Scael.Sh DESCRIPTION 2547208Scael.Fn Select 2626071Skarelsexamines the I/O descriptor sets whose addresses are passed in 2747208Scael.Fa readfds , 2847208Scael.Fa writefds , 2920255Smckusickand 3047208Scael.Fa exceptfds 3126062Sdonnto see if some of their descriptors 3226062Sdonnare ready for reading, are ready for writing, or have an exceptional 3320255Smckusickcondition pending, respectively. 3426062SdonnThe first 3547208Scael.Fa nfds 3626062Sdonndescriptors are checked in each set; 3747208Scaeli.e., the descriptors from 0 through 3847208Scael.Fa nfds Ns No -1 3926062Sdonnin the descriptor sets are examined. 4026062SdonnOn return, 4147208Scael.Fn select 4226062Sdonnreplaces the given descriptor sets 4326071Skarelswith subsets consisting of those descriptors that are ready 4426071Skarelsfor the requested operation. 4547208Scael.Fn Select 4647208Scaelreturns the total number of ready descriptors in all the sets. 4747208Scael.Pp 4826071SkarelsThe descriptor sets are stored as bit fields in arrays of integers. 4926071SkarelsThe following macros are provided for manipulating such descriptor sets: 5047208Scael.Fn FD_ZERO &fdsetx 5126062Sdonninitializes a descriptor set 5247208Scael.Fa fdset 5326062Sdonnto the null set. 5447208Scael.Fn FD_SET fd &fdset 5526062Sdonnincludes a particular descriptor 5647208Scael.Fa fd 5726062Sdonnin 5847208Scael.Fa fdset . 5947208Scael.Fn FD_CLR fd &fdset 6026062Sdonnremoves 6147208Scael.Fa fd 6226062Sdonnfrom 6347208Scael.Fa fdset . 6447208Scael.Fn FD_ISSET fd &fdset 6547208Scaelis non-zero if 6647208Scael.Fa fd 6726062Sdonnis a member of 6847208Scael.Fa fdset , 6926062Sdonnzero otherwise. 7026062SdonnThe behavior of these macros is undefined if 7126062Sdonna descriptor value is less than zero or greater than or equal to 7247208Scael.Dv FD_SETSIZE , 7326062Sdonnwhich is normally at least equal 7426062Sdonnto the maximum number of descriptors supported by the system. 7547208Scael.Pp 7620255SmckusickIf 7747208Scael.Fa timeout 7847208Scaelis a non-nil pointer, it specifies a maximum interval to wait for the 7920255Smckusickselection to complete. If 8047208Scael.Fa timeout 8147208Scaelis a nil pointer, the select blocks indefinitely. To affect a poll, the 8247208Scael.Fa timeout 8347208Scaelargument should be non-nil, pointing to a zero-valued timeval structure. 8447208Scael.Pp 8520255SmckusickAny of 8647208Scael.Fa readfds , 8747208Scael.Fa writefds , 8820255Smckusickand 8947208Scael.Fa exceptfds 9047208Scaelmay be given as nil pointers if no descriptors are of interest. 9147208Scael.Sh RETURN VALUES 9247208Scael.Fn Select 9326062Sdonnreturns the number of ready descriptors that are contained in 9426062Sdonnthe descriptor sets, 9547208Scaelor -1 if an error occurred. 9647208ScaelIf the time limit expires, 9747208Scael.Fn select 9820255Smckusickreturns 0. 9926071SkarelsIf 10047208Scael.Fn select 10126071Skarelsreturns with an error, 10226071Skarelsincluding one due to an interrupted call, 10326071Skarelsthe descriptor sets will be unmodified. 10447208Scael.Sh ERRORS 10547208ScaelAn error return from 10647208Scael.Fn select 10747208Scaelindicates: 10847208Scael.Bl -tag -width Er 10947208Scael.It Bq Er EBADF 11026062SdonnOne of the descriptor sets specified an invalid descriptor. 11147208Scael.It Bq Er EINTR 11223789SsechrestA signal was delivered before the time limit expired and 11323789Ssechrestbefore any of the selected events occurred. 11447208Scael.It Bq Er EINVAL 11526062SdonnThe specified time limit is invalid. One of its components is 11623789Ssechrestnegative or too large. 11747208Scael.El 11847208Scael.Sh SEE ALSO 11947208Scael.Xr accept 2 , 12047208Scael.Xr connect 2 , 121*66449Sbostic.Xr getdtablesize 2 , 122*66449Sbostic.Xr gettimeofday 2 , 12347208Scael.Xr read 2 , 12447208Scael.Xr recv 2 , 12547208Scael.Xr send 2 , 126*66449Sbostic.Xr write 2 12747208Scael.Sh BUGS 12826071SkarelsAlthough the provision of 12947208Scael.Xr getdtablesize 2 13026071Skarelswas intended to allow user programs to be written independent 13126071Skarelsof the kernel limit on the number of open files, the dimension 13226071Skarelsof a sufficiently large bit field for select remains a problem. 13347208ScaelThe default size 13447208Scael.Dv FD_SETSIZE 13547208Scael(currently 256) is somewhat larger than 13626071Skarelsthe current kernel limit to the number of open files. 13728209SanneHowever, in order to accommodate programs which might potentially 13826071Skarelsuse a larger number of open files with select, it is possible 13926071Skarelsto increase this size within a program by providing 14047208Scaela larger definition of 14147208Scael.Dv FD_SETSIZE 14247208Scaelbefore the inclusion of 14347208Scael.Aq Pa sys/types.h . 14447208Scael.Pp 14547208Scael.Fn Select 14626071Skarelsshould probably return the time remaining from the original timeout, 14726071Skarelsif any, by modifying the time value in place. 14826071SkarelsThis may be implemented in future versions of the system. 14926071SkarelsThus, it is unwise to assume that the timeout value will be unmodified 15026071Skarelsby the 15147208Scael.Fn select 15226071Skarelscall. 15347208Scael.Sh HISTORY 15447208ScaelThe 15547208Scael.Nm 15647208Scaelfunction call appeared in 15747208Scael.Bx 4.2 . 158