操作系統多線程實現
Each process has an address space. There is one thread of control in every traditional OS. Sometimes, it is viable to have multiple threads of control in the similar address space which is running in quasi-parallel. Though they were separate processes they have same shared address space.
每個進程都有一個地址空間。 每個傳統的OS中都有一個控制線程 。 有時,在類似地址空間中以準并行運行方式具有多個控制線程是可行的。 盡管它們是獨立的進程,但它們具有相同的共享地址空間。
Threads are used in case of multiple applications running at the same particular time few activities might block from one point of time to another. By decomposition into multiple threads that are running in quasi-parallel, the programming model becomes simpler and easier.
在多個應用程序在同一特定時間運行的情況下使用線程 ,很少有活動可能從一個時間點阻塞到另一個時間點。 通過分解成多個準并行運行的線程,編程模型變得越來越簡單。
The new element can only be added with threads. This ability to share the same address space and data is essential for some applications.
新元素只能與線程一起添加。 對于某些應用程序,共享相同地址空間和數據的能力至關重要。
There are no resources attached to threads. Processes are difficult to create and destroy but threads, on the other hand, can be easily created and destroyed. Creating a thread isabout100x faster than creating a process.
沒有資源附加到線程。 進程很難創建和銷毀,但另一方面,線程卻可以輕松創建和銷毀。 創建線程的速度比創建進程快100倍。
The thread has program counter(pc)to keep the track of the instruction to be executed next. It also has registers to hold the presently working variables. There is a stack to store the execution history there is one frame for one procedure called but not still returned from.
該線程具有程序計數器(pc),以跟蹤下一條要執行的指令。 它還具有用于保存當前工作變量的寄存器。 有一個堆棧可以存儲執行歷史記錄,對于一個被調用但尚未返回的過程,只有一幀。
Threads are scheduled for the implementation or execution on CPU.
線程被安排在CPU上實現或執行 。
There are four states of a thread:
線程有四種狀態:
Running
跑步
Blocked
受阻
Read
讀
Terminated
已終止
The stack of each thread is as follows:
每個線程的堆棧如下:
There are two ways of implementing a thread package:
有兩種實現線程包的方法:
In user space
在用戶空間
In kernel
在內核中
Threads implementation in the user space
用戶空間中的線程實現
In this model of implementation, the threads package entirely in user space, the kernel has no idea about it. A user-level threads package can be executed on an operating system that doesn't support threads and this is the main advantage of this implementation model i.e. Threads package in user space.
在這種實現模型中,線程完全封裝在用戶空間中,內核對此一無所知。 用戶級線程包可以在不支持線程的操作系統上執行,這是此實現模型的主要優點,即用戶空間中的線程包。
Threads implementation in the kernel
內核中的線程實現
In this method of implementation model, the threads package completely in the kernel. There is no need for any runtime system. To maintain the record of all threads in the system a kernel has a thread table.
在這種實現模型方法中,線程完全封裝在內核中。 不需要任何運行時系統。 為了維護系統中所有線程的記錄,內核具有線程表。
A call to the kernel is made whenever there is a need to create a new thread or destroy an existing thread. In this, the kernel thread table is updated.
每當需要創建新線程或銷毀現有線程時,都會調用內核。 在此,內核線程表被更新。
Other two methods are as follows:
其他兩種方法如下:
Hybrid implementation
混合實施
Scheduler activation
調度程序激活
Hybrid implementation
混合實施
In this implementation, there is some set of user-level threads for each kernel level thread that takes turns by using it.
在此實現中,每個使用它輪流使用的內核級線程都有一組用戶級線程。
Scheduler activation
調度程序激活
The objective of this scheduler activation work is to replicate the working or function of kernel threads, but with higher performance and better flexibility which are usually related to threads packages which are implemented in userspace.
調度程序激活工作的目的是復制內核線程的工作或功能,但具有更高的性能和更好的靈活性,通常與在用戶空間中實現的線程包有關。
Pop-up threads
彈出線程
In this system, a new thread is created just to handle the message as soon as the arrival of a message takes place and thus it is called pop up thread.
在此系統中,將創建一個新線程來僅在消息到達時處理消息,因此將其稱為彈出線程。

The benefit of this pop-up thread is that they are brand new and hence don’t need any stacks or history registers, etc. that must be restored. Each pop-up thread is fresh and new and is similar to all other pop up threads. This is the reason why a pop-up thread can be created very quickly. The incoming message to be processed is given to the new thread.
此彈出線程的好處是它們是全新的,因此不需要任何必須還原的堆棧或歷史記錄寄存器等。 每個彈出線程都是新的,并且與所有其他彈出線程相似。 這就是可以很快創建彈出線程的原因。 要處理的傳入消息將提供給新線程。
The result of using such a thread is mainly that the latency between the arrival of the message and the start of processing is made very short.
使用這種線程的結果主要是使消息到達和處理開始之間的等待時間非常短。
翻譯自: https://www.includehelp.com/operating-systems/thread-Implementation.aspx
操作系統多線程實現