[SOLVED] Strange interaction between sleep() and printf strings: GCC/Debian
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Strange interaction between sleep() and printf strings: GCC/Debian
I encountered a strange problem in the very early stages of prototyping a test routine in GCC under Debian. The program follows, it should compile and run, please excuse any syntactical or style shortcomings, cleanup will come later.
Sorry, my indents are being removed by the site. Can't find a way to put them in.
The program loops with a 1 second sleep delay at the end of the loop. As presented, what I saw is that the sleep delay was being bypassed for certain values of the counter, two of the Entering statements would occur together. When I removed the ‘\n’s from the last two printf statements, the problem disappeared.
I wasn’t aware that there would be any interaction between control characters in printf strings and the sleep function. Can anyone explain what's going on?
The only thing I see is the location of the \n as previously noted, and a warning about implicit declaration of the sleep(1) function.
Your program seems to work for me and the only mod I made was to relocate the \n to the end of the string in each of those 3 printf statements.
With the original code as posted I added a single printf statement
Code:
printf ("sleeping\n") ;
immediately before the sleep(1) statement and this is the result of the compile and test.
Yes, it seemed to work fine with no changes, but the location of the \n at the beginnint of the print line causes the extra spacing in the output lines as soon as one additional printf statement is added that ends with a \n. This clearly shows that the output is buffered until it receives the \n (newline) output.
Code:
$ gcc -o testitprint test.c
test.c: In function ‘main’:
test.c:34:1: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
34 | sleep(1) ;
| ^~~~~
[jvian@eagle test]$ ./testitprint
Entering 4sleeping
Entering 3sleeping
Entering 2sleeping
Entering 1
LED ON, MCCNT = 14000sleeping
Entering 0
LED OFF, dividers setsleeping
Entering 6sleeping
I then modified the 3 original printf statements to move the \n to the end of the line and this was the result.
Code:
$ gcc -o testitprint2 test2.c
test.c: In function ‘main’:
test.c:34:1: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
34 | sleep(1) ;
| ^~~~~
[jvian@eagle test]$ ./testitprint2
Entering 4
sleeping
Entering 3
sleeping
Entering 2
sleeping
Entering 1
LED ON, MCCNT = 14000
sleeping
Entering 0
LED OFF, dividers set
sleeping
Modified program with the original printf statements altered and the added printf statement.
Code:
$ cat test2.c
/* Test program for ......
November 22
*/
#include <stdio.h>
#include <malloc.h>
int main()
{
int counter, ledstate ;
ledstate = 0 ;
counter = 4 ;
while (1)
{
printf ("Entering %d\n", counter) ;
if (counter > 0) counter = counter - 1 ;
if (ledstate == 1)
{
ledstate = 0 ;
counter = 6 ;
printf ("LED OFF, dividers set\n") ;
}
else if (ledstate == 0 && counter == 0)
{
ledstate = 1 ;
printf ("LED ON, MCCNT = 14000\n") ;
}
printf ("sleeping\n") ;
sleep(1) ;
}
return 0 ;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.