PacktPublishing / Linux-System-Programming-Techniques

Linux System Programming Techniques, published by Packt
MIT License
96 stars 44 forks source link

ch-9/terminal-size.c : calling sleep(0.1) does not delay the program by 100 ms #3

Closed zhenya4880 closed 1 year ago

zhenya4880 commented 2 years ago

Function sleep() has an integer argument. See man 3 sleep.
Calling sleep(0.1) is equivalent to calling sleep(0). It will definitely not give you a 100ms delay.
Please, use nanosleep() to get a delay of 100 ms or write sleep(0) "to avoid draining the system resources".
By the way, calling a sleep(0) in most cases is not a good idea

jackbenny commented 1 year ago

You are absolutely correct @zhenya4880, thanks for spotting this. I must have confused the sleep() function with the sleep command when I wrote this... Embarrassing to say the least. Nonetheless, I am grateful you found it! 👍😊 I'll update the code and notify Packt Publishing to update the book.

By the way, sleep(0) is a nice hack, even though it's a hack and not recommended.

jackbenny commented 1 year ago

I have updated the errata and the code with sleep(0). Thank you for pointing this out 😊