Apparently, you can release cpu cycles, but it's with the sched_yield (section 2 in the man pages), not the sleep command on linux.

So, apparently, the sleep is still using full system recourses, even though it's just waiting.  You apparently have to release cpu time with the sched_yield command to make it actually give up time slices., and even then, it's not clear it will actually do so, the description claims it simply moves your application to the end of the cue, which doesn't really give away it's cpu cycles, just changes it's location in the execution order.

Perhaps it will help with the cpu usage though, I've not done any testing to see how (or not) it affects cpu.


On 5/18/2021 6:29 PM, Bo Berglund via fpc-pascal wrote:
On Tue, 18 May 2021 21:40:15 +0000, Alexander Grotewohl via fpc-pascal
<fpc-pascal@lists.freepascal.org> wrote:

if it's waiting on keyboard input you might be better off using select() for
that instead of looping and checking for keyboard input each go around.

sleep() might already do something similar with a zero timeout but even that
would probably be insufficient
As I tried to explain at the start of this thread my application runs as a
*systemd service* on Linux, so there is absolutely no keyboard input.

The main program looks like this after initializations etc:

   try
     try
       bSTerm := False;
       bSInt := False;
       bsHup := False;
       {$IFDEF UNIX}
       fpSignal(SigTerm, SignalHandler(@handleSignal));
       fpSignal(SigInt, SignalHandler(@handleSignal));
       fpSignal(SigHup, SignalHandler(@handleSignal));
       {$ENDIF}

       Debug_Writeln('Enter eternal loop');
       FLogServ.StdLog('Enter eternal loop');

       While not (bSTerm or bSInt or bsHup) do
       begin
         //Here is where the server runs as defined elsewhere
         //Eternal loop to wait for system messages
         Sleep(1); //To not hog the CPU
         CheckSynchronize; //To get thread comm working
       end;

       Debug_Writeln('Exit eternal loop');
       FLogServ.StdLog('Exit eternal loop');
     except
       on E: Exception do
       begin
         FLogServ.ExceptionLog('Unhandled exception: ' + E.Message);
       end;
     end;
   finally
     ShutdownServer;
   end;

As you can see it sits in the eternal loop until the system sends a "signal" to
it to stop. So systemd does that if I issue a sudo systemctl stop myservice and
this works fine.
Could this tiny loop consume 6-7% CPU cycles?


_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to