How do interruption technique help to implement multithreading?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-15 05:24:28

问题


How do interruption technique help to implement multithreading? Or what's the relationship between multithreading and interruption?

Thank you very much!


回答1:


'Or what's the relationship between multithreading and interruption?'

On most modern preemptive OS, an interrupt is how the OS is entered - unless there is an interrupt, the OS does nothing. Interrupts come in two flavours - hardware interrupts from devices/drivers, (ie. disk, keyboard, NIC, mouse, USB, memory-management hardware, timer hardware), and 'software interrupts', ie. system calls. Either type of interrupt can change the set of ready/running threads. Essentially, the OS is a big interrupt-handler that can choose to change the set of threads that will run after an interrupt return.

The rescheduling on hardware interrupts allow a preemptive, multithreaded OS with the ability to respond quickly when hardware, (and/or its associated driver), requires attention.

This high-performance response at thread level to hardware signaling is the single biggest advantage that a preemptive multitasker has over cooperative systems and is the main reason why we put up with the complication of inter-thread signaling, synchronization etc.

Without interrupts and preemption, the performance of disks, networks, USB, displays, almost everything on our desktop systems would be so apallingly bad that apps like video streaming would just not work. When a NIC chip has a nice, big buffer of video, it can raise a hardware interrupt, have its driver run and load up a user buffer with the data and make a thread in the video streaming app, that was waiting on the I/O, ready/running so that it can display the data.




回答2:


The most common implementation of time- and event-driven thread scheduling is based on interrupts. For example, the hardware timer generates periodic interrupts. The ISR that handles them can save the state of the currently executing thread into the thread's state structure and then load the state of another thread from another structure and upon execution of the last ISR instruction (some sort of "return-from-interrupt"/IRET) make the CPU execute that other thread.




回答3:


It's precisely analogous to how timeslicing helps with multi-tasking. If a thread can be interrupted to run another thread, you don't have to worry about a thread hogging the processor and preventing other threads from making any forward progress.




回答4:


multithreading

thread1 ______          _____ 
thread2       _____          _____
thread3            _____          _____

threads share the CPU time

interruption

isr              ________
threadn _________        _________

interrupt always cause a context switch to an interrupt handler/ interrupt service routine and force taking the CPU time.



来源:https://stackoverflow.com/questions/9497542/how-do-interruption-technique-help-to-implement-multithreading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!