LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-30-2007, 10:09 PM   #1
ieatsplaydoh
Member
 
Registered: Oct 2006
Location: Denver
Distribution: All of them
Posts: 62

Rep: Reputation: 15
C timer


I am an new C programmer, (yes i know its old), and i need some help with a basic program mainly using printf

ill use this example:

main()
{
printf("Programming is fun!")
printf("and Programming in C is more fun")
}

but, how do i make the terminal print out "Programming is fun!", then wait 5 seconds and print out "and Programming in C is more fun"?
i dont know if it matters, but i am using Linux

Last edited by ieatsplaydoh; 04-30-2007 at 10:10 PM.
 
Old 04-30-2007, 10:37 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Type this at the command line:

Code:
man 3 sleep
Hope this helps.
 
Old 05-01-2007, 10:33 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
Since you're not (yet) a hacker extraordinaire, forget about this method until later:

You could also have the kernel send you a signal at the specified interval, as this code demonstrates.
Code:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void handler(int); /* signal handler */

int main(int argc, char* argv[]) {
    int x = 0;
    /* register the handler for keyboard interrupt */
    if (signal(SIGINT, handler) == SIG_ERR)
        fprintf(stderr, "Couldn't handle SIGINT.\n");
    /* register the handler for alarm signal */
    if (signal(SIGALRM, handler) == SIG_ERR)
        fprintf(stderr, "Couldn't handle SIGALRM.\n");
    else
        /* set the alarm to go off in three seconds */
        alarm(3);
    while (1) {
        fprintf(stderr, "%d ", x++);
        sleep(1);
    }
    return 0;
}

static void handler(int signum) {
    if (signum == SIGINT) {
        printf("Got an interrupt signal.\n");
        exit(0);
    }
    else if (signum == SIGALRM) {
        fprintf(stderr, "ALARM! ");
        alarm(3);
    }
    return;
}
 
Old 05-01-2007, 11:42 AM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Was that supposed to be a joke?
 
Old 05-01-2007, 06:27 PM   #5
ieatsplaydoh
Member
 
Registered: Oct 2006
Location: Denver
Distribution: All of them
Posts: 62

Original Poster
Rep: Reputation: 15
um...
what?
as osor said, is that supposed to be a joke.
i am using a very old C book from the 80's
i am still in the first chapter.
i dont understand ANY of that code you just put there.
anything simpler would be appreciated
 
Old 05-01-2007, 07:02 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
I don't think you are supposed to understand that.. yet.

If you want to know what those syscalls are doing, I highly recommend getting "Advanced Programming for the Unix Enviroment". The author explains stuff far better than I would.
 
Old 05-01-2007, 09:38 PM   #7
ieatsplaydoh
Member
 
Registered: Oct 2006
Location: Denver
Distribution: All of them
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tuxdev
I don't think you are supposed to understand that.. yet.

If you want to know what those syscalls are doing, I highly recommend getting "Advanced Programming for the Unix Enviroment". The author explains stuff far better than I would.
thanks
ill get that from the library cause i can be a tightwad
i learned all i know about linux from the library too
 
Old 05-01-2007, 10:03 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
But you don’t need an expensive book to tell you that “sleep(5);” sleeps for 5 seconds (the original question)…
 
Old 05-01-2007, 10:42 PM   #9
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
Quote:
Originally Posted by osor
Was that supposed to be a joke?
Nah; at least, I didn't think it was a very funny one. A good one would have been if I'd written how to use the OCaml FFI to interact with C code that slept. No, the OP already had his answer, so I described another (more flexible) way to solve the problem. I did preface it with a statement saying that he wouldn't understand it yet... just know that it's possible to have a function called recurrently inside your code according to a timer.

Evil, I know; sorry.
 
Old 05-02-2007, 07:07 AM   #10
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
*ahem*

To unhijack this thread for just a sec (then you guys can get back to playing):

ieatsplaydoh, did

Code:
man 3 sleep
solve the problem for you?
 
Old 05-02-2007, 10:19 AM   #11
knobby67
Member
 
Registered: Mar 2006
Posts: 546

Rep: Reputation: 42
use slpeep(3); between the two prinf's you'll also need #include <unistd.h> as a header for the sleep function so

#include<stdio.h>
#include <unistd.h>

main()
{
printf("Programming is fun!");
slpeep(3);
printf("and Programming in C is more fun");
}
 
Old 05-02-2007, 11:31 AM   #12
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,455

Rep: Reputation: 84
Hya,

I guess it would be a good idea to flush buffer by fflush(NULL); after printf("Programming is fun");
Code:
printf("Programming is fun");
fflush(NULL);
system("sleep 5");
printf("fun fun fun");
Happy Penguins!

Last edited by kaz2100; 05-03-2007 at 09:40 AM.
 
Old 05-02-2007, 07:36 PM   #13
ieatsplaydoh
Member
 
Registered: Oct 2006
Location: Denver
Distribution: All of them
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wjevans_7d1@yahoo.co
*ahem*

To unhijack this thread for just a sec (then you guys can get back to playing):

ieatsplaydoh, did

Code:
man 3 sleep
solve the problem for you?
oops!
i saw that..
but for some reason i thought it was for bash and i ignored it.
oops...
maybe that'll work...
*checks*
yay! it works
thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 04:49 PM
About timer.... help rajsun Programming 3 05-24-2005 07:06 AM
timer in C timmy334 Programming 10 11-18-2003 12:40 PM
timer... perdesiz Linux - Software 1 09-18-2003 04:39 AM
QT Timer kusio Linux - Software 1 04-15-2003 06:48 PM


All times are GMT -5. The time now is 11:29 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration