Forum Index
Task scheduler issue?

 
Author Message
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 19 September 2009, 19:41 PM    Post subject: Task scheduler issue?

It looks like the VM (v2.7 in a ZX-24a) eats a tick when:
- Main is allowed to terminate and
- a single remaining running task issues Sleep(0).

Excepting VM overhead, shouldn't it return control to the sole remaining task immediately?

The following code demonstrates. BackgroundTask simply spins and counts as fast as it can for awhile, then displays the normalized ratio of the resulting count and actual elapsed time. I use it in projects to reveal how much processor time is available.

As a demo here, Main() also simply spins, issuing a Sleep(0) in each iteration to relinquish control to BackgroundTask, which is tweaked to display 100% available processor; counting to 10000 takes about two seconds. If Main() is instead commented so that it terminates after starting BackgroundTask, each BackgroundTask loop takes an entire tick, resulting in ~7% available processor - taking ~20 seconds to reach 10000, one tick per count.


Code:
   dim BackgroundTaskStack(1 to 100) as byte

sub Main()
   call sleep(0.5)
   CallTask "BackgroundTask", BackgroundTaskStack   'processor overhead test
   
   'If the following loop is commented so that Main terminates, each Sleep in
   ' BackgroundTask takes a full tick despite no other running tasks
   do
      sleep(0)
   loop
   
end sub

' BackgroundTask displays percentage available processor time if it is run with other active tasks
sub BackGroundTask()
   dim iX as integer, lSpinCount as long, lNow as long
   Register.RTCTick = 0
   lSpinCount = 0
   Do
      For iX= 1 to 10000             'just spin,
         lSpinCount = lSpinCount + 1 ' accumulating apparent elapsed time
         Call Sleep(0)             '  while other functions run
      Next
      lNow = Register.RTCTick 'now and again, get actual elapsed time
      
      'The value 7.21 in the following line arbitrarily adjusts the displayed
      ' ratio of apparent:elasped time to 100% when no other tasks are running
      Console.WriteLine (fmt(Min(100.0, cSng(lSpinCount) * 7.21 / csng(lNow)), 1) _
         & "% available to BackgroundTask" )' show normalized ratio   
   Loop
end sub



I think this was discussed some time ago and, in fact, if there are more than one remaining running tasks, the VM gives them control appropriately after marking Main() stopped. It appears, though, that a single running task after Main() terminates remains improperly handled. Is that so?
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Location: Portland, OR

Posted: 20 September 2009, 3:16 AM    Post subject: Re: Task scheduler issue?

GTBecker wrote:
It appears, though, that a single running task after Main() terminates remains improperly handled. Is that so?
I'll have to investigate what's happening. Do you see the same anomaly on the ZX-24n?
Back to top
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 20 September 2009, 3:21 AM    Post subject: Re: Task scheduler issue?

dkinzer wrote:
Do you see the same anomaly on the ZX-24n?


Haven't tried that. I'll do it tomorrow.
Back to top
GTBecker



Joined: 18 Jan 2006
Location: Cape Coral

Posted: 20 September 2009, 22:21 PM    Post subject:

On a ZX-24n, a terminated Main() appears to use no processor. A Do:Sleep(0):Loop in Main() leaves 29.6% available to BackgroundTask.

A larger stack is necessary, and the display factor (7.21 on a current ZX-24a) must be set to 0.637 to normalize 100% available to BackgroundTask when Main() is terminated. The count to 10000 is too quick and measurable time is lost in the quickly-repeating display, so the count here uses a Long to count to 100000, instead.

I look forward to trying this again if you can correct the VM - assuming it is, in fact, misbehaving.

The code for the ZX-24n is:

Code:
 dim BackgroundTaskStack(1 to 200) as byte

sub Main()
   call sleep(0.5)
   CallTask "BackgroundTask", BackgroundTaskStack   'processor overhead test
   
   do
      sleep(0)
   loop
   
end sub

' BackgroundTask displays percentage available processor time if it is run with other active tasks
sub BackGroundTask()
   dim lX as long, lSpinCount as long, lNow as long
   Register.RTCTick = 0
   lSpinCount = 0
   Do
      For lX= 1 to 100000             'just spin,
         lSpinCount = lSpinCount + 1 ' accumulating apparent elapsed time
         Call Sleep(0)             '  while other functions run
      Next
      lNow = Register.RTCTick 'now and again, get actual elapsed time
     
      'The value 0.637 in the following line arbitrarily adjusts the displayed
      ' ratio of apparent:elapsed time to 100% when no other tasks are running
      Console.WriteLine (fmt(Min(100.0, cSng(lSpinCount) * 0.637 / csng(lNow)), 1) _
         & "% available to BackgroundTask" )' show normalized ratio   
   Loop
end sub


The terminated Main() problem only appears on the VM machine.
Back to top
Display posts from previous:   
Page 1 of 1

 



ZBasic Microcontrollers Home
All content Copyright © 2005, 2006, 2007, 2008, 2009, 2010 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group