ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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');
}
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.