Concurrent Programming Notes learning context switching

Before the introduction of a context switch to introduce the concepts processes, threads in order to better understand the context switch

Process: defined in the operating system of a computer program is run on a data collection activities on, the system is the basic unit of resource allocation and scheduling. In earlier operating systems, indeed the execution of a program directly from the process, that is, the so-called program data, instructions and organizational forms of description. The process has its own independent stack and heap, the heap is neither shared nor shared stack, the process of scheduling by the operating system

Thread: Early operating system program is executed by the process, and now the operating system is performed by a thread, the thread is the process container, each process has its own address space, under normal circumstances, including the text area (text region) a data area (data region) and stack (stack region). Code is executed by a processor stored text area; dynamically allocated memory area stores data used during execution process variables; stack area stores instructions and local variables of the calling procedure activities. Thread has its own independent stack and heap share, thread heap is shared, not shared stack, the same thread scheduling by the operating system

Coroutine: similar to the subroutine and coroutine. Coroutine (the coroutine) is also a component. Coroutines and threads share the same stack, the stack is not shared. Coroutine whether to schedule controlled by a programmer codes, code control well, it is possible to avoid meaningless scheduling, it can be used to avoid coroutine

In JDK native Java do not provide the corresponding api, can only be done through a third party component, there is supported on the two frame github, https://github.com/offbynull/coroutines,https://github.com/ kilim / kilim

Context switching (context switch): For single-core CPU, in one time only one thread is running, the parallel, a single core cpu can also support multi-threaded execution of the code, is solved by a CPU time slice allocated to threads the so-called CPU time slice to each site is assigned time slice time is very short, so after the completion of the implementation of a time slice, task switching, first save the state of the task before switching to the next change in come back, you can load the task of the state, so the process to re-load the task from the task of saving state called context switching between threads can only context switching, the process can also

Context Switch Testing:

  • Lmbench3 [1] to measure the duration of the context switch.
  • Use vmstat context switches can be measured.

Method avoiding context switching:

Multi-threaded lock contention would lead to frequent context switching, so you can start from these two aspects, one is a lock, that is, do not try to lock; one aspect is the thread, without thread, replaced by other methods

  • Modulo segment, in accordance with the hash algorithm modulo id segment, data of different processing threads of different end
  • CAS algorithm, java is the use of Atomic CAS algorithm to update the data and did not use locks
  • Coroutine using methods not place unnecessary calls, context switches to avoid
  • volatitle applications, volatile keyword can be said to be lightweight lock, volatile keyword is the visibility of thread operations to achieve, it can be used to avoid a context switch

Guess you like

Origin www.cnblogs.com/mzq123/p/11582282.html