/******************************************************************** * * Copyright 2010 by Sean Conner. All Rights Reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Comments, questions and criticisms can be sent to: sean@conman.org * * -------------------------------------------------------------------- * * Used to test the resolution of nanosleep(). To compile: * * Linux: * gcc -std=c99 -Wall -Wextra -pedantic -g -o nanosleep nanosleep.c * * Solaris: * cc -xc99 -g -o nanosleep nanosleep.c -lrt * * To run: * ./nanosleep * ./nanosleep X # X = number of seconds to run program * ************************************************************************/ #define _POSIX_C_SOURCE 200112L #include #include #include #include #include #include #define DEFSECS 3 /*********************************************************************/ static int timevalcmp( const struct timeval *const restrict a, const struct timeval *const restrict b ) { assert(a != NULL); assert(b != NULL); if (a->tv_sec < b->tv_sec) return -1; else if (a->tv_sec > b->tv_sec) return 1; if (a->tv_usec < b->tv_usec) return -1; else if (a->tv_usec > b->tv_usec) return 1; return 0; } /*********************************************************************/ int main(int argc,char *argv[]) { struct timespec interval; struct timeval start; struct timeval stop; unsigned long count; unsigned long secs; if (argc == 1) secs = DEFSECS; else secs = strtoul(argv[1],NULL,10); gettimeofday(&stop,NULL); stop.tv_sec += secs; count = 0; while(true) { gettimeofday(&start,NULL); if (timevalcmp(&start,&stop) > 0) break; interval.tv_sec = 0; interval.tv_nsec = 1000; nanosleep(&interval,NULL); count++; } printf("%lu %lu/sec\n",count,count / secs); return EXIT_SUCCESS; }