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

--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to