Operating System 简明教程

Operating System - Multi-Threading

What is Thread?

一个线程是一个通过进程代码执行的流,它有自己的程序计数器,用来跟踪下一个要执行的指令,有用来保存当前工作变量的系统寄存器以及包含执行历史的堆栈。

A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history.

一个线程与它的对等线程共享一些信息,例如代码段、数据段和打开的文件。当一个线程更改某代码段内存项时,其他所有线程都会看到。

A thread shares with its peer threads few information like code segment, data segment and open files. When one thread alters a code segment memory item, all other threads see that.

一个线程也称为 lightweight process 。线程提供了一种通过并行性来提高应用程序性能的方法。线程代表了一种通过降低开销来提高操作系统性能的软件方法,线程相当于一个传统进程。

A thread is also called a lightweight process. Threads provide a way to improve application performance through parallelism. Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process.

每一个线程都精确地属于一个进程,且没有一个线程能够存在于进程之外。每一个线程代表一个独立的控制流。线程已经在网络服务器和 Web 服务器的实现中得到了成功的应用。它们还为共享内存多处理机上应用程序的并行执行提供了一个合适的基石。下图显示了单线程和多线程进程的工作方式。

Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control. Threads have been successfully used in implementing network servers and web server. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors. The following figure shows the working of a single-threaded and a multithreaded process.

thread processes

Difference between Process and Thread

S.N.

Process

Thread

1

Process is heavy weight or resource intensive.

Thread is light weight, taking lesser resources than a process.

2

Process switching needs interaction with operating system.

Thread switching does not need to interact with operating system.

3

In multiple processing environments, each process executes the same code but has its own memory and file resources.

All threads can share same set of open files, child processes.

4

If one process is blocked, then no other process can execute until the first process is unblocked.

While one thread is blocked and waiting, a second thread in the same task can run.

5

Multiple processes without using threads use more resources.

Multiple threaded processes use fewer resources.

6

In multiple processes each process operates independently of the others.

One thread can read, write or change another thread’s data.

Advantages of Thread

  1. Threads minimize the context switching time.

  2. Use of threads provides concurrency within a process.

  3. Efficient communication.

  4. It is more economical to create and context switch threads.

  5. Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.

Types of Thread

线程通过以下两种方式实现:

Threads are implemented in following two ways −

  1. User Level Threads − User managed threads.

  2. Kernel Level Threads − Operating System managed threads acting on kernel, an operating system core.

User Level Threads

在这种情况下,线程管理内核不知道线程的存在。线程库包含用于创建和销毁线程、在线程之间传递消息和数据、调度线程执行以及保存和还原线程上下文的代码。该应用程序从单个线程开始。

In this case, the thread management kernel is not aware of the existence of threads. The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts. The application starts with a single thread.

user threads

Advantages

  1. Thread switching does not require Kernel mode privileges.

  2. User level thread can run on any operating system.

  3. Scheduling can be application specific in the user level thread.

  4. User level threads are fast to create and manage.

Disadvantages

  1. In a typical operating system, most system calls are blocking.

  2. Multithreaded application cannot take advantage of multiprocessing.

Kernel Level Threads

在这种情况下,线程管理是由内核完成的。应用程序区域中没有线程管理代码。内核线程直接受操作系统支持。任何应用程序都能被编程为多线程的。应用程序中的所有线程都在一个单一进程中受到支持。

In this case, thread management is done by the Kernel. There is no thread management code in the application area. Kernel threads are supported directly by the operating system. Any application can be programmed to be multithreaded. All of the threads within an application are supported within a single process.

内核对进程整体和进程中的各个线程维护上下文信息。内核根据线程进行调度。内核在内核空间中执行线程创建、调度和管理。与用户线程相比,内核线程通常创建和管理的速度较慢。

The Kernel maintains context information for the process as a whole and for individuals threads within the process. Scheduling by the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and management in Kernel space. Kernel threads are generally slower to create and manage than the user threads.

Advantages

  1. Kernel can simultaneously schedule multiple threads from the same process on multiple processes.

  2. If one thread in a process is blocked, the Kernel can schedule another thread of the same process.

  3. Kernel routines themselves can be multithreaded.

Disadvantages

  1. Kernel threads are generally slower to create and manage than the user threads.

  2. Transfer of control from one thread to another within the same process requires a mode switch to the Kernel.

Multithreading Models

一些操作系统提供了一种组合的用户级线程和内核级线程工具。Solaris 就是这种方法的一个很好的示例。在组合系统中,同一个应用程序中的多个线程可以在多处理器上并行运行,并且一个阻塞系统调用不必阻塞整个进程。多线程模式有三种类型:

Some operating system provide a combined user level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process. Multithreading models are three types

  1. Many to many relationship.

  2. Many to one relationship.

  3. One to one relationship.

Many to Many Model

多对多模型将任意数量的用户线程复用到相等或更少数量的内核线程上。

The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads.

下图显示了多对多线程模型,其中 6 个用户级线程与 6 个内核级线程进行复用。在这个模型中,开发者可以创建任意数量必要的用户线程,而对应的内核线程可以在多处理器机器上并行运行。此模型可以最准确地反映并发性,当一个线程执行阻塞系统调用时,内核可以调度另一个线程执行。

The following diagram shows the many-to-many threading model where 6 user level threads are multiplexing with 6 kernel level threads. In this model, developers can create as many user threads as necessary and the corresponding Kernel threads can run in parallel on a multiprocessor machine. This model provides the best accuracy on concurrency and when a thread performs a blocking system call, the kernel can schedule another thread for execution.

many to many

Many to One Model

多对一模型将许多用户级线程映射到一个内核级线程上。线程管理在用户空间中通过线程库完成。当线程执行一个阻塞系统调用时,整个进程将被阻塞。一次只能有一个线程访问内核,因此多线程无法在多处理器上并行运行。

Many-to-one model maps many user level threads to one Kernel-level thread. Thread management is done in user space by the thread library. When thread makes a blocking system call, the entire process will be blocked. Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.

如果以系统不支持的方式在操作系统中实现用户级线程库,那么内核线程将使用多对一关系模式。

If the user-level thread libraries are implemented in the operating system in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship modes.

many to one

One to One Model

用户级线程和内核级线程之间是 一对一 关系。此模型比多对一模型提供了更高的并发性。它也允许在某个线程执行阻塞系统调用时运行另一个线程。它支持多线程在微处理器上并行执行。

There is one-to-one relationship of user-level thread to the kernel-level thread. This model provides more concurrency than the many-to-one model. It also allows another thread to run when a thread makes a blocking system call. It supports multiple threads to execute in parallel on microprocessors.

这个模型的劣势是创建用户线程需要相应的内核线程。OS/2、Windows NT 和 Windows 2000 使用一对一关系模型。

Disadvantage of this model is that creating user thread requires the corresponding Kernel thread. OS/2, windows NT and windows 2000 use one to one relationship model.

one to one

Difference between User-Level & Kernel-Level Thread

S.N.

User-Level Threads

Kernel-Level Thread

1

User-level threads are faster to create and manage.

Kernel-level threads are slower to create and manage.

2

Implementation is by a thread library at the user level.

Operating system supports creation of Kernel threads.

3

User-level thread is generic and can run on any operating system.

Kernel-level thread is specific to the operating system.

4

Multi-threaded applications cannot take advantage of multiprocessing.

Kernel routines themselves can be multithreaded.