LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 11-12-2003, 02:38 AM   #1
timmy334
LQ Newbie
 
Registered: Nov 2003
Distribution: Gentoo
Posts: 4

Rep: Reputation: 0
timer in C


I'm teaching myself C and i'm wondering how to implement something like Java's sleep function so i can make something happen every second or so. For example like a simple spinner of ascii that uses \ | / - in it and switches characters every second. I'm thinking it is in the time.h but I haven't seen how to do implement that.
 
Old 11-12-2003, 02:54 AM   #2
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
nanosleep or sleep will work for you. nanosleep runs on nanoseconds and sleep runs on seconds.

nanosleep:
Code:
SYNOPSIS
       #include <time.h>

       int nanosleep(const struct timespec *req, struct timespec *rem);

DESCRIPTION
       nanosleep delays the execution of the program for at least the time speci-
       fied in *req.  The function can return earlier if a signal has been deliv-
       ered to the process. In this case, it returns -1, sets errno to EINTR, and
       writes the remaining time into the structure pointed to by rem unless  rem
       is  NULL.   The value of *rem can then be used to call nanosleep again and
       complete the specified pause.

       The structure timespec is used to specify intervals of time with  nanosec-
       ond precision. It is specified in <time.h> and has the form

              struct timespec
              {
                      time_t  tv_sec;         /* seconds */
                      long    tv_nsec;        /* nanoseconds */
              };

       The  value of the nanoseconds field must be in the range 0 to 999 999 999.

       Compared to sleep(3) and usleep(3), nanosleep has  the  advantage  of  not
       affecting  any  signals,  it  is standardized by POSIX, it provides higher
       timing resolution, and it allows to continue a sleep that has been  inter-
       rupted by a signal more easily.
sleep:

Code:
SLEEP(3)                    Linux Programmer's Manual                    SLEEP(3)

NAME
       sleep - Sleep for the specified number of seconds

SYNOPSIS
       #include <unistd.h>

       unsigned int sleep(unsigned int seconds);

DESCRIPTION
       sleep() makes the current process sleep until seconds seconds have elapsed
       or a signal arrives which is not ignored.

Last edited by megaspaz; 11-12-2003 at 02:57 AM.
 
Old 11-12-2003, 09:14 AM   #3
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
#include <signal.h>
#include <unistd.h>

/* */
void tick(int i);

/* */
void setAlarm(int delay) {
signal(SIGALRM, tick);
}

/* */
void tick(int i) {

printf("

}
 
Old 11-12-2003, 09:15 AM   #4
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
#include <signal.h>
#include <unistd.h>

/* */
void tick(int i);

/* */
void setAlarm(int delay) {
signal(SIGALRM, tick);
}

/* */
void tick(int i) {
static nr = 0;
printf(" tick tack tock: nr %d\n", nr++);

}
 
Old 11-12-2003, 09:17 AM   #5
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
#include <signal.h>
#include <unistd.h>

/* */
void tick(int i);

/* */
void setAlarm(int delay) {
signal(SIGALRM, tick);
}

/* */
void tick(int i) {
static nr = 0;
printf(" tick tack tock: nr %d\n", nr++);
setAlarm(5);
}

int main(int argc, char** argv) {
setAlarm(5);
/* do something that .. does something here .. like a sleep*/
sleep(60);
return 0;
}

......................

And yes, I have no clue how to make this a code section.. and I think theres a autosubmit thing.. I hate that.. loses focus.. and blamm.. submitted.
 
Old 11-12-2003, 11:06 AM   #6
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
you can put your code inside code tags:

[ code ] code inside here [ /code ]

no spaces before or after the "[" and "]" though.

there's a button on the posting page too labeled PHP, but it looks like a single line entry field.
 
Old 11-17-2003, 05:08 PM   #7
timmy334
LQ Newbie
 
Registered: Nov 2003
Distribution: Gentoo
Posts: 4

Original Poster
Rep: Reputation: 0
Okay, here is what I have so far. It works without the sleep(). Now I just need to get the sleep to work correctly. any ideas?
Code:
#include <stdio.h>
#include <unistd.h>
                                                                                
main()
{
        char s[4] = {'-', '\\', '|', '/'};
        int i;
        int x = 1;
        printf("Behold, a spinner: ");
        while(x = 1)
        {
                for(i = 0; i < 5; ++i)
                {
                        printf("%c", s[i]);
                        //sleep(60);
                        printf("%c", '\10');
                }
        }
}

Last edited by timmy334; 11-17-2003 at 05:12 PM.
 
Old 11-17-2003, 07:49 PM   #8
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
sleep(60);

Will make it so that you have to wait a full
minute between baton movements.
Code:
for (i = 0; i < 5; i++) {
	printf("%c", s[i]);
       fflush(NULL); /* Make sure to flush stdout */
	sleep(1); /* Sleep for 1 second */
	printf("\b");
}

Last edited by LogicG8; 11-17-2003 at 08:06 PM.
 
Old 11-18-2003, 12:45 AM   #9
timmy334
LQ Newbie
 
Registered: Nov 2003
Distribution: Gentoo
Posts: 4

Original Poster
Rep: Reputation: 0
ok, this is what I ended up with and it works perfectly. The thing that helped was flushing stdout. I didn't know about that and not doing that was causing nothing to appear. Thanks for the info!!!
Code:
#include <stdio.h>
#include <unistd.h>
                                                                                
main()
{
        char s[4] = {'-', '\\', '|', '/'};
        int i;
        int x = 1;
        printf("Behold, a spinner: ");
        while(x = 1)
        {
                for(i = 0; i <= 3; i++)
                {
                        printf("%c", s[i]);
                        fflush(NULL);
                        sleep(1);
                        printf("%c", '\10');
                }
        }
}
 
Old 11-18-2003, 12:47 AM   #10
timmy334
LQ Newbie
 
Registered: Nov 2003
Distribution: Gentoo
Posts: 4

Original Poster
Rep: Reputation: 0
Just a side question: What's the syntax to use the nanosecond function in there?
 
Old 11-18-2003, 11:40 AM   #11
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
#include <time.h>

int nanosleep(const struct timespec *req, struct timespec *rem);
 
  


Reply



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
About timer.... help rajsun Programming 3 05-24-2005 06:06 AM
timer in c?? AcidHell2 Programming 9 08-12-2004 01:01 AM
First timer, help!!!!! jcniest5 Linux - Newbie 7 02-24-2004 08:32 AM
timer... perdesiz Linux - Software 1 09-18-2003 03:39 AM
QT Timer kusio Linux - Software 1 04-15-2003 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 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