-5

I've been coding some simple things such as printing statements but I wanted to have my processor sleeping to not have too many printing statements at the same time. I've used the library unistd.h and used the function sleep(). The problem I have encountered is that when I ask the sleep function to sleep less then one second like 0.9 is printing the same quantity of statements as if I wasn't using the function sleep() at all. It's working fine if I make it sleep more than one second but I don't want one second of sleep, I want less.

Tank you kindly for reading and/or helping.

Paul Banus
  • 27
  • 7
  • 3
    You'd have wanted to read the `sleep()` manpage before asking. This would have told you that some of your assumptions are wrong. – tofro Sep 14 '16 at 18:28
  • 1
    Please read the manual page - it oly sleeps for an integral number of seconds. Usleep perhaps – Ed Heal Sep 14 '16 at 18:28
  • 5
    What makes you think it can take a float? Have you read the documentation of sleep? – Macmade Sep 14 '16 at 18:29
  • So when you `sleep` does your program do nothing? There are creative ways to use your time slice, such as taking an average, or only printing when the value changes. On the other hand if you have "too many printing statements" you could redirect the program output to a text file and read it at your leisure. – Weather Vane Sep 14 '16 at 18:46
  • I'm voting to close this question as off-topic because the OP did not read the man page for sleep before asking his question – KevinDTimm Sep 14 '16 at 19:21

1 Answers1

5

If you read the sleep manual page you will see that it takes the time in seconds as an integer.

If you use a floating-point value when an integer value is expected, the compiler will truncate it. For example the floating-point value 0.9 will be truncated to the integer 0.

So what you are doing is effectively sleep(0).

Also on the sleep manual page (at least for the Linux manual page) you will see a link to the nanosleep function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621