public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* How efficient is 'sleep'?
@ 2023-12-15 21:55 Backwoods BC
  2023-12-15 22:27 ` Brian Inglis
  2023-12-15 23:46 ` Eliot Moss
  0 siblings, 2 replies; 5+ messages in thread
From: Backwoods BC @ 2023-12-15 21:55 UTC (permalink / raw)
  To: cygwin

I have quite a few "service-like" scripts that I put into the
background and then have them wake up on a regular basis to do
something. I use 'sleep' for the timing of the wakeup periods.

My question is: How efficient is 'sleep'? I know of other OSes that
just set a timer flag and the process isn't allocated any CPU time
until the timer expires.

I know that creating a service or even using Task Scheduler are more
"proper" ways of doing this, but they are also much more work and
would require a significant learning curve as my background is
embedded systems, not Windows. I know that my lazy way probably has a
penalty, but just how bad is it?

Thanks

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How efficient is 'sleep'?
  2023-12-15 21:55 How efficient is 'sleep'? Backwoods BC
@ 2023-12-15 22:27 ` Brian Inglis
  2023-12-15 23:46 ` Eliot Moss
  1 sibling, 0 replies; 5+ messages in thread
From: Brian Inglis @ 2023-12-15 22:27 UTC (permalink / raw)
  To: cygwin

On 2023-12-15 14:55, Backwoods BC via Cygwin wrote:
> I have quite a few "service-like" scripts that I put into the
> background and then have them wake up on a regular basis to do
> something. I use 'sleep' for the timing of the wakeup periods.
> 
> My question is: How efficient is 'sleep'? I know of other OSes that
> just set a timer flag and the process isn't allocated any CPU time
> until the timer expires.
> 
> I know that creating a service or even using Task Scheduler are more
> "proper" ways of doing this, but they are also much more work and
> would require a significant learning curve as my background is
> embedded systems, not Windows. I know that my lazy way probably has a
> penalty, but just how bad is it?

Install Cygwin packages cron plus cygrunsrv to run daemons as services.
Setup cron service by running /usr/sbin/cron-config as elevated admin.
Then use crontab to schedule scripts every so often or at specific times or days.
Install package and read man pages or see the current package repo:

	https://github.com/vixie/cron/blob/master/crontab.5

-- 
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retirer     but when there is no more to cut
                                 -- Antoine de Saint-Exupéry


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How efficient is 'sleep'?
  2023-12-15 21:55 How efficient is 'sleep'? Backwoods BC
  2023-12-15 22:27 ` Brian Inglis
@ 2023-12-15 23:46 ` Eliot Moss
  2023-12-16 17:36   ` [EXTERNAL] " Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  1 sibling, 1 reply; 5+ messages in thread
From: Eliot Moss @ 2023-12-15 23:46 UTC (permalink / raw)
  To: Backwoods BC, cygwin

On 12/15/2023 4:55 PM, Backwoods BC via Cygwin wrote:
> I have quite a few "service-like" scripts that I put into the
> background and then have them wake up on a regular basis to do
> something. I use 'sleep' for the timing of the wakeup periods.
> 
> My question is: How efficient is 'sleep'? I know of other OSes that
> just set a timer flag and the process isn't allocated any CPU time
> until the timer expires.
> 
> I know that creating a service or even using Task Scheduler are more
> "proper" ways of doing this, but they are also much more work and
> would require a significant learning curve as my background is
> embedded systems, not Windows. I know that my lazy way probably has a
> penalty, but just how bad is it?
> 
> Thanks

You could strace on it to see what system calls it makes, but I am
pretty sure it sets up a timer interrupt and then waits for an event.
Very efficient.  It does not, for example, continually read the clock
until the requested amount of time has passed.

The more interesting question is this.  If you coded something like:

while true; do x; sleep 60; done

are you ok with x starting more than 60 seconds apart, because the time
to execute x itself, to fork and wait for the processes, etc., gets added
into each iteration?  If you prefer the invocations of x start at the start
of each minute, then you need to calculate, on each iteration, how long to
sleep.  There's loads of info on this stackoverflow page:

https://stackoverflow.com/questions/645992/sleep-until-a-specific-time-date

Cheers - Eliot Moss

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [EXTERNAL] Re: How efficient is 'sleep'?
  2023-12-15 23:46 ` Eliot Moss
@ 2023-12-16 17:36   ` Lavrentiev, Anton (NIH/NLM/NCBI) [C]
  2023-12-16 19:06     ` Backwoods BC
  0 siblings, 1 reply; 5+ messages in thread
From: Lavrentiev, Anton (NIH/NLM/NCBI) [C] @ 2023-12-16 17:36 UTC (permalink / raw)
  To: Backwoods BC, cygwin

> the process isn't allocated any CPU time until the timer expires.

Almost so.  But the "sleep" functions are interruptible, so if a process (the "sleep" command)
is somehow signaled, it will wake up prematurely, and will have to either put itself back to
sleep (for the remaining unslept time) or terminate, whatever the implementation is.  So in the
former case, there is some CPU still consumed (and that would depend on how often the signal
arrives), and in the former case, the actual slept time can be quite inaccurate (from what you
think it should have been).

Signaling with scripts can be quite tricky as the signal can propagate to the entire process
group, rather than a single process (depending on which process the signal was sent to).

Using cron (as others suggested) gives you a time accuracy up to a second (give or take), but
then again it depends on the load of the system, and may drift rather significantly.

My $.02,

Anton Lavrentiev
Contractor NIH/NLM/NCBI


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [EXTERNAL] Re: How efficient is 'sleep'?
  2023-12-16 17:36   ` [EXTERNAL] " Lavrentiev, Anton (NIH/NLM/NCBI) [C]
@ 2023-12-16 19:06     ` Backwoods BC
  0 siblings, 0 replies; 5+ messages in thread
From: Backwoods BC @ 2023-12-16 19:06 UTC (permalink / raw)
  Cc: cygwin

On Sat, Dec 16, 2023 at 9:37 AM Lavrentiev, Anton (NIH/NLM/NCBI) [C]
<lavr@ncbi.nlm.nih.gov> wrote:
>
> > the process isn't allocated any CPU time until the timer expires.
>
> Almost so.  But the "sleep" functions are interruptible, so if a process (the "sleep" command)
> is somehow signaled, it will wake up prematurely, and will have to either put itself back to
> sleep (for the remaining unslept time) or terminate, whatever the implementation is.  So in the
> former case, there is some CPU still consumed (and that would depend on how often the signal
> arrives), and in the former case, the actual slept time can be quite inaccurate (from what you
> think it should have been).
>
> Signaling with scripts can be quite tricky as the signal can propagate to the entire process
> group, rather than a single process (depending on which process the signal was sent to).
>
> Using cron (as others suggested) gives you a time accuracy up to a second (give or take), but
> then again it depends on the load of the system, and may drift rather significantly.
>
> My $.02,
>
> Anton Lavrentiev
> Contractor NIH/NLM/NCBI

Thank you for the answers. This is a personal computer and nothing my
"services" do cares much about the actual time between runs so the
uncertainty using 'sleep' isn't an issue. Back in the XP days, I used
a Windows version of 'cron' to run things, but it stopped working and
I just modified the small number of scripts it launched to time
themselves.

I was just curious as to how much of a penalty I was paying for my
laziness and you've confirmed that the answer is "not much." My
computer has been slowing down and I was looking for the culprit,
which is most likely just increasing software bloat (wheezy voice: "In
my day, 4 kB was a huge program:)

My current computer is not "Win 11 ready" and I'm not replacing
perfectly functional hardware, so I will be migrating to a Linux
desktop in 2025 (or sooner) and will have 'cron' available again.

Again, thanks for the answers.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-16 19:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-15 21:55 How efficient is 'sleep'? Backwoods BC
2023-12-15 22:27 ` Brian Inglis
2023-12-15 23:46 ` Eliot Moss
2023-12-16 17:36   ` [EXTERNAL] " Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2023-12-16 19:06     ` Backwoods BC

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).