fpSelect function
Wait for events on file descriptors.
Declaration
function FPSelect(N: cint; readfds: pFDSet; writefds: pFDSet;
exceptfds: pFDSet; TimeOut: ptimeval): cintDescription
FpSelect checks one of the file descriptors in the
FDSets to see if
the following I/O operation on the file descriptors will block.
readfds, writefds and
exceptfds are pointers to arrays of 256
bits. If you want a file descriptor to be checked, you set the
corresponding element in the array to 1. The other elements in the array
must be set to zero. Three arrays are passed : The entries in
readfds
are checked to see if the following read operation will block. The entries
in
writefds are checked to see if the following write operation
will block, while entries in
exceptfds are checked to see if an
exception occurred on them.
You can use the functions fpFD_ZERO, fpFD_Clr, fpFD_Set or fpFD_IsSet to manipulate the individual elements of a set.
The pointers can be Nil.
N is the value of the largest file descriptor in one of the sets, + 1. In other words, it is the position of the last bit which is set in the array of bits.
TimeOut can be used to set a time limit.
If
TimeOut can be two types :
-
TimeOutis of typeptimevaland contains a zero time, the call returns immediately. IfTimeOutisNil, the kernel will wait forever, or until a status changed. -
TimeOutis of typecint. If it is -1, this has the same effect as aTimeoutof typePTimewhich isNil. Otherwise,TimeOutcontains a time in milliseconds.
When the TimeOut is reached, or one of the file descriptors has changed,
the Select call returns. On return, it will have modified the entries
in the array which have actually changed, and it returns the number of
entries that have been changed. If the timeout was reached, and no descriptor
changed, zero is returned; The arrays of indexes are undefined after that.
On error, -1 is returned.
The variant with the text file will execute the FpSelect call on
the file descriptor associated with the text file T
Errors
On error, the function returns -1. Extended error information can be retrieved using fpGetErrno.
- SYS_EBADF
- An invalid descriptor was specified in one of the sets.
- SYS_EINTR
- A non blocked signal was caught.
- SYS_EINVAL
-
Nis negative or too big. - SYS_ENOMEM
-
Selectwas unable to allocate memory for its internal tables.
See also
Example: ex33
Program Example33;
{ Program to demonstrate the Select function. }
Uses BaseUnix;
Var FDS : Tfdset;
begin
fpfd_zero(FDS);
fpfd_set(0,FDS);
Writeln ('Press the <ENTER> to continue the program.');
{ Wait until File descriptor 0 (=Input) changes }
fpSelect (1,@FDS,nil,nil,nil);
{ Get rid of <ENTER> in buffer }
readln;
Writeln ('Press <ENTER> key in less than 2 seconds...');
Fpfd_zero(FDS);
FpFd_set (0,FDS);
if fpSelect (1,@FDS,nil,nil,2000)>0 then
Writeln ('Thank you !')
{ FD_ISSET(0,FDS) would be true here. }
else
Writeln ('Too late !');
end.An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
