ransleep.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of the License "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description:
  15. *
  16. */
  17. /*
  18. ransleep.c: sleep for some time period specified in milliseconds
  19. optionally choose a random time up to the maximum time specified.
  20. Description: Useful for delays between retries and for perturbing the
  21. start times of tools which might cause resource starvation
  22. if they all execute at exactly the same time.
  23. */
  24. #include "../config.h"
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. // OS specific headers
  29. #ifdef HOST_WIN
  30. #include <windows.h>
  31. #else
  32. #include <sys/types.h>
  33. #include <sys/select.h>
  34. #endif
  35. int main(int argc, char *argv[])
  36. {
  37. srand(getpid());
  38. int millisecs=0;
  39. if (argc != 2)
  40. {
  41. fprintf(stderr,"Must supply numeric argument - maximum milliseconds to sleep\n");
  42. exit(1);
  43. }
  44. millisecs = atoi(argv[1]);
  45. if (millisecs <= 0 )
  46. {
  47. fprintf(stderr,"Must supply numeric argument > 0 - maximum milliseconds to sleep\n");
  48. exit(1);
  49. }
  50. millisecs = rand() % millisecs;
  51. fprintf(stderr,"random sleep for %d milliseconds\n", millisecs);
  52. #ifndef HAS_MILLISECONDSLEEP
  53. struct timeval wtime;
  54. wtime.tv_sec=millisecs/1000;
  55. wtime.tv_usec=(millisecs % 1000) * 1000;
  56. select(0,NULL,NULL,
  57. NULL, &wtime);
  58. #else
  59. Sleep(millisecs);
  60. #endif
  61. return 0;
  62. }