How long do you wait between task switching?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Unspoken_Magi

How long do you wait between task switching?

Post by Unspoken_Magi »

How long do you wait between task switching? (on the ia-32)

If you are running two threads on a single processor machine, how long should you wait to switch? The thread shouldn't have to call Sleep(1), should it?

Also, do you think it would be reasonable to run multiple tasks on the 16MHz ARM GBA processor?
gtsphere

Re:How long do you wait between task switching?

Post by gtsphere »

i recently wrote a round robin simulator and one thing i learned about time sharing is that you are able to give it any amount you want. The way i did it was i just set up a general kTicks, which is nothing more than a total amount of ticks each process is allowed, and depending on how it uses those ticks, it is then either done - rescheduled - put in IO, etc. My demostation was 50 ticks, but a good idea would be to set ProcessTicks and IOTicks, cause maybe you want more time for the IO, so if its an IO call, use the IO ticks, so it is given more time / less time. But thats just the option, so its pretty much up to you.

I hope this helps
-GT
Tim

Re:How long do you wait between task switching?

Post by Tim »

Most of the time it won't matter. Think of a typical program: most of the time it's waiting for something else, such as a key from the keyboard, or for the kernel to write to a file. This is co-operative multi-tasking.

For example, when a thread tries to read from the keyboard, you should add it to a list of threads waiting for the keyboard, remove it from the run queue, and select another thread to run. If there are no threads to run, schedule the idle thread, which does nothing. Then, when the user presses a key, you can remove the first thread from the list waiting on the keyboard, add it to the run queue again, and reschedule. From then on, the thread will continue to run (until the next time it stops).

Task switching on the timer is only used when a thread doesn't attempt to wait on anything else. You should schedule on the timer interrupt periodically to prevent these threads from using the CPU forever and stopping other threads from running. This is pre-emptive multitasking; a good system will use a combination of both types.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How long do you wait between task switching?

Post by Pype.Clicker »

The only case where the "quota" value may have an impact is when you have a cpu-bound thread (i.e. that goes on computing until it is interrupted by the scheduler) and that a I/O bound thread is waken up to process some new input.
If the I/O-bound thread is put at the *end* of the scheduler queue, and if you have no priorities mechanism, then your I/O response time will be slow because you'll have to wait for a scheduling quota to be exhausted before the I/O can actually be processed.
And the longer the quota will be, the slower the response.

That's why most of the systems will have priorities for just-awaken threads against long-running threads...
Post Reply