Select (): differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| Riga 4: | Riga 4: | ||
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 " | 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()" è <SPAN style="text-decoration:underline">omonima</span> 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''. | ||
<BR>Dunque avremo ad esempio: | <BR>Dunque avremo ad esempio: | ||
Private <FONT color=#B22222>Extern select_C</font>(secondi As Integer) As Integer In "<FONT color=#B22222>libc:6</font>" <FONT color=#B22222>'''Exec "select"'''</font> | Private <FONT color=#B22222>Extern select_C</font>(secondi As Integer) As Integer In "<FONT color=#B22222>libc:6</font>" <FONT color=#B22222>'''Exec "select"'''</font> | ||
| Riga 26: | Riga 26: | ||
Public Sub Main() | |||
Dim rfds As New Fd_set | Dim rfds As New Fd_set | ||
| Riga 32: | Riga 32: | ||
Dim rit As Integer | Dim rit As Integer | ||
rfds.__fds_bits[0] = 1 | |||
<FONT Color=gray>' ''Attende per 24 ore:''</font> | <FONT Color=gray>' ''Attende per 24 ore:''</font> | ||
With tv | |||
.tv_sec = 86400 | |||
.tv_usec = 0 | |||
End With | |||
rit = <FONT color=#B22222>select_C(1, rfds, Null, Null, tv)</font> | |||
If rit = -1 Then | |||
Error.Raise("Errore alla funzione 'select()' !") | |||
Else | |||
Print "Dato leggibile adesso !" | |||
Endif | |||
End | |||
Versione delle 15:33, 16 giu 2024
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