Nanosleep(): differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| Riga 18: | Riga 18: | ||
End Struct | End Struct | ||
* la funzione ''nanosleep()'' con ''Extern'' e la libreria di C: libc.so.6, nella quale la funzione è contenuta: | * la funzione ''nanosleep()'' con ''Extern'' e la libreria di C: libc.so.6, nella quale la funzione è contenuta: | ||
<FONT color=Gray>' ''int nanosleep(const struct timespec *req, struct timespec *rem) | <FONT color=Gray>' ''int nanosleep(const struct timespec *req, struct timespec *rem)'' | ||
' ''Pause execution for a number of nanoseconds.''</font> | |||
Private <FONT color=#B22222>Extern nanosleep</font>(req As Timespec, rem As Timespec) As Integer In "<FONT color=#B22222>libc:6</font>" | Private <FONT color=#B22222>Extern nanosleep</font>(req As Timespec, rem As Timespec) As Integer In "<FONT color=#B22222>libc:6</font>" | ||
| Riga 33: | Riga 34: | ||
End Struct | End Struct | ||
<FONT color=Gray>' ''int nanosleep(const struct timespec *req, struct timespec *rem) | <FONT color=Gray>' ''int nanosleep(const struct timespec *req, struct timespec *rem)'' | ||
Private <FONT color=#B22222> | ' ''Pause execution for a number of nanoseconds.''</font> | ||
Private Extern <FONT color=#B22222>nanosleep</font>(req As Timespec, rem As Timespec) As Integer In libc:6 | |||
Versione delle 19:48, 7 nov 2015
La funzione della libreria di C
int nanosleep (const struct timespec *__requested_time, struct timespec *__remaining)
sospende l'esecuzione del programma per intervalli di nanosecondi (miliardesimi di secondo).
Questa funzione prevede come parametri una Struttura per specificare gli intervalli di tempo con una precisione al nanosecondo:
struct timespec {
__time_t tv_sec; /* secondi */
__syscall_slong_t tv_nsec; /* nanosecondi */
};
Il parametro dei nanosecondi va espresso in un ambito compreso fra 0 e 999999999.
Con particolare riferimento alla suddetta Struttura timespec va rilevato che nanosleep() sospende l'esecuzione del programma almeno tutto il tempo specificato nel membro *req della Struttura.
Pertanto, in Gambas utilizzeremo il primo parametro (*req) della Struttura timespec, nonché dichiareremo:
- la predetta Struttura timespec:
Public Struct timespec tv_sec As Long tv_nsec As Long End Struct
- la funzione nanosleep() con Extern e la libreria di C: libc.so.6, nella quale la funzione è contenuta:
' int nanosleep(const struct timespec *req, struct timespec *rem) ' Pause execution for a number of nanoseconds. Private Extern nanosleep(req As Timespec, rem As Timespec) As Integer In "libc:6"
Semplice esempio di uso in Gambas:
Private Const EFAULT As Integer = 14
Private Const EINVAL As Integer = 22
Public Struct timespec
tv_sec As Long
tv_nsec As Long
End Struct
' int nanosleep(const struct timespec *req, struct timespec *rem)
' Pause execution for a number of nanoseconds.
Private Extern nanosleep(req As Timespec, rem As Timespec) As Integer In libc:6
Public Sub Main()
Dim err As Integer
Dim nsReq, nsRem As New Timespec
Dim errore$ As String
' Imposta un ritardo di 300000000 nanosecondi (300000000 miliardesimi di secondo = 0,3 secondi):
With nsReq
.tv_sec = 0
.tv_nsec = 300000000
End With
' Sospende l'esecuzione per 300000000 nanosecondi:
err = nanosleep(nsReq, Null)
If err = 0 Then
Print "Chiamata alla funzione 'nanosleep()' riuscita !"
Else
Select Case err
Case EFAULT
errore$ = "Problema con la copia di informazioni da spazio utente !"
Case EINVAL
errore$ = "Il valore nel membro 'tv_nsec' non rientra nell'ambito 0 - 999999999, oppure 'tv_sec' è negativo !"
End Select
Error.Raise("Chiamata alla funzione 'nanosleep()' fallita:\n" & errore$)
Endif
End