next up previous contents index
Next: SelectText Up: Functions and procedures Previous: SeekDir

Select

   

Declaration:

Function Select (N : Longint;
var readfds,writefds,exceptfds : PFDset; Var Timeout) : Longint;

Description:

Select checks one of the file descriptors in the FDSets to see if its status changed.

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 characters become available for reading. The entries in writefds are checked to see if it is OK to write to them, while entries in exceptfds are cheked to see if an exception occorred on them.

You can use the functions FD_Clear, FD_Clr, FD_Set, FD_IsSet to manipulate the individual elements of a set.

The pointers can be nil.

N is the largest index of a nonzero entry plus 1. (= the largest file-descriptor + 1).

TimeOut can be used to set a time limit. If TimeOut can be two types :

  1. TimeOut is of type PTime and contains a zero time, the call returns immediately. If TimeOut is Nil, the kernel will wait forever, or until a status changed.
  2. TimeOut is of type Longint. If it is -1, this has the same effect as a Timeout of type PTime which is Nil. Otherwise, TimeOut contains 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 timout was reached, and no decsriptor changed, zero is returned; The arrays of indexes are undefined after that. On error, -1 is returned.

Errors:

On error, the function returns -1, and Errors are reported in LinuxError :

SYS_EBADF
An invalid descriptot was specified in one of the sets.
SYS_EINTR
A non blocked signal was caught.
SYS_EINVAL
N is negative or too big.
SYS_ENOMEM
Select was unable to allocate memory for its internal tables.

See also:

SelectText, GetFS, FD_Clear, FD_Clr, FD_Set, FD_IsSet

Example
Program Example33;

{ Program to demonstrate the Select function. }

Uses linux;

Var FDS : FDSet;

begin
  FD_Zero (FDS);
  FD_Set (0,FDS);
  Writeln ('Press the <ENTER> to continue the program.');
  { Wait until File descriptor 0 (=Input) changes }
  Select (1,@FDS,nil,nil,nil);
  { Get rid of <ENTER> in buffer }
  readln;
  Writeln ('Press <ENTER> key in less than 2 seconds...');
  FD_Zero (FDS);
  FD_Set (0,FDS);
  if Select (1,@FDS,nil,nil,2000)>0 then 
    Writeln ('Thank you !')
    { FD_ISSET(0,FDS) would be true here. }
  else
    Writeln ('Too late !');
end.


next up previous contents index
Next: SelectText Up: Functions and procedures Previous: SeekDir

Michael Van Canneyt
Thu Sep 10 13:59:33 CEST 1998