Differenze tra le versioni di "Nanosleep()"

Da Gambas-it.org - Wikipedia.
Riga 13: Riga 13:
 
<BR>Pertanto, in Gambas utilizzeremo il primo parametro (''*req'') della Struttura ''timespec'', nonché dichiareremo:
 
<BR>Pertanto, in Gambas utilizzeremo il primo parametro (''*req'') della Struttura ''timespec'', nonché dichiareremo:
 
* la predetta Struttura ''timespec'':
 
* la predetta Struttura ''timespec'':
  Public Struct nsleep
+
  Public Struct timespec
 
   tv_sec As Integer
 
   tv_sec As Integer
 
   tv_nsec As Long
 
   tv_nsec As Long
Riga 19: Riga 19:
 
* 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>
 
  <FONT color=Gray>' ''int nanosleep(const struct timespec *req, struct timespec *rem);''</font>
  Private <FONT color=#B22222>Extern nanosleep</font>(req As Nsleep, rem As Nsleep) 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 28: Riga 28:
 
   
 
   
 
   
 
   
  Public Struct nsleep
+
  Public Struct timespec
 
   tv_sec As Integer
 
   tv_sec As Integer
 
   tv_nsec As Long
 
   tv_nsec As Long
Riga 34: Riga 34:
 
   
 
   
 
  <FONT color=Gray>' ''int nanosleep(const struct timespec *req, struct timespec *rem);''</font>
 
  <FONT color=Gray>' ''int nanosleep(const struct timespec *req, struct timespec *rem);''</font>
  Private <FONT color=#B22222>Extern nanosleep</font>(req As Nsleep, rem As Nsleep) 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 40: Riga 40:
 
   
 
   
 
   Dim err As Integer
 
   Dim err As Integer
   Dim nsReq, nsRem As New Nsleep
+
   Dim nsReq, nsRem As New Timespec
 
   Dim errore$ As String
 
   Dim errore$ As String
 
   
 
   

Versione delle 08:14, 9 set 2014

La funzione della libreria di C

int nanosleep(const struct timespec *req, struct timespec *rem);

sospende l'esecuzione del programma per intervalli di nanosecondi (miliardesimi di secondi).

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 */
   long   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 Integer
  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);
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 Integer
  tv_nsec As Long
End Struct

' int nanosleep(const struct timespec *req, struct timespec *rem);
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