LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 11-16-2022, 06:47 AM   #1
BruceV
LQ Newbie
 
Registered: Jan 2016
Posts: 22

Rep: Reputation: Disabled
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?

Thanks for any info.

/* Test program for ......
November 22
*/

#include <stdio.h>
#include <malloc.h>

int main()
{
int counter, ledstate ;

ledstate = 0 ;
counter = 4 ;

while (1)
{
printf ("\nEntering %d", counter) ;

if (counter > 0) counter = counter - 1 ;

if (ledstate == 1)
{
ledstate = 0 ;
counter = 6 ;
printf ("\nLED OFF, dividers set") ;
}

else if (ledstate == 0 && counter == 0)
{
ledstate = 1 ;
printf ("\nLED ON, MCCNT = 14000") ;
}

sleep(1) ;
}
return 0 ;
}

Last edited by BruceV; 11-16-2022 at 06:49 AM. Reason: cleanup
 
Old 11-16-2022, 07:05 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
would be nice to use code tags for your code to keep formatting.
In general the print output can be buffered, probably you need to switch it off.
 
1 members found this post helpful.
Old 11-16-2022, 08:05 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
put your \n at the end of the string if you want it to be printed right away. By default, output is buffered until \n.
 
1 members found this post helpful.
Old 11-16-2022, 06:11 PM   #4
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
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.
 
1 members found this post helpful.
Old 11-16-2022, 10:09 PM   #5
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 480

Rep: Reputation: 179Reputation: 179
Code:
/* 
   Test program for ......
   November 22 
*/

#include <stdio.h>
#include <malloc.h> // not needed, should comment out or delete
#include <unistd.h> // for sleep() get rid of implicit warning

int main()
   {
   int counter, ledstate;

   ledstate = 0;
   counter  = 4;
   while (1)
      {
      printf("\nEntering %d", counter);

      if (counter > 0) 
         counter = counter - 1;

      if (ledstate == 1)
         {
         ledstate = 0;
         counter  = 6;
         printf("\nLED OFF, dividers set");
         }
      else if ((ledstate == 0) && (counter == 0))
         {
         ledstate = 1;
         printf("\nLED ON, MCCNT = 14000");
         }
      sleep(1);
      }
   return 0;
   }
output:
Code:
Entering 4
Entering 3
Entering 2
Entering 1
LED ON, MCCNT = 14000
Entering 0
LED OFF, dividers set
Entering 6
Entering 5
Entering 4
Entering 3
Entering 2
Entering 1
LED ON, MCCNT = 14000
Entering 0
LED OFF, dividers set
Entering 6
Entering 5
Entering 4
Entering 3
Entering 2
Entering 1
LED ON, MCCNT = 14000
...
Works fine here.
 
Old 11-17-2022, 12:38 PM   #6
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
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 ;
}
 
  


Reply

Tags
debian 10, gcc 4.9.4, printf, sleep



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange interaction between camera and wifi on Thikpad W530 bimboleum Slackware 1 01-24-2013 09:23 AM
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 08:21 AM
Strange interaction between mic/webcam and wifi + mouse fakie_flip Linux - Kernel 3 06-23-2010 12:56 AM
How is 'man 3 printf' different from 'man printf' ?? purpleburple Linux - General 3 09-23-2002 12:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration