操作系统多线程实现
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
操作系统多线程实现