导言:本文是在小组内的一个分享,介绍协程实现的几种方法和优化策略,对比GoLang中goroutine实现方式及调度器的设计,与常见后台服务器设计模式对比,使用协程的优劣分析。
some questions
Q1: multitasking ?
time-sharing (1960s,voluntarily/hardware interrupt to relinquish the CPU)
real-time (real-time constraint)
Multiple processes
(heavyweight)are allowed to share processors (CPUs) and other system resources.Threads
(lightweight)are scheduled preemptively and are described as lightweight processes because switching between threads does not involve changing the memory context. It can make use of machines with multiple processors.- other ?
Q2: context switch ?
Most commonly, within some scheduling scheme, one process must be switched out of the CPU so another process can run. This context switch can be triggered by the process making itself unrunnable, such as
by waiting for an I/O or synchronization operation to complete
. On a pre-emptive multitasking system, the scheduler may also switch out processes which are still runnable. To prevent other processes from being starved of CPU time, preemptive schedulers often configure a timer interrupt to fire when a process exceeds its time slice. This interrupt ensures that the scheduler will gain control to perform a context switch.In computing, a
context switch
is the process of storing and restoring the state (more specifically, the execution context) of a process or thread so that execution can be resumed from the same point at a later time. This enables multiple processes to share a single CPU and is an essential feature of amultitasking operating system
.
- thread switch/process switch/task switch
- mode switch (switching between user mode and kernel mode)
- register switch
- stack frame switch
- address space switch (changing virtual memory to physical memory map)
Q3: the cost of context switches ?
Context switches are usually
computationally intensive
. Switching from one process to another requires a certain amount of time for doing the administration – saving and loading registers and memory maps, updating various tables and lists, etc. For example, in the Linux kernel, context switching involves switching registers, stack pointer, and program counter.
Q4: context switching can be selective and store only those registers that need storing ?
- context switching between
threads
in the same process is typically faster than context switching betweenprocesses
. Thread switching is relatively cheap: it requires a context switch (saving and restoring registers and stack pointer), but does not change virtual memory and is thus cache-friendly (leaving TLB valid).- a context switch between threads requires system calls (involving the OS kernel), which can cost more than thousand CPU cycles on x86 CPUs. By contrast,
transferring control
among them requires only fewer than hundred CPU cycles because it does not involve system calls as it is done within a single thread.- Making coroutines fast (reuse of stacks and a lightweight swapcontext implementation.)
Q5: pre-emptive multitasking (time slice/instruction cycle) VS co-operative multitasking (non-preemptive multitasking) ?
Preemptive multitasking
involves the use of aninterrupt mechanism
which suspends the currently executing process and invokes a scheduler to determine which process should execute next. Therefore, all processes will get some amount of CPU time at any given time.As a
cooperatively multitasked system
relies on each process regularly giving up time to other processes on the system, one poorly designed program can consume all of the CPU time for itself, either by performingextensive calculations
or bybusy waiting
; both would cause the whole system to hang. In a server environment, this is a hazard that makes the entire environment unacceptably fragile. A cooperative multitasking system wherein processes or tasksmust be explicitly programmed to yield when they do not need system resources
.
user thread
Coroutines(a language-level construct)are computer program components that generalize subroutines for nonpreemptive multitasking
, by allowing multiple entry points for suspending and resuming execution at certain locations. Coroutines are well-suited for implementing more familiar program components such as cooperative tasks
, exceptions, event loop
, iterators, infinite lists and pipes.
According to Donald Knuth, the term coroutine wa