OS Multitasking time distribution

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
MrLolthe1st
Member
Member
Posts: 90
Joined: Sat Sep 24, 2016 12:06 am

OS Multitasking time distribution

Post by MrLolthe1st »

Hi all!
I'm written multitasking for my own OS, it working ok, but i'm switching tasks when interrupt from PIT has thrown, an interrupt is working with frequency ~1000.15 Hz and that give me about 1000 processes will be working in one second(for every ~1ms, sorry for my english), but when running more than 1000 processes my task-switching mechanism will work more one second. Are there other methods to switch task more than 1000 times per second?
WIth best regards, thanks a lot,
Aleksandr
frabert
Member
Member
Posts: 38
Joined: Wed Jul 25, 2018 2:47 pm
Location: Pizzaland, Southern Europe
Contact:

Re: OS Multitasking time distribution

Post by frabert »

If your processes each have a time quantum of 1ms, and you have more than 1000 of them, then there's no way you can execute them all within 1s, unless some of them yield partway through their time slice, so your only option is to increase the timer's frequency. But the bigger question is, why do you want your processes to end before 1s? Seems like a XY problem to me
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: OS Multitasking time distribution

Post by Brendan »

Hi,
MrLolthe1st wrote:I'm written multitasking for my own OS, it working ok, but i'm switching tasks when interrupt from PIT has thrown
Unfortunately, assuming task switches are caused by the timer IRQ and nothing else is a common mistake. I've even seen kernels where (e.g.) near the start of its time slice a process will call a function like "read()" and have to wait for data to arrive from disk, but the CPU will be left doing nothing for almost the entire time slice until a timer IRQ triggers a task switch; so about 99% of CPU time ends up being wasted.

The reality is that most task switches are caused by the currently running task blocking (e.g. needing to wait for data from another process, or from keyboard or network or disk or...), or are caused by something happening that a task was waiting for (especially when there were no other tasks running at the time or there's some kind of task priorities involved); and doing task switches when the timer IRQ occurs is just a relatively unimportant optional thing in case a task doesn't block sooner.

I guess what I'm saying is that you could set your timer to 1 Hz, and still get 1 million task switches per second between thousands of tasks.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply