[C++/Linux] Nanosleep

Pagina: 1
Acties:

  • Olaf van der Spek
  • Registratie: September 2000
  • Niet online
Ik weet wel dat dit de functie is:
int nanosleep(const struct timespec *rqtp, struct timespec
*rmtp);

Maar waar kan ik vinden wat ik als rqtp mee moet geven?

  • Pooh
  • Registratie: April 2001
  • Niet online

Pooh

Lees eens een boek

Function: int nanosleep (const struct timespec *requested_time, struct timespec *remaining)
If resolution to seconds is not enough the nanosleep function can be used. As the name suggests the sleep interval can be specified in nanoseconds. The actual elapsed time of the sleep interval might be longer since the system rounds the elapsed time you request up to the next integer multiple of the actual resolution the system can deliver.

*requested_time is the elapsed time of the interval you want to sleep.

The function returns as *remaining the elapsed time left in the interval for which you requested to sleep. If the interval completed without getting interrupted by a signal, this is zero.

struct timespec is described in See section Elapsed Time.

If the function returns because the interval is over the return value is zero. If the function returns @math{-1} the global variable errno is set to the following values:

EINTR
The call was interrupted because a signal was delivered to the thread. If the remaining parameter is not the null pointer the structure pointed to by remaining is updated to contain the remaining elapsed time.
EINVAL
The nanosecond value in the requested_time parameter contains an illegal value. Either the value is negative or greater than or equal to 1000 million.
This function is a cancellation point in multi-threaded programs. This is a problem if the thread allocates some resources (like memory, file descriptors, semaphores or whatever) at the time nanosleep is called. If the thread gets canceled these resources stay allocated until the program ends. To avoid this calls to nanosleep should be protected using cancellation handlers.

The nanosleep function is declared in `time.h'.
Data Type: struct timespec
The struct timespec structure represents an elapsed time. It is declared in `time.h' and has the following members:

long int tv_sec
This represents the number of whole seconds of elapsed time.
long int tv_nsec
This is the rest of the elapsed time (a fraction of a second), represented as the number of nanoseconds. It is always less than one billion.
Lukt 't je daarmee? ;)

Verwijderd

Op woensdag 17 april 2002 17:35 schreef OlafvdSpek het volgende:
Ik weet wel dat dit de functie is:
int nanosleep(const struct timespec *rqtp, struct timespec
*rmtp);

Maar waar kan ik vinden wat ik als rqtp mee moet geven?
man usleep of man nanosleep

Werkt altijd ;)

  • XTerm
  • Registratie: Juli 2001
  • Laatst online: 10-06-2025
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    struct timespec *x;
    long s;
    x=malloc(sizeof(*x));
    printf("Number of nsecs to sleep : ");
    scanf("%i",&s);
    printf("Sleeping :::");
    x->tv_nsec=s;
     nanosleep(x,NULL);
    printf("::: done\n");
    return(0);
}

Het is ook eens interessant om x->tv_sec=<int> te doen, dat is wel merkbaar :)

  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 09-09 21:25

Creepy

Tactical Espionage Splatterer

ah.. en die ene nano sec. of micro sec. haal je toch niet. Ergens op de lkml gelezen dat dit iets van 10msec is ofzo..

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney


  • JayTaph
  • Registratie: Oktober 1999
  • Laatst online: 28-11-2025

JayTaph

Portability is for canoes.

Op woensdag 17 april 2002 21:59 schreef XTerm89D het volgende:
code:
1
2
3
4
5
    struct timespec *x;
    long s;
    x=malloc(sizeof(*x));
    x->tv_nsec=s;
}
He jakkes.. akelige malloc'jes waarna de helft van de variabelen worden geset *vraagt* gewoon om urenlang zoeken waarom je proggie niet werkt :+

Yo dawg, I heard you like posts so I posted below your post so you can post again.


  • Olaf van der Spek
  • Registratie: September 2000
  • Niet online
Op woensdag 17 april 2002 17:58 schreef Poohbear het volgende:
Lukt 't je daarmee? ;)
Ja, maar waar heb je dat tweede gedeelte gevonden? Met man nanosleep vond ik alleen het eerste gedeelte.
Op woensdag 17 april 2002 22:15 schreef Creepy het volgende:
ah.. en die ene nano sec. of micro sec. haal je toch niet. Ergens op de lkml gelezen dat dit iets van 10msec is ofzo..
Ik had juist gehoopt hiermee ongeveer 5 msec te kunnen wachten. Ik wil met een bepaalde rate UDP packets wegsturen, maar waar vind ik een timer die wel zo'n resolutie heeft dan?

  • Creepy
  • Registratie: Juni 2001
  • Laatst online: 09-09 21:25

Creepy

Tactical Espionage Splatterer

Op donderdag 18 april 2002 12:10 schreef OlafvdSpek het volgende:

[..]

Ja, maar waar heb je dat tweede gedeelte gevonden? Met man nanosleep vond ik alleen het eerste gedeelte.
[..]

Ik had juist gehoopt hiermee ongeveer 5 msec te kunnen wachten. Ik wil met een bepaalde rate UDP packets wegsturen, maar waar vind ik een timer die wel zo'n resolutie heeft dan?
Niet.. helaas... zul je zelf moeten schrijven.

Ik heb het zelf gedaan met gettimeofday() en dan net zolang totdat er een bepaald aantal microsecs is verstreken. (aansturing van LCD... wachttijd van 40 micro sec's.

Deze methode werd voor Linux aangeraden om de LKML door Alan Cox, dus zal wel ok zijn. Zal zo eens zoeken of ik die post kan terug vinden.

"I had a problem, I solved it with regular expressions. Now I have two problems". That's shows a lack of appreciation for regular expressions: "I know have _star_ problems" --Kevlin Henney

Pagina: 1