nanosleep (Full Version)

All Forums >> [SFU / Interix / SUA Technology] >> Interix Advanced Forum



Message


Mike -> nanosleep (Jul. 30, '03, 10:23:29 AM)

We are evaluating SFU 3.0 and have the need to use the
nanosleep function which does not seem to be supported.

Is there a work around for nanosleep and other time functions?




Rodney -> RE: nanosleep (Jul. 30, '03, 12:49:14 PM)

The usleep() API is the closest/easiest to use.
You could also use the setitimer/getitimer() API' s.
Interix doesn' t have restartable API (after a signal is delivered).

Nanosleep' s struct timespec isn' t declared since there is no API using it. But if you declare it along with a #define to usleep() in an appropriate header file for your code.
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
#define nanosleep(x, y) usleep( ((x)->tv_sec)*1000000 + ((y)->tv_nsec)/1000 )

Which other time functions were you interested in?
Ones like asctime, ctime, sleep, setitimer, etc are already there.

- Rodney




ciarancummins -> RE: nanosleep (Feb. 28, '06, 12:54:44 PM)

Hi,

I am trying to implement this nanosleep function for my code but I am getting errors, probably because I am implementing it incorrectly...
struct timespec is defined in an included header file already, and I have defined nanosleep in this file.

#include <sys/time.h>
#include "vars.h"
#include "eventgen.h"
#include "flowop.h"
#include "ipc.h"


void
eventgen_usage()
{
fprintf(stderr, "eventgen rate=<rate>\n");
fprintf(stderr, "\n");
}

/* Producer side of rate eventgen */
void
eventgen_thread(void)
{
struct timespec sleeptime ;
hrtime_t last;
hrtime_t delta;
int count;
#define nanosleep(x,y) usleep( ((x)->tv_sec)*1000000 + ((y)->tv_nsec)/10
00 )
last = gethrtime();
while(1) {
if (filebench_shm->eventgen_hz == 0) {
sleep(1);
continue;
}
/* Sleep for 10xperiod */
sleeptime.tv_sec = 0;
sleeptime.tv_nsec = 10000000000UL / filebench_shm->eventgen_hz;
if (sleeptime.tv_nsec < 1000UL)
sleeptime.tv_nsec = 1000UL;
sleeptime.tv_sec = sleeptime.tv_nsec / 1000000000UL;
if (sleeptime.tv_sec > 0)
sleeptime.tv_nsec -= (sleeptime.tv_sec * 1000000000UL);
nanosleep(&sleeptime, NULL);
.
.

Error on compile:

source='eventgen.c' object='eventgen.o' libtool=no \
depfile='.deps/eventgen.Po' tmpdepfile='.deps/eventgen.TPo' \
depmode=gcc3 /bin/sh ../config/depcomp \
gcc -D_REENTRANT -I. -I. -I.. -I../intl -I/usr/local/include/gsl -DFILEBENCHDIR
=\"/usr/local/filebench\" -g -O2 -D_POSIX_C_SOURCE=19309 -D_ALL_SOURCE -DYYDE
BUG=1 -c `test -f eventgen.c || echo './'`eventgen.c
eventgen.c: In function `eventgen_thread':
eventgen.c:40: warning: integer constant is too large for "unsigned long" type
eventgen.c:46: warning: dereferencing `void *' pointer
eventgen.c:46: error: request for member `tv_nsec' in something not a structure
or union
make: *** [eventgen.o] Error 1
% make

What am I doing wrong?

Thanks for any help,
Ciaran




Rodney -> RE: nanosleep (Mar. 1, '06, 12:27:11 AM)

The essence of my example above is that this is a standalone workaround. It's a "fake-out"
by using usleep() as the API. You need to redefine the "struct timespec". Otherwise you
don't get 'tv_nsec'. This is to avoid rewriting sections of your code.
So don't use the 'struct timespec' from the header file with this fake-out.
If 'tv_nsec' already existed then a real nanosleep() API likely would too.




ciarancummins -> RE: nanosleep (Mar. 2, '06, 6:52:47 PM)

Hi, thanks for the reply.

I am migrating the code from a solaris build. There is a nanosleep() syscall in solaris, so there is no function in the code. The following compiles, but should it produce the correct results? Thanks in advance for any comments.... The original nanosleep syntax is commented out below the new one...


eventgen_thread(void)
{
       struct timespec sleeptime;
       hrtime_t last;
       hrtime_t delta;
       int count;

       #define nanosleep(x, y) usleep( ((x)->tv_sec)*1000000 + ((x)->tv_nsec)/1
000 )

       last = gethrtime();
       while(1) {
               if (filebench_shm->eventgen_hz == 0) {
                       sleep(1);
                       continue;
               }
               /* Sleep for 10xperiod */
               sleeptime.tv_sec = 0;
               sleeptime.tv_nsec = 10000000000UL / filebench_shm->eventgen_hz;
               if (sleeptime.tv_nsec < 1000UL)
                       sleeptime.tv_nsec = 1000UL;
               sleeptime.tv_sec = sleeptime.tv_nsec / 1000000000UL;
               if (sleeptime.tv_sec > 0)
                       sleeptime.tv_nsec -= (sleeptime.tv_sec * 1000000000UL);
               nanosleep(&sleeptime, NULL); 


Regards,
Ciaran




Page: [1]



Forum Software © ASPPlayground.NET Advanced Edition 2.5 ANSI

0.031