Sam111 wrote:I am on a old 32bit machine
so I don't think LAPIC timer applies I think I only have the PITS , and RTC to work with here.
According to
the specifications, this is a Pentium 4 with hyper-threading, and therefore definately does have local APICs (and local APIC timers). The Intel 865G chipset also has HPET, which is far better than PIT.
You also don't actually need to use a timer, even for "non-cooperative scheduling". It's possible to use (for example) the performance monitoring interrupt instead, to allow each task to execute for "N instructions" before it is pre-empted. There are reasons (involving power management and its interaction with modern variable speed CPUs) why this approach is superior (fairer) than using a timer; and it's also likely to be far more precise.
Sam111 wrote:But their is no multiprocessors or dual cores ,...etc stuff on this old computer so SMP doesn't apply.
The process or is an old pentium 4. So only single core uni-processor (one processor).
As far as I can tell, it's single-core with hyper-threading. Hyper-threading makes one core look like 2 logical CPUs (which helps to keep the core's "otherwise idle" execution units busy, especially when one logical CPU has to wait for cache misses, etc).
However, hyper-threading in Pentium 4 wasn't as good as it is more recent CPUs (and software was worse back then); so some software (especially badly designed software running on an OS that isn't optimised for hyper-threading) can run slower than it would with hyper-threading disabled. For good software you can expect up to about 15% performance improvement by using hyper-threading (by supporting SMP) on your Pentium 4.
Basically, SMP does apply (although you may need to enable it in the BIOS); and even though you're not going to get 100% performance improvement it's still worth doing because it'll mean you can upgrade your computer (e.g. to a quad-core) some time in the next 10 years without rewriting half your OS to use SMP.
Sam111 wrote:Brendan wrote:There's 3 ways. Any IRQ (not necessarily a timer IRQ) interrupting the program, the program causing an exception (which may or may not mean the program crashed), or the program explicitly calling the kernel's API.
Ok, but in theory you must use a type of interrupt weather it be an exception , kernel call , ...etc
(i.e correct me if I am wrong you cann't do context switching/mulitasking if the interrupts where disabled with cli and never enabled )
(when I say mulitasking I mean not having to wait for one program to completely complete before the next task can be executed/run.)
The kernel API can use a call gate, SYSENTER or SYSCALL. None of these are a type of interrupt, and all of them are faster than a software interrupt.
Note: Your Pentium 4 probably doesn't support SYSCALL, but will support call gates and SYSENTER).
Disabling interrupts (e.g. with "CLI") will not prevent software from using software interrupts, call gates, SYSENTER or SYSCALL, and doesn't prevent software from generating exceptions. It only effects IRQs.
Sam111 wrote:Correct me if I am wrong when a PITS interrupt is called I should cli (so no other interrupts are called) do the PITS function code then sti them back on and iret back to the schedular function.
When an interrupt occurs the CPU looks at the "Interrupt Descriptor Table" to figure out what to do. For interrupts, there's 3 types of descriptors that can be in the IDT - "interrupt gate" (which automatically disables IRQs for you), "trap gate" (which doesn't disable IRQs for you) and "task gates" (which is part of hardware task switching and not something most people bother with).
The normal approach is to use an "interrupt gate" for the timer IRQ, so that the CPU automatically disables IRQs for you (and you don't need to do "CLI" yourself). Some people would begin the IRQ handler by setting some sort of "don't pre-empt me" flag, then enabling IRQs for the remainder of the interrupt handler to ensure that more important IRQs aren't postponed. Basically, you should never need to use "CLI" to disable IRQs in an interrupt handler, but you may want to use "STI" to enable IRQs in an interrupt handler.
Sam111 wrote:If the above is how it works then I am curious how much goes into the PITS interrupt function and how much task switching stuff should go into the schedular routine function. Should the PITS only set some global variables that the schedular uses to switch tasks and have the schedular do all the other work. Or should the PITS function be responsible for actually saving a Tasks data and have the schedular be responsible for only scheduleing/prioritizing/loading the next task. I guess it is totally up to the designer but was curious typically how major os's do it (like windows,linux,mac,..etc).
Your scheduler should have a low level "goto_task(task_id)" function, which does a task switch from the currently running task to a different (explicitly requested) task. You scheduler should also have a "do_task_switch(void)" function that determines which task to switch to and then calls the "goto_task(task_id)" function. The timer interrupt handler should only call the scheduler's "do_task_switch(void)" function.
Note: Different tasks block for different reasons (e.g. waiting for disk IO, called "sleep()", etc). When a task blocks for any reason, the kernel needs to find another task to run. Therefore the scheduler's "do_task_switch(void)" function would be called by lots of different parts of the kernel (not just the timer IRQ). When a task unblocks (e.g. it was waiting for disk IO and the disk IO was completed, or it called "sleep()" a while ago and needs to wake up now) the task can be given CPU time again. If the task that was unblocked is a higher priority than the currently running task, then you probably want the unblocked task to pre-empt the currently running task. In this case the scheduler's "goto_task(task_id)" function would be used by any code in the kernel that causes tasks to unblock (e.g. "goto_task(the_higher_priority_task_that_I_just_unblocked)" and not just used by the scheduler's "do_task_switch(void)" function.
Cheers,
Brendan