High CPU usage in Linux Daemon (using TDaemon) lazarus / 5-Lazarus Extra Components ---------------------------------------------------------------------- jl · 2009-07-14 08:37 I created a very simple Daemon and discovered that it takes up up to half the CPU resources when doing nothing.  The problem does not occur in win32. I've attached the source and screen capture, simpledaemon- screenshot.JPG. Is there a command to make the daemon go to a sleeping or idle mode? Appreciate any pointers. ---------------------------------------------------------------------- Loesje · 2009-07-14 20:54 It's a bug in fpPause. For some reason it returns immediately. But that should be no problem for you. Your service should not exit the OnExecute  procedure, until the service is terminated. Now your code is terminated but the system doesn't know that so it waits forever.... ie: procedure TDaemon1.DataModuleExecute(Sender: TCustomDaemon); begin   while status=csrunning do     begin     sleep(50);     end; end; btw: using 'writeln' in a daemon is not a good idea. Maybe that Application.Logger.debug('sometext'); is better. ---------------------------------------------------------------------- jl · 2009-07-15 09:37 Quote from: Loesje on July 14, 2009, 10:54:28 pmIt's a bug in fpPause. For some reason it returns immediately. But that should be no problem for you. Your service should not exit the OnExecute  procedure, until the service is terminated. Now your code is terminated but the system doesn't know that so it waits forever.... ie: procedure TDaemon1.DataModuleExecute(Sender: TCustomDaemon); begin   while status=csrunning do     begin     sleep(50);     end; end; btw: using 'writeln' in a daemon is not a good idea. Maybe that Application.Logger.debug('sometext'); is better. Thanks, this worked perfect.  I just discovered that the Ubuntu version of Application.Logger rocks - the viewer scrolls real-time! By the way, I noticed that the daemon waiting channel is always at 'futex_wait' while the majority of the rest like Gnome-panel are at 'do_poll'.  Is that suppose to be correct? ---------------------------------------------------------------------- jl · 2009-08-07 04:25 Thanks.  By the way, I found out that if I don't use TDaemon1.DataModuleExecute, I won't have this problem in the first place.  I've moved the startup code to TDaemon1.DataModuleStart. Meanwhile, I'm facing a new problem.  I'm not able to quit a service startup (both Win32 and Linux) - I want to do that if say the UDP port is occupied when starting my UDP Server: Code: [Select]procedure TDaemon1.DataModuleStart(Sender: TCustomDaemon; var OK: boolean);  while IdUDPServer1.Active = False do   begin     try       IdUDPServer1.Active := True;          //binding         ..............     except       on E: Exception do       begin         writelog('error','Main', E.ClassName + #9 +           'Errors raised when starting server with message :' + E.message);         writelog('error','Main','Encountered fatal error.  Exiting.');;         sleep(5000);      //     OK:=false;     //  exit;       end;     end;   end;     The following code was added but the service will become hang - not started and not stopped, even after 5 minutes.  I have to kill it from the process, so this is no go.        //     OK:=false;     //  exit;