Differenze tra le versioni di "Nanosleep()"

Da Gambas-it.org - Wikipedia.
Riga 8: Riga 8:
 
     __syscall_slong_t  tv_nsec;      /* nanosecondi */
 
     __syscall_slong_t  tv_nsec;      /* nanosecondi */
 
  };
 
  };
Il parametro dei ''nanosecondi'' va espresso in un ambito compreso fra 0 e 999999999.
+
Il parametro dei ''nanosecondi'' va espresso in un ambito compreso fra <FONT color=#B22222>0</font> e <FONT color=#B22222>999999999</font>.
  
 
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.
 
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.
Riga 25: Riga 25:
  
 
Semplice esempio di uso in Gambas:
 
Semplice esempio di uso in Gambas:
 +
Library "libc:6"
 +
 
  Private Const EFAULT As Integer = 14
 
  Private Const EFAULT As Integer = 14
 
  Private Const EINVAL As Integer = 22
 
  Private Const EINVAL As Integer = 22
Riga 36: Riga 38:
 
  <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>
 
  ' ''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
+
  Private Extern <FONT color=#B22222>nanosleep</font>(req As Timespec, rem As Timespec) As Integer
 
   
 
   
 
   
 
   

Versione delle 16:41, 20 giu 2016

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:

Library "libc:6"

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


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