Google Groups không còn hỗ trợ đăng ký sử dụng hoặc đăng nội dung mới trên Usenet. Bạn vẫn có thể xem nội dung cũ.

Sleep and Alarm

0 lượt xem
Chuyển tới thư đầu tiên chưa đọc

mike_...@my-dejanews.com

chưa đọc,
03:00:00 19 thg 1, 199919/1/99
đến
I have a question thats been nagging me for a while now. I have a program
which requires the use of sleep and alarm. The problem rests mainly with the
fact that sleep uses SIGALRM. But I need both sleep and alarm; is there
a replacement for sleep which does not use alarm; or is there another solution
to the ever present problem of sleep and alarm.

ttfn,
Mike

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

mike_...@my-dejanews.com

chưa đọc,
03:00:00 19 thg 1, 199919/1/99
đến
I have a question thats been nagging me for a while now. I have a program
which requires the use of sleep and alarm. The problem rests mainly with the
fact that sleep uses SIGALRM. But I need both sleep and alarm; is there
a replacement for sleep which does not use alarm; or is there another solution
to the ever present problem of sleep and alarm.

ttfn,
Mike

Reply to ve...@home.com or post. Thank you.

Barry Margolin

chưa đọc,
03:00:00 19 thg 1, 199919/1/99
đến
In article <782nej$gno$1...@nnrp1.dejanews.com>,

<mike_...@my-dejanews.com> wrote:
>I have a question thats been nagging me for a while now. I have a program
>which requires the use of sleep and alarm. The problem rests mainly with the
>fact that sleep uses SIGALRM. But I need both sleep and alarm; is there
>a replacement for sleep which does not use alarm; or is there another solution
>to the ever present problem of sleep and alarm.

When you're about to call sleep(), do the following: If there's an alarm
pending, and it's schedule to go off before the sleep would finish, only
sleep until the time the alarm would have gone off. If it's scheduled to
go off after, save away the alarm time; when the sleep finishes, call
alarm() again.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.

You Shung-I

chưa đọc,
03:00:00 20 thg 1, 199920/1/99
đến
mike_...@my-dejanews.com 提到:
: I have a question thats been nagging me for a while now. I have a program

: which requires the use of sleep and alarm. The problem rests mainly with the
: fact that sleep uses SIGALRM. But I need both sleep and alarm; is there
: a replacement for sleep which does not use alarm; or is there another solution
: to the ever present problem of sleep and alarm.

You can use select.
void my_sleep(int sec)
{
struct timeval tim;
tim.tv_sec = sec;
tim.tv_usec = 0;
select(0,NULL,NULL,NULL,&tim);
}

But the my_sleep() blocking will return if the signal happen.

--
=================================================
游順益: 天蠍座
E-mail dan...@info4.csie.nctu.edu.tw
-------------------------------------------------
羔羊無己,獅子無懼
=================================================

r...@trixy.demon.co.uk

chưa đọc,
03:00:00 21 thg 1, 199921/1/99
đến
In article <782nds$gng$1...@nnrp1.dejanews.com>,

mike_...@my-dejanews.com wrote:
> I have a question thats been nagging me for a while now. I have a program
> which requires the use of sleep and alarm. The problem rests mainly with the
> fact that sleep uses SIGALRM. But I need both sleep and alarm; is there
> a replacement for sleep which does not use alarm; or is there another solution
> to the ever present problem of sleep and alarm.
>
> ttfn,
> Mike

>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
>

If during a sleep() your previously set alarm() times out, then
your handler will be called as you expect but the sleep() will
then return prematurely. Is that the problem?
Remember sleep() returns the number of unslept seconds in this case.

If you want the sleep() to last the full time,
then you can use something like:

void deepsleep(int n)
{
long target=time(NULL)+n; /* you might want n+1 here */
while (time(NULL)<target)
n=sleep(n);
}

In this case your alarm handler could set a "signalled" variable
and check that after the deepsleep()

Also, sleep() is clever enough to restore your alarm call
(in the case of your alarm() lasting longer than the sleep) so
as to take account of the time slept.
so:
alarm(7);
sleep(3);
/* your alarm(7) now has 4 secs to run */

Other things to look at are itimers and select() with a timeout.

hope this is of some use
Rob

Nate Eldredge

chưa đọc,
03:00:00 22 thg 1, 199922/1/99
đến
r...@trixy.demon.co.uk wrote:
>
> In article <782nds$gng$1...@nnrp1.dejanews.com>,
> mike_...@my-dejanews.com wrote:
> > I have a question thats been nagging me for a while now. I have a program
> > which requires the use of sleep and alarm. The problem rests mainly with the
> > fact that sleep uses SIGALRM. But I need both sleep and alarm; is there
> > a replacement for sleep which does not use alarm; or is there another solution
> > to the ever present problem of sleep and alarm.
> >
> > ttfn,
> > Mike
> >
> > -----------== Posted via Deja News, The Discussion Network ==----------
> > http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
> >
>
> If during a sleep() your previously set alarm() times out, then
> your handler will be called as you expect but the sleep() will
> then return prematurely. Is that the problem?

I think what he means is that some (old) systems implement `sleep'
something like

void nop(int unused) { }

int sleep(int secs) {
void (*old)(int);
old = signal(SIGALRM, nop);
alarm(secs);
pause();
signal(SIGALRM, old);
}

Obviously that would present a problem if you wanted to use SIGALRM.

Actually, I wonder if the poster's system is such. Probably most modern
Unices don't. (Linux I know doesn't.)

--

Nate Eldredge
na...@cartsys.com

0 tin nhắn mới