Given a single core system that employs preemption for multitasking, how does the OS carry out thread interrupts when a user mode application is executing on the system? Since the processor is handling the user code when does it get a chance to ask the thread scheduler for context switch. Is it handled similarly to page faults? And if it is, how often it happens and isn't it causing a performance hit?
Read up on hardware interrupts.
The hardware timer issues periodic interrupts. When one happens, the CPU switches into kernel mode and executes a routine, the interrupt handler. That's where context switching takes place. When the CPU comes back from kernel to user mode, it returns control to a different thread than the one that the interrupt happened in. The state of the preempted thread is saved, naturally.
Preemption happens immediately, not on the next time slice boundary. If not due to an interrupt, then it's due to a lower priority thread setting some type of event that a higher priority thread is pending on (waiting for). Normally the OS API switch from user mode to kernel mode is done via the X86 sysenter instruction.
Time slicing is used to switch between threads of equal priority, which isn't considered to be preemption.
lock
prefix can be patched into a NOP, leaving a "normal" read-modify-write insn like xadd
.) –
Brinkley One-by-one:
Given a single core system that employs preemption for multitasking, how does the OS carry out thread interrupts when a user mode application is executing on the system?
Two ways. 1) The user code makes a syscall that causes the kernel to be entered or a hardware peripheral requests a 'real' processor interrupt that causes a driver to be run, and that driver makes an appropriate kernel entry, eg. to ask for a scheduler run upon interrupt-return because it has made a thread ready.
Since the processor is handling the user code when does it get a chance to ask the thread scheduler for context switch.
The running user code can make syscalls that may change thread state whenever it wants to. On top of which, the kernel may be entered upon hardware interrupts from drivers, eg. KB, mouse, disk, memory-manager, network, timer.
Is it handled similarly to page faults?
A page-fault interrupt is a hardware interrupt from a peripheral: the memory-management hardware. It needs to load the appropriate page/s and restart the failed instruction that generated the interrupt. It's one in the set of hardware interrupts that may change thread state. 'Is it handled similarly to page faults?' is looking at the issue backwards.
And if it is, how often it happens
Well, that depends on how often threads make relevant system calls and how often they request resources that are not immediately available and so do not need CPU execution until the resources do become available.
and isn't it causing a performance
hit?
Well, no:) Preemptive multitasking usually results in a large performance gain since it does not apply the execution resource to thread that do not require it and can rapidly apply execution to threads that require it urgently. Without preemption, systems would be unable to respond promptly to IO completions and the IO performance would be abysmally bad. You could forget about apps like audio/video streaming/playback, high-speed net downloads and the like. The system performance would be too poor for them to work at all. This performance gain is the overriding reason for the wide use of such systems in most environments. There are examples where a preemptive OS may result in a perceived and/or real performance 'hit', but you have to try quite hard to find one on your typical desktop or server box.
© 2022 - 2024 — McMap. All rights reserved.