Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-03

Solution for Timer in daemon

9 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Krzysztof

Hi,

I'm reading that I can't use timer in daemon because daemon core is based on thread. So I'm trying to create another thread which simulate timer. My interval is quite big (~1-5 minutes), so I can't just use sleep(60000) because daemon will hung on terminate. So I have two ideas:

1. Create loop with short sleep(1000) which on each loop check if main interval occur and check if daemon is terminated
2. Create loop with RTL event with RtlEventWaitFor(Event, 60000) and daemon on terminate just send event to worker so it immediately exit.

What is the best efficient solution? Maybe exists another way?

Regards.

Leonardo M. Ramé
to Krzysztof
Show quoted text 21 lines
________________________________ From: Krzysztof To: fpc-pascal Sent: Friday, March 1, 2013 3:10 PM Subject: [fpc-pascal] Solution for Timer in daemon Hi, I'm reading that I can't use timer in daemon because daemon core is based on thread. So I'm trying to create another thread which simulate timer. My interval is quite big (~1-5 minutes), so I can't just use sleep(60000) because daemon will hung on terminate. So I have two ideas: 1. Create loop with short sleep(1000) which on each loop check if main interval occur and check if daemon is terminated 2. Create loop with RTL event with RtlEventWaitFor(Event, 60000) and daemon on terminate just send event to worker so it immediately exit. What is the best efficient solution? Maybe exists another way? Regards.

Well, as far as I know, sleep is no thread safe, it will block your app.

Leonardo M. Ramé
http://leonardorame.blogspot.com

Sven Barth
to Leonardo M. Ramé
Leonardo M. Ramé wrote 25 lines
> ________________________________ > From: Krzysztof > To: fpc-pascal > Sent: Friday, March 1, 2013 3:10 PM > Subject: [fpc-pascal] Solution for Timer in daemon > > > Hi, > > > I'm reading that I can't use timer in daemon because daemon core is based on thread. So I'm trying to create another thread which simulate timer. My interval is quite big (~1-5 minutes), so I can't just use sleep(60000) because daemon will hung on terminate. So I have two ideas: > > > 1. Create loop with short sleep(1000) which on each loop check if main interval occur and check if daemon is terminated > 2. Create loop with RTL event with RtlEventWaitFor(Event, 60000) and daemon on terminate just send event to worker so it immediately exit. > > > What is the best efficient solution? Maybe exists another way? > > > Regards. Well, as far as I know, sleep is no thread safe, it will block your app.

Sleep only blocks the current thread.

Regards,
Sven

Sven Barth
to Krzysztof
Krzysztof wrote
I know how sleep blocking works in thread :) . I'm just wondering what is better, sleep or RTL event or something else

You might know, but we answered to Leonardo who does not seem to know

Ludo Brands
to Krzysztof: I'm reading that I can't use timer in daemon because daemon core is based on thread. So…
Krzysztof wrote
Hi, I'm reading that I can't use timer in daemon because daemon core is based on thread.

daemonapp is using threads but nothing stops you from daemonizing your app yourself with a simple fork.

Show quoted text 11 lines
So I'm trying to create another thread which simulate timer. My interval is quite big (~1-5 minutes), so I can't just use sleep(60000) because daemon will hung on terminate. So I have two ideas: 1. Create loop with short sleep(1000) which on each loop check if main interval occur and check if daemon is terminated 2. Create loop with RTL event with RtlEventWaitFor(Event, 60000) and daemon on terminate just send event to worker so it immediately exit. What is the best efficient solution? Maybe exists another way?

Very difficult to give an absolute answer without knowing what the daemon is doing.

On unix, an alternate solution is to use fpSelect (Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint; ) and specify a time-out value. You can specify a signal with exceptfds or a file descriptor with readfds/writefds that will trigger the return of select. I have written a few daemons that are stopped by sending them a signal. fpSelect is then a natural solution. If your daemon is already receiving control data from the outside through sockets, FIFO's or whatever file, simply attach the select to the file descriptor of that channel.

Note that the time precision of loops using sleep is not fptime to decide when to trigger the task. If a precision higher than 1000ms is needed, nothing stops you from modulating the sleep time based on fptime so that the last sleep expires at the time wanted.

So the best solution really depends on what else you are doing in the daemon.

Ludo

Michael Van Canneyt
to Ludo Brands
Ludo Brands wrote 8 lines
On 03/01/2013 07:10 PM, Krzysztof wrote: > Hi, > > I'm reading that I can't use timer in daemon because daemon core is > based on thread. daemonapp is using threads but nothing stops you from daemonizing your app yourself with a simple fork.

The threads are not there for daemonizing.
They are there because a single app can host multiple services (daemons) as on Windows.

Show quoted text 18 lines
> So I'm trying to create another thread which simulate > timer. My interval is quite big (~1-5 minutes), so I can't just use > sleep(60000) because daemon will hung on terminate. So I have two ideas: > > 1. Create loop with short sleep(1000) which on each loop check if main > interval occur and check if daemon is terminated > 2. Create loop with RTL event with RtlEventWaitFor(Event, 60000) and > daemon on terminate just send event to worker so it immediately exit. > > What is the best efficient solution? Maybe exists another way? > Very difficult to give an absolute answer without knowing what the daemon is doing. On unix, an alternate solution is to use fpSelect (Function fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint; ) and specify a time-out value.

I would go for this.

Michael.

Mark Morgan Lloyd
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
> On unix, an alternate solution is to use fpSelect (Function > fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint; > ) and specify a time-out value. I would go for this.

With the very minor caveat that different variants of fpSelect() hide the OS-specific behavior (whether the time parameter is updated on return) of the underlying select() call to a varying extent. It's particularly worth checking code that relies on this on both Linux and Solaris.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]