Symbian Developer Network

   
 

Need help using the forums?
Click here

Home » Symbian Developer Network Forums » Discussion Groups » Symbian General

Thread: ActiveObject and Simple Application Don't Run Forever

Legend
  • Five stars: 251 - 10000 pts
  • Four stars: 101 - 250 pts
  • Three stars: 51 - 100 pts
  • Two stars: 11 - 50 pts
  • One star: 1 - 10 pts
  • No stars: 0 - 0 pts
Helpful Answer (1 pts)
Correct Answer (2 pts)
This question is answered.


Permlink Replies: 18 - Pages: 2 [ 1 2 | Next ] - Last Post: 03-Jun-2008 09:34 Last Post By: diidu
kball

Posts: 33
Registered: 16/11/07
ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 11:27
 
Click to report abuse...   Click to reply to this thread Reply
Have written a Watchdog application to kill and restart another application I am running an N95. While the Watchdog is simply a console application with a checking ActiveObject, the Watchdog stops working at some unknown point in the future. After about 1 hour the Watchdog just dies. I'm fairly certain the code is good (no memory leaks). The Watchdog is super simple.

Q. Is there any way to make sure that a Process is run continuously and that Symbian OS does not affect the Process thread in any way i.e. shut it down at its own initiative.

Q. Am I seeing this correctly? The OS seems to be shakey when running applications in background for extended periods of time.
jnm

Posts: 138
Registered: 12/02/08
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 13:20   in response to: kball in response to: kball
Helpful
Click to report abuse...   Click to reply to this thread Reply
Hi RudikBall,

Can you provide more explanation please?
to write "The Watchdog is super simple." doesn't help. If you use an active object, you can surly tell us why "the Watchdog just dies". does it stop working? Do you see the same behaviour on emulator?

A1) I'm not aware of this, but I don't see the OS "shut it down at its own initiative"

A2) you can run your phone for days, and this OS will run flawlessly. However, if an application has a memory leak, it's a different matter.

Regards,
Jean-Noël
kball

Posts: 33
Registered: 16/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 13:44   in response to: jnm in response to: jnm
 
Click to report abuse...   Click to reply to this thread Reply
The Active Object Code

#include "ProcessWatcher.h"
#include

extern RFileLogger *log;

CProcessWatcher::CProcessWatcher() :
CActive(EPriorityStandard) // Standard priority
{
}

CProcessWatcher* CProcessWatcher::NewLC()
{
CProcessWatcher* self = new ( ELeave ) CProcessWatcher();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}

CProcessWatcher* CProcessWatcher::NewL()
{
CProcessWatcher* self = CProcessWatcher::NewLC();
CleanupStack::Pop(); // self;
return self;
}

void CProcessWatcher::ConstructL()
{
User::LeaveIfError(iTimer.CreateLocal() ); // Initialize timer
CActiveScheduler::Add( this); // Add to scheduler
}

CProcessWatcher::~CProcessWatcher()
{
Cancel(); // Cancel any request, if outstanding
iTimer.Close(); // Destroy the RTimer object
iProcess.Close(); //Close the Process
// Delete instance variables if any
}

void CProcessWatcher::DoCancel()
{
iTimer.Cancel();
}

void CProcessWatcher::StartL(TTimeIntervalMicroSeconds32 aDelay)
{
Cancel(); // Cancel any request, just to be sure
iState = EUninitialized;
iTimer.After(iStatus, aDelay); // Set for later
SetActive(); // Tell scheduler a request is active
}

void CProcessWatcher::RunL()
{
if (iState == EUninitialized)
{
// Do something the first time RunL() is called
iState = EInitialized;
CheckAndRun();
}
else
{
if (iState != EError)
{
CheckAndRun();
}
}
iTimer.After(iStatus, 300000000); // Do kill every hour
SetActive(); // Tell scheduler a request is active
}

TInt CProcessWatcher::RunError(TInt aError)
{
return aError;
}

TBool CProcessWatcher::IsApplicationRunning(void)
{
TBool Ret(EFalse);

TFileName res;
TFindProcess find;

while(find.Next(res) == KErrNone)
{
RProcess ph;
ph.Open(res);

if(ph.SecureId() == 0x00000013)
{
Ret = ETrue;
break;
}

ph.Close();
}

return Ret;
}

void CProcessWatcher::CheckAndRun()
{

//Check if Application is Running
TBool running = IsApplicationRunning();

log->WriteFormat(_L("Iteration = %d"), iIteration);

if (running)
{
log->Write(_L("CProcessWatcher::RunL:Application is ON..."));

if (iIteration >= 12)
{

//Application is Running
iProcess.Kill(0);
log->Write(_L("CProcessWatcher::RunL:Application KILLED..."));
//Attempt to Start Application
iProcess.Create(KApplicationExeFile, KNullDesC);
iProcess.Resume();
log->Write(_L("CProcessWatcher::RunL:Re-starting Application..."));

iIteration = 0; //reset the counter
}

}
else
{
//Application is not Running
log->Write(_L("CProcessWatcher::RunL:Application is OFF..."));
//Attempt to Start Application
iProcess.Create(KApplicationExeFile, KNullDesC);
iProcess.Resume();
log->Write(_L("CProcessWatcher::RunL:Ran Application Executable"));
iIteration = 0; //reset the counter

}

iIteration++; //Increment the iteration

}

TInt CProcessWatcher::whatIsBatteryLevel()
{
TInt level = 0;

//To be completed.

return level;
}

tanzimhusain

Posts: 270
Registered: 23/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 14:55   in response to: kball in response to: kball
 
Click to report abuse...   Click to reply to this thread Reply
Hi ~rudikball,

A brief glance at your code reveals that you are using RTimer as a scheduler for your process kill/restart cycle. Guess what? Using RTimer isn't exactly a robust solution for this.

First of all, the time can easily complete when the system time changes, so you might get baffled with the results (it doesn't help if you don't check iStatus.Int() in RunL...). Secondly, timers are inherintly evil. Let me repeat, timers are inherintly evil, since they use polling and that means draining the battery! See this for a guide to timer usage and do's and dont's.

Secondly, your code for the process management could a lot more efficient than it is right now. What's the point of searching for your process? You are the creator, so you can use RProcess::Open directly anyway...
kball

Posts: 33
Registered: 16/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 15:18   in response to: tanzimhusain in response to: tanzimhusain
 
Click to report abuse...   Click to reply to this thread Reply
MaxPayne

So what would you use as a solution other than a Timer? What is the alternative? I need a timed polling check.
kball

Posts: 33
Registered: 16/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 15:21   in response to: tanzimhusain in response to: tanzimhusain
 
Click to report abuse...   Click to reply to this thread Reply
Need to check that the process is running every 5 minutes. If the code isn't running I restart the process. Every 60 minutes I kill and restart the process.
kball

Posts: 33
Registered: 16/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 15:23   in response to: tanzimhusain in response to: tanzimhusain
 
Click to report abuse...   Click to reply to this thread Reply
Thought it was dangerous to use Synchronous Timer (User::After).
tanzimhusain

Posts: 270
Registered: 23/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 16:11   in response to: kball in response to: kball
 
Click to report abuse...   Click to reply to this thread Reply
Hi ~rudikball,

Have a look at the discussion thread https://developer.symbian.com/forum/thread.jspa?threadID=25656&tstart=0.
Its really silly that you'd want to check every 5 mintes that you're target process is running or not, when you have mechanisms like RProcess::Logon or even the more sophisticated RUndertaker at your service. If you actually read the links I posted earlier to your original process related posting, you'd have come across them by now!
kball

Posts: 33
Registered: 16/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 16:40   in response to: tanzimhusain in response to: tanzimhusain
 
Click to report abuse...   Click to reply to this thread Reply
Thanks for the positive help Max. What's so silly about checking that a Process is running every 5 minutes?
tanzimhusain

Posts: 270
Registered: 23/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 29-May-2008 16:43   in response to: kball in response to: kball
Helpful
Click to report abuse...   Click to reply to this thread Reply
Hi ~rudikball,

What's so silly about checking that a Process is running every 5 minutes?

Polling is an inherintly bad thing, specially in a power constrained device like a mobile phone. Why use polling when you have a first class notification mechanism at hand?

Cheers.
hamishw

Posts: 2,286
Registered: 11/10/06
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 30-May-2008 08:30   in response to: kball in response to: kball
Correct
Click to report abuse...   Click to reply to this thread Reply
Hi Rudikball,

The reason that maxpayne says that polling is evil, is because you need to run your polling code on a timer even if the watched process is in a good state.
On mains power that (arguably) might be fine, but on a resource contrained device you're consuming clock cycles you don't need to (draining the battery). Even worse, you're potentially waking up the phone from a low power mode.

Since phone manufacturer standby times are dependent on the phone really being idle, you may be reducing the amount of time between recharges to a noticeable level.

Instead, you can use an undertaker (RUndertaker) to notify your code when the process is dead and that it shoudl run. Then your code is quiescent unless needed.

Regards
Hamish Willee
Symbian Ltd.
kball

Posts: 33
Registered: 16/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 30-May-2008 11:17   in response to: hamishw in response to: hamishw
 
Click to report abuse...   Click to reply to this thread Reply
Hi Hamish,

Thanks for the reply.

Certainly the problem we're having is making sure our Application is running indefinitely. The application code is made up of many timers and complexities, which are run in background.

I'm concerned that the Timers are not trustworthy enough to make certain an application is running. In effect is Symbian saying that once you start a looping application we cannot be sure it will loop forever? Hence this is not like Linux or any other normal computer operating system.

Do you have any good recommended links (examples of) the RUndertaker? It is something I've never heard of.

Regards

Rudi

simonm

Posts: 22
Registered: 25/01/08
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 30-May-2008 11:36   in response to: kball in response to: kball
 
Click to report abuse...   Click to reply to this thread Reply
Hi rudikball,

The timers are trustworthy, they cannot be the reason your application is failing. Please stop saying it is because of Symbian OS - it isn't. Many other applications run ok - the phone application for example - if that didn't run for a long time, it wouldn't be much use as a phone?

The reason people are saying not to use RTimer is because for what you want to do, it wastes battery power.

By using RTimer, you are forcing the OS to keep checking a counter value until your time limit is hit and this forces the CPU to stay in a high-power state, using more of the battery.

If you use RUndertaker (or RProcess::Logon), your watchdog app will go to sleep (i.e. stop running) until it is triggered (by the other application stopping). By going to sleep, it means the CPU can go into a low-power state and save battery.

I hope that makes it clearer?

The main problem though is your main application stopping running. It is not a failure of the OS. It is a defect in the application and you need to debug it to find out what the problem is.
kball

Posts: 33
Registered: 16/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 30-May-2008 11:42   in response to: simonm in response to: simonm
 
Click to report abuse...   Click to reply to this thread Reply
Hi Simon

What I don't understand is surely, that if your application is correctly coded one would expect certain behavior. If one writes a simple Hello World application which uses the Timer, the application does not seem to run indefinitely. I suggest you try it.

Regards

tanzimhusain

Posts: 270
Registered: 23/11/07
Re: ActiveObject and Simple Application Don't Run Forever
Posted: 30-May-2008 12:22   in response to: kball in response to: kball
 
Click to report abuse...   Click to reply to this thread Reply
Hi ~rudikball,

This will be my last post in this thread, since its already become bit of a drag.

Firsty, READ THE DOCUMENTATION! If you actually did, you'd see that RTimer::After uses TTimeIntervalMicroSeconds32, which has the following to say in the docs:

Description


Represents a microsecond time interval stored in 32 rather than 64 bits.

Its range is +-2147483647, which is +-35 minutes, 47 seconds.

And then there's your code (which I still bet hasn't been changed to take account of error checking I mentioned earlier and also time changes due to system time change):


iTimer.After(iStatus, 300000000); // Do kill every hour

O yeah... also if my high school maths is correct, one hour translated to microseconds is 3600000000.

Anyway, blaming the OS isn't going to take you anywhere if you don't know how to use the APIs and can't be bothered to read the documentation AND the recommended solution for your scenario.

Hope that helps.


Point your RSS reader here for a feed of the latest messages in all forums