Process Management (13) - Thread

Process Management (13) - Thread

What is the thread

Process is the basic unit of program execution time and resource allocation. A basic scheduling unit in the process is called a thread (Thread) or Right process called light (Light weight process), the scheduling unit may be controlled by the operating system kernel may be programmed by the user.

Why introduction thread

The main thread is introduced in order to improve the efficiency of the system, idle time and reduce the time scheduled handover processor (protected field information), and to facilitate system management.

Thread VS process

process

1. The process is the basic unit of resource allocation.
2. All resources associated with the process, are recorded in the process control block PCB. To indicate that the process has the resources or are using them.
3. The process is also preempted processor scheduling unit, it has a complete virtual address space
4. scheduling process occurs, different processes have different virtual address space.

Thread

1. Thread Independent resource and resource allocation, it belongs to a particular process, and the process of sharing with other threads within a process
different threads share the same address space within the same process 2.
3. thread only by the relevant stack (system stack or user stack) thread control registers and tables composed of TCB. Register can be used to store threads in a local variable, but can not store other relevant variables thread.

Thread switching

The process of switching: involves issues such as address changes and save space on resource pointer during the process of switching, scheduling and switching processes are done by the operating system kernel

Thread switching: When a thread switch, does not involve the preservation and address resource information change (threads share resources and address space within the same process) to reduce the overhead time operating system. Scheduling and thread switching is completed either by the operating system kernel, but also by the user program.

Scope thread

The benefits of using threads

The biggest advantage of using threads when multiple tasks need to be addressed to reduce the switching time processor; and, creating and ending threads required overhead than the process of creating and ending much smaller.

Suitable for multi-threaded system

The most suitable thread system is a multi-processor system, the same user program can be divided according to different functions for different threads, on the implementation of the different processors.

Not suitable for multi-threaded system

Rarely do the switching process scheduling and real-time systems, personal digital assistant system, due to the unity of the task, set the thread on the contrary it will take up more memory space and register

Multi-threaded applications

Or document management server 1. The communication control

LAN file server, access to documentation requirements can be derived server process threads for processing. Since the server at the same time likely to accept many file access requirements, the system can generate multiple threads simultaneously for processing. If the computer system is a multiprocessor, these threads can also be arranged to execute on different processors.

2. Taiwan before and after treatment

Many users have had the station before and after the treatment experience, that a large amount of calculation program or less demanding real-time program scheduled to run on the processor is idle. For the same process in the above procedures, the thread may be used to reduce the switching time and increase processor execution speed.

3. Asynchronous Processing

If the program is not in two parts on a predetermined execution sequence, then the two parts of the program are available thread.

Batch and sending and receiving information in a network system 4. The data processing and other related

The execution characteristics of the thread

Three states and five operations

For three basic state of the thread, the thread presence status to convert five kinds of basic operation. These five basic operations are:

Derivation

Threads within a process derived out, it can be derived from the process, a thread can also be derived. Users generally use system calls (or the corresponding library functions) derived own thread.

New derived thread is placed in the ready queue

Clog

If a thread needs to wait for an event occurs during execution, it was blocked. When the obstruction, register context, the program counter, and stack pointer are guaranteed to give

activation

If the thread is blocked event occurs, the thread is active and enter the ready queue.

Dispatch

Select a thread enters execution ready state.

End

If the end of a thread execution, the register context and its contents will be released stack

Synchronize

Since all threads in the same process share all resources and address space of a process, any thread of resources will affect the operation of the other threads. Therefore, the system must provide for the implementation of thread synchronization control mechanisms to prevent execution of a thread and the destruction of data structures and other adverse effects on other threads, synchronization control mechanism and process threads used used the same synchronization control mechanism

Classification threads

User-level threads

1. The user-level threads (user level threads) management process completed by the user program, the operating system kernel only manage the process
2. In order for a user-level thread management, the operating system provides an execution thread library in user space, the thread library to create, schedule, revocation threading capabilities. Meanwhile, the library also provides an execution thread, and the thread context stored communication between threads, the thread function
3. only the user-level threads is assigned to the user stack and registers the user process belongs

Thread creation process

1. When a thread is derived, the corresponding thread library generated for the thread control block TCB data structures and the like, and the assignment and thread into the ready state as a parameter in the TCB

2. The process of its creation process is similar to the process. the difference is

(1) scheduling algorithm and the user-level scheduling of all threads and choose OK, regardless of the operating system kernel by the user. In the user-level thread system, the operating system kernel scheduling unit is still process. If the process of scheduling interval is T, T in the range, the user can set a different thread scheduling algorithm according to their needs.

(2) user-level thread scheduling algorithm only thread context switching without switching processor, and its thread context switching is performed in the kernel is not involved.
Thread context switching only switch between user stack, the user registers, the processor does not involve state.
The new thread calls by the program pointer changes make the program counter changes to be implemented.

(3) because the context of the user-level threads and kernel independent handover, there may occur a case where: a process that, when since the I / O interruption time slice expires or the other causes the processor to exit the process, while the process execution part of the thread is still being executed.
That is, although the state-related processes are blocked or waiting, but belongs to the thread state is performed.
But the thread is blocked may cause the process belongs to the thread is blocked.

System-level threads (kernel-level threads)

1. The operating system kernel to the application to provide the appropriate system calls and application program interface API, so that the user program can create, execute, revoke thread.
2. with different user thread, core-level threads can either be scheduled on a processor concurrently, also is scheduled to be executed in parallel on different processors
dispatch 3. The operating system kernel is responsible for both processes, it is also responsible for the different threads within a process scheduling work. Therefore, in the case of blocking the process or wait state, and the thread in the execution state of the core-level thread does not appear.
4. The core-level threads are also available for the kernel itself, thereby improving the efficiency of the operating system kernel

Disadvantages:
1. Compared with the user-level threads, kernel-level thread context switching time is greater than a user-level thread context switch time
2. Compared with the system-level threads, another advantage is that no user-level threads operating system kernel special support, as long as one can provide thread creation, scheduling, execution, undo, and communications functions of the threads library on the line

Guess you like

Origin www.cnblogs.com/mengxiaoleng/p/11626766.html