LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   timer in C (https://www.linuxquestions.org/questions/programming-9/timer-in-c-115100/)

timmy334 11-12-2003 02:38 AM

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.
:Pengy:

megaspaz 11-12-2003 02:54 AM

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.


worldmagic 11-12-2003 09:14 AM

#include <signal.h>
#include <unistd.h>

/* */
void tick(int i);

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

/* */
void tick(int i) {

printf("

}

worldmagic 11-12-2003 09:15 AM

#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++);

}

worldmagic 11-12-2003 09:17 AM

#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.

megaspaz 11-12-2003 11:06 AM

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.

timmy334 11-17-2003 05:08 PM

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');
                }
        }
}


LogicG8 11-17-2003 07:49 PM

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");
}


timmy334 11-18-2003 12:45 AM

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');
                }
        }
}


timmy334 11-18-2003 12:47 AM

Just a side question: What's the syntax to use the nanosecond function in there?

LogicG8 11-18-2003 11:40 AM

#include <time.h>

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


All times are GMT -5. The time now is 09:21 AM.