Multitasking Scheduling

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
chorakm
Posts: 14
Joined: Fri Dec 08, 2006 12:07 am

Multitasking Scheduling

Post by chorakm »

I'm planning on implementing multitasking. I have a few questions. Firstly. I was going to use the PIT to fire an interrupt every so often on IRQ0. Then I will put the scheduler in the interrupt handler for IRQ0. How often should the PIT fire? I figure, if it fires every 10 milliseconds(100 times a second), each task would have a minimum of 10 milliseconds of execution time. If I have 100 tasks, would it be safe to say that it could take 1 second before the task sees the processor again? Wouldn't this create a very non responsive system? On another note, when I use VirtualBox(emulator), I can't set the frequency of the PIT very high or else the emulator can't keep up(doesn't fire the PIT at correct intervals), but when I run linux on the emulator, it seems to run in real time. Doesn't linux use the PIT? I'm sort of confused. Any input would be great!
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

I have set the pit in my os to 1ms. It just seems like such a nice number. Also you have to remember that even if you had 100 tasks only maybe 10 of those would be able to run at any given moment, the rest will be blocking or asleep. Also a lot of systems use priorities when deciding which task will run next. That way the really important tasks like putting the next letter on the screen in a word processing program or moving the mouse don't seem to take forever, while background tasks such as checking to see if you have any new email take a little bit longer.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

setting the pit to 1 ms is maybe not that smart because you get a 1000 interrupts per second and thus 1000 priviledge level switches, 1000 register save/restores and it certainly kills your cache. 10 ms is not that bad and is used by most operating system but only with round-robin, that is the behaviour what chorakm described. If you want more responsiveness you'll need priorities and preemptive multitasking, which you can read about in the faq/wiki or google.
Author of COBOS
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

It is not that difficult to change at a later date though so you really don't have to worry about what you set it to now. Though you shouldn't set the PIT to any faster than 1ms. I would agree there.
chorakm
Posts: 14
Joined: Fri Dec 08, 2006 12:07 am

Post by chorakm »

another question I have is, how do kernels like linux and windows detect cpu usage for a processes? Linux can tell me what "percentage" of the cpu a certain process is utilizing. For some reason I cannot figure out a way to do this in code! I'm thinking, if a process doesn't yield its timeslice for the full duration its supposed to receive, so it ends up using its entire timeslice, that could would be one way to measure cpu usage. do you guys have a method?

also. there are a lot of processes on my linux box that are sleeping. does the scheduler pretty much ignore these tasks? and are all of them calling a sleep function or does the scheduler figure out which processes to put to sleep?

thanks guys!
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

also. there are a lot of processes on my linux box that are sleeping. does the scheduler pretty much ignore these tasks? and are all of them calling a sleep function or does the scheduler figure out which processes to put to sleep?
Well some of the tasks will be sleeping because they called sleep and some of them will be waiting on something to complete, such as a file access. For the most part the scheduler will yes just ignore those processes by not placing them on the to be run queue.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Post by bewing »

I think I'm going to have my timer interval be adjustable by the superuser in a config file. I don't see any reason to force it to any preset value, what with processor speed doubling so often.

And as far as percentage usage goes, I suspect they mostly do it by just calculating the number of times the process was started with a task switch. It doesn't matter if the process used the whole timeslice or not. If there were 30,000 task switches in a given second, and the process got 600 of them, then it got "5%" of the machine. -- Alternately, all recent Intel CPUs have a "performance monitoring register" that can count cpu cycles. You can use that to accumulate a total # of cpu cycles per process if you like.
Post Reply