Threads and processes

Source: Internet
Author: User

Process Concepts

Process is the basic unit of resource allocation and the basic unit of dispatching operation. For example, when a user runs his or her own program, the system creates a process and assigns IT resources, including tables, memory space, disk space, I/O devices, and so on. Then, put the process in the ready queue of the human process. The process Scheduler selects it, allocates CPU and other related resources to it, and the process actually runs. Therefore, the process is the unit of concurrent execution in the system.

In a microkernel-based operating system such as Mac, Windows NT, the functionality of the process has changed: it is only the unit of the resource allocation, not the unit of the dispatch run. In a microkernel system, the basic unit of a real dispatch operation is a thread. Therefore, the unit that implements the concurrency feature is the thread.

Threading Concepts

A thread is the smallest unit of execution in a process, that is, the basic unit of the execution processor dispatch. If the process is understood as a task that is done logically by the operating system, then the thread represents one of many possible subtasks that complete the task. For example, if a user launches a database application in a window, the operating system represents a call to the database as a process. Suppose a user is going to generate a payroll report from a database and upload it to a file, which is a subtask; in the process of generating payroll reports, the user can also lose the database query request, which is a sub-task. In this way, the operating system represents each request-payroll report and the new sender's data query as a separate thread in the database process. Threads can be scheduled to execute independently on the processor, allowing several threads to be on separate processors in a multiprocessor environment. The operating system provides threads for the convenience and effectiveness of this concurrency

Benefits of introducing threading

(1) Easy to dispatch.

(2) Improve concurrency. Concurrency can be easily and efficiently achieved through threading. A process can create multiple threads to perform different parts of the same program.

(3) less overhead. Creating a line turndown creates a process that is fast and requires little overhead.

(4) Facilitate the full use of multi-processor functions. By creating a multithreaded process (that is, a process can have two or more threads), each thread runs on a single processor, enabling the concurrency of the application so that each processor is fully operational.

Relationship of processes and threads

(1) A thread can belong to only one process, while a process may have multiple threads, but at least one thread. A thread is the smallest execution and dispatch unit that the operating system can recognize.

(2) A resource is allocated to a process, and all the threads of the same process share all the resources of that process. Multiple threads in the same process share code snippets (code and constants), data segments (global variables and static variables), extension segments (heap storage). However, each thread has its own stack segment, which is also called the runtime window, which is used to hold all local variables and temporary variables.

(3) The processor is assigned to a thread, that is, a thread that is actually running on the processing machine.

(4) Threads need to be synchronized during execution. Synchronization is achieved between threads of different processes using the means of message communication.

Processor management is one of the basic management functions of the operating system, and it is concerned with the problem of processor allocation. In other words, the use of CPU (central processor) to a program, usually the program is ready to enter the memory called the job, when the job into memory, we call it a process.

Since the 60 's, the process concept has been presented in the operating system as a basic unit capable of running independently. It was not until the middle of the 80 that people raised the basic unit-thread, which could run independently than the process, and tried to use it to increase the speed of concurrent execution of programs in the system, which could further improve the throughput of the system. In recent years, the concept of threading has been widely used, not only in the new operating system, most of the threading concept has been introduced, but also in the new database management system and other application software, has introduced a thread to improve the performance of the system.

If the purpose of introducing a process in the operating system is to enable multiple programs to execute concurrently to improve resource utilization and improve the throughput of the system, then the introduction of threads in the operating system is intended to reduce the time and space overhead incurred by program concurrency and to make the operating system more concurrency-efficient. To illustrate this, we first review the two basic properties of the process:

(1) A process is an independent unit that can have resources;

(2) The process is also a basic unit that can be dispatched and distributed independently. It is because the process has these two basic attributes that it becomes a basic unit that can run independently and thus forms the basis for concurrent execution of the process.

However, in order for the program to execute concurrently, the system must also perform the following series of actions:

(1) Create a process. When the system creates a process, it must allocate all resources that it requires, except the processor. such as memory space, i/0 equipment and the establishment of the corresponding PCB.

(2) Undo process. When you revoke a process, the system must recycle the resources before undoing the PCB.

(3) Process switching. When switching processes, it takes a lot of processor time to keep the current process's CPU environment and set the CPU environment for the newly selected process.

In short, because the process is a resource owner, the system must pay a large space-time overhead for the process's creation, revocation, and switchover. Because of this, the number of processes set in the system should not be too much, the frequency of process switching should not be too high, but this also limits the degree of concurrency to further improve.

How to make more concurrent execution of multiple programs, while minimizing the overhead of the system, has become an important goal in designing the operating system in recent years. So, a lot of operating system scholars think, can be the process of the above attributes, separated by the operating system for processing. That is, as the basic unit of dispatch and dispatch, not as a unit of independent allocation of resources, so as to make it run lightly, and for the basic unit of resource, it is not frequently switched. It is under the guidance of this idea that a threading concept is produced.

In an operating system that introduces threads, a thread is an entity in a process that is the basic unit of Dispatch and dispatch independently by the system. The thread itself does not own the system resources, it has only a few resources (such as program counters, a set of registers and stacks) that are essential in the run, but it can share all the resources owned by the process with other threads of the same process. One thread can create and revoke another thread, and can execute concurrently between multiple threads in the same process. Because of the mutual restriction between threads, the thread is also intermittent in running. Accordingly, the thread is also ready, blocking, and executing three basic states, some of which have a terminating state.

Thread-to-process comparison

Threads have the characteristics of many traditional processes and are also referred to as lightweight processes (Light-weight process) or process elements, whereas traditional processes are called heavy-duty processes (Heavy-weight process), which is equivalent to a task with only one thread. In the operating system in which threading is introduced, typically a process has several threads and at least one thread is required. Below, we compare threads and processes in terms of scheduling, concurrency, overhead, owning resources, and so on.

1. Scheduling

In the traditional operating system, the basic unit with resources and the basic unit of dispatch and dispatch are all processes. In the operating system in which threading is introduced, threads are used as the basic unit of dispatch and dispatch. And the process as the basic unit of resources, so that the traditional process of the two attributes, the thread can run light, so that the system can significantly improve the degree of concurrency. In the same process, the switch of the thread does not cause a switchover of the process, which causes the process to switch when a thread in one process switches to a thread in another process.

2. Concurrency of

In the operating system that introduces the thread, not only can the process be executed concurrently, but also execute concurrently between multiple threads in a process, so that the operating system has better concurrency, which makes it more efficient to use system resources and improve system throughput. For example, in a single-CPU operating system that does not introduce threads, if only one file service process is set up, when it is blocked for some reason, there is no other file services process to provide the service. In an operating system that introduces threads, you can set up multiple service threads in a single file service process, and the second thread in the file services process can continue to run when the first thread waits, and when the second thread blocks, the third thread can continue to perform, significantly improving the quality of the file service and the system throughput.

3. Own resources

Whether it is a traditional operating system or a threading operating system, a process is a separate unit of resources that can have its own resources. Generally, the thread itself does not own system resources (there is also a bit of an essential resource), but it can access the resources of its subordinate processes. That is, a process's code snippets, data segments, and system resources, such as open files, I/O devices, etc., can be shared by all other threads of a process.

4. System overhead

Because the system allocates or reclaims resources, such as memory space, I/O devices, and so on, when a process is created or revoked. As a result, the operating system will pay significantly more than the cost of creating or revoking a thread. Similarly, when the process is switched on, it involves the storage of the entire current process CPU environment and the setting of the CPU environment of the newly scheduled running process. Thread switching only needs to save and set the contents of a few registers, and does not involve the operation of memory management. It can be seen that the cost of process switching is much greater than the overhead of thread switching. In addition, because multiple threads in the same process have the same address space, the synchronization between them and the implementation of communication becomes easier. In some systems, thread switching, synchronization, and communication do not require the intervention of the operating system kernel.

Threads and processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.