View Full Version : sleep the process
julius.luo
09-26-2006, 04:39 PM
Hi
I am programming a program to do broadcasting, and I want to do broadcast with time
interval. The first C function comes to my mind is using sleep(), but when I put this
into loop, the program just suspended. Is this because the signals come too rapidly
making the process impossible to handle?
Beside sleep(), are they any other function that I can use?
Thanks
Julius
julius.luo
09-26-2006, 05:08 PM
oh, I shall add more about my question. Actually my requirment is to display current
broken-down time on the screen, and in the loop I also have to plus a random number
on the time. The problem seems in running time function, because when I remove them,
there is no problem at all.
i3839
09-26-2006, 05:56 PM
It's quite fuzzy what your problem is, please post your code so we can see what's wrong with it.
RobSeace
09-26-2006, 06:22 PM
Yeah, I'm not sure exactly what you're saying, either...
However, if you want alternatives to sleep(), there are various ones: usleep(),
nanosleep(), and select() with empty fd_sets... Or, if you wanted to do something
else while waiting, you could use alarm() or setitimer() and a signal handler...
By an chance is your code multi-threaded? If so, you probably don't want to be
using sleep(), since it might be implemented with signals... I think you want either
nanosleep() or select(), in that case... (In practice, on glibc, I think usleep() is also
thread-safe, since it just calls nanosleep(), but you probably don't want to rely on it...)
But, yeah, like i3839 says, just posting your problematic code is probably the best
approach, so we can really see what's going on... (Yet, I felt it necessary to post
four paragraphs of babble to basically say the same thing he said in one sentence! ;-))
i3839
09-26-2006, 11:00 PM
I tend to be terse, you tend to ramble. I think people prefer your babbling as it always has some useful info even if it doesn't solve their current problem. ;-)
julius.luo
09-27-2006, 05:47 AM
Thanks for quick response, guys. Actually my code is quite simple, as below:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int interval;
srand(time(NULL));
for(;;)
{
interval = rand()%10;
printf("interval is %i", interval);
sleep(interval);
}
return 0;
}
This question looks silly, but I just don`t know what is going on here. When I put sleep()
into loop, it just not works.
julius.luo
09-27-2006, 06:12 AM
oh, thanks RobSeace. I AM planning to make it work with multithreading. More info
is always helpful for the beginner like me to catch up the knowledge fast ;)
i3839
09-27-2006, 10:35 AM
printf is line buffered, just add a '\n' to the string you print and it will "work".
julius.luo
09-27-2006, 11:12 AM
Thanks i3839, the stream is printed, but why the newline have to be provided here? Is it
because in the loop that newline character doesn`t appear, so the stream will not end and
print, but when I put sleep() and printf() out of the loop, the stream can be printed.
julius.luo
09-27-2006, 11:45 AM
oh, I have found out the answer. Two ways to show my string, one is like i3839 said,
puting newline at the end. Another is to fflush() the buffer.
I find people here respond my question very fast, even faster than the newsgroup in
my school ;) thanks
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.