tags:

views:

255

answers:

6

Hi everyone, I can't figure out how to display something (like hello world) in every 3 seconds. I'm writing in only C programming with gcc compiler in linux. We can stop it by Ctrl+c. I just want simplest and easiest way to manipulate thet code with my project.

Thank you so much in advance!

+4  A: 
sleep(3);

The sleep function causes your program to wait X seconds before returning. Note that this is between executions, so it won't be running your command exactly every 3 seconds, but close enough for most purposes. Also, some platforms have usleep where the u stands for micro which could get you more resolution. See the man pages for more information.

Epsilon Prime
thanks for the reply. do we need any special header for that?
Devyn
`unistd.h`: http://www.opengroup.org/onlinepubs/000095399/functions/sleep.html
Alok
+8  A: 
while(1) {
    printf("something\n");
    sleep(3);
}
Wim
I just got blinking cursor. Nothing shows up! What should I do?
Devyn
@Devyn, wait three seconds.
Pavel Shved
Well I actually waited more than 10 seconds. Here is my complete code.#include <stdio.h>#include <unistd.h>int main(void){ while(1) { printf("something\n"); sleep(3); }}
Devyn
Works for me. Try adding `fflush(stdout);` after the `printf()`?
Alok
+3  A: 

You may run into a problem in that standard output is normally buffered, which means that the implementation batches up output until it's convenient to write it. If you're writing out every three seconds (and sleep(3) is a good way to do that), and it's not showing up every three seconds, try putting in fflush(stdout); or writing to the standard error output with `fprintf(stderr, "something\n");.

David Thornley
You save me. fflush(stdout) must be used to show every changes.
Devyn
@Devyn, maybe then you should accept this answer instead?
Pavel Shved
+1  A: 

The generic approach is to write an infinite loop (while (1) { ... }) and pause your program execution each tick.

To pause, you may use sleep function from standard library. Alternatively to sleep, which can only specify sleeping time in seconds, you can use nanosleep function that allows better precision.

#include <stdio.h>
#include <time.h>

int main()
{
  struct timespec t = { 3/*seconds*/, 0/*nanoseconds*/};
  while (1){
    printf("Wait three seconds and...\n");
    nanosleep(&t,NULL);
    fflush(stdout); //see below
  }
}

Note that unless you add a newline character (\n) to the string you outpu, you most likely will see nothing, because the string is first printed to a buffer and that buffer is occasionally flushed to the terminal (that usually happens when you print a newline, but even this cannot guarantee the flush on every system). That's why it's better to add fflush call.

Pavel Shved
A: 

Stream's buffer type can be controlled using setvbuf() function. By default, streams referring to files are block buffered and terinals are line buffered (stdout) or unbuffered (stderr).

#include <stdio.h>

int main(int argc, char* argv[]) {
    setvbuf(stdout, NULL, _IONBF, 0);
    while ( 1 ) {
        printf("Wait 3 seconds... ");
        sleep(3);
    }
}

Note that there's no \n in printf. If you comment out the setvbuf() call you can see how all the output come at once after filling the buffer (usually 1024 bytes).

dtmilano
+1  A: 

Only distantly related, but a technique I use all the time is to write a program to display something once then repeat it every n seconds by using /usr/bin/watch. I get a lot more reuse that way.

Norman Ramsey