Free Downloads, Community Forum,
FAQs and Developer Resources


Make /Tools Your Home | Link to us

Today's posts | Posts since last visit | Most Active Topics

All Forums Register Login Search Subscriptions My Profile Inbox
Tool Warehouse FAQs Resources Help Member List Address Book Logout

nanosleep

 
Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [SFU / Interix / SUA Technology] >> Interix Advanced Forum >> nanosleep Page: [1]
Login
Message << Older Topic   Newer Topic >>
nanosleep - Jul. 30, '03, 10:23:29 AM   
Mike

 

Posts: 2
Joined: Jul. 30, '03,
Status: offline
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?
Post #: 1
RE: nanosleep - Jul. 30, '03, 12:49:14 PM   
Rodney

 

Posts: 3714
Joined: Jul. 9, '02,
From: /Tools lab
Status: offline
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

(in reply to Mike)
Post #: 2
RE: nanosleep - Feb. 28, '06, 12:54:44 PM   
ciarancummins

 

Posts: 2
Joined: Feb. 27, '06,
Status: offline
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

(in reply to Rodney)
Post #: 3
RE: nanosleep - Mar. 1, '06, 12:27:11 AM   
Rodney

 

Posts: 3714
Joined: Jul. 9, '02,
From: /Tools lab
Status: offline
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.

(in reply to ciarancummins)
Post #: 4
RE: nanosleep - Mar. 2, '06, 6:52:47 PM   
ciarancummins

 

Posts: 2
Joined: Feb. 27, '06,
Status: offline
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

< Message edited by ciarancummins -- Mar. 8, '06, 4:46:40 AM >

(in reply to Rodney)
Post #: 5
RE: nanosleep - Jul. 24, '06, 2:14:00 PM   
kindel

 

Posts: 22
Joined: Jun. 6, '06,
Status: offline
Just a quick note.. you might have problems if you are sleeping for significant periods of time. usleep has a maximum sleep time of 1 second (1000000 us) and according to the manpage will return EINVAL if passed a larger number. I haven't tested this yet, just came across it while trying to implement nanosleep to work around a problem with poll().

-Bob

(in reply to ciarancummins)
Post #: 6
RE: nanosleep - Jul. 24, '06, 2:57:35 PM   
Rodney

 

Posts: 3714
Joined: Jul. 9, '02,
From: /Tools lab
Status: offline
Remember that poll() only works with the /proc filesystem.
It does not work with any other. The select() API should be used instead.

(in reply to Mike)
Post #: 7
Page:   [1]
All Forums >> [SFU / Interix / SUA Technology] >> Interix Advanced Forum >> nanosleep Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Search All Forums -

Advanced search


SPONSORS



Forum Software © ASPPlayground.NET Advanced Edition 2.5 ANSI

0.047