Select ()

Da Gambas-it.org - Wikipedia.

La funzione della libreria di C

int select (int __nfds, fd_set *__restrict __readfds, fd_set *__restrict __writefds, fd_set *__restrict __exceptfds, struct timeval *__restrict __timeout)

consente di controllare più file descriptor, ponendo il programma in attesa sino a quando uno o più file descriptor, posti in osservazione, diventano "pronti" per un'operazione I/O. Esso è pertnto una chiamata di sistema che dice quando un dato può essere letto o scritto in un file descriptor.


Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta. Da sottolineare che, poiché questa funzione esterna di C "select()" è omonima all'istruzione di Gambas "Select Case", bisognerà assegnarle un nome a piacere, ma si dovrà anche richiamare il suo vero nome con il comando Exec.
Dunque avremo ad esempio:

Private Extern select_C(secondi As Integer) As Integer In "libc:6" Exec "select"


In questo esempio il programma resterà in attesa sino a quando non srà stato inviato un dato tramite lo standard input:

Public Struct timeval
  tv_sec As Long    ' seconds
  tv_usec As Long   ' microseconds
End Struct


Public Struct fd_set
  __fds_bits[16] As Long    ' 16 = __FD_SETSIZE / __NFDBITS
End Struct


' int select (int __nfds, fd_set *__restrict __readfds, fd_set *__restrict __writefds, fd_set *__restrict __exceptfds, struct timeval *__restrict __timeout)
' Check the first NFDS descriptors each in READFDS (if not NULL) for read readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS (if not NULL) for exceptional conditions.
Private Extern select_C(__nfds As Integer, readfds As Fd_set, writefds As Pointer, exceptfds As Pointer, timeout As Timeval) As Integer "libc:6" Exec "select"


Public Sub Main()

 Dim rfds As New Fd_set
 Dim tv As New Timeval
 Dim rit As Integer

  rfds.__fds_bits[0] = 1

' Attende per 24 ore:
  With tv
    .tv_sec = 86400
    .tv_usec = 0
  End With

  rit = select_C(1, rfds, Null, Null, tv)
  If rit = -1 Then
    Error.Raise("Errore alla funzione 'select()' !")
  Else
    Print "Dato leggibile adesso !"
  Endif

End'