Definitions, relationships, and differences between processes and threads

Source: Internet
Author: User

Definitions, relationships, and differences between processes and threads

--Reference blog post:http://blog.csdn.net/yanxiaolx/article/details/51763372

I. Definition OF the process

Process: The basic unit that can run independently and be allocated as a resource in a system, which consists of a set of machine instructions, data and stacks, and is an active entity that can run independently.

The process generally has three states: ready state, execution state and wait state "or" blocking state "; the process can only be established by the parent process, and all processes in the system form a hierarchy of process trees; The suspend command can be issued by the process itself and other processes, but the release of the suspend command can only be emitted by other processes.

Process Control block (PCB): The PCB can not only record the process's attribute information, so that the operating system to control and manage the process, and the PCB marks the existence of the process, the operating system according to whether the process of Process control block PCB and know if the process exists or not. The system establishes the process of the PCB at the same time, in the revocation of a process, it will revoke its PCB, so the process of the PCB to the process is its existence of the specific physical signs and manifests. The general PCB includes the following three types of information: Process identification information, processor status information, process Control information.

The process entity (also called process image) is constituted by the program section, the related Data section and the PCB, in general, we take the process entity as the process.

Characteristics of the process:

1. Dynamic: The essence of the process is a process of execution, the process is dynamic generation, the dynamic extinction.
2. Concurrency: Any process can execute concurrently with other processes.
3. Independence: The process is a unit that can run independently, but also the system allocates resources and scheduling independent units.
4. Asynchrony: Because of the inter-process constraints, the process has a discontinuity of execution, that is, the process is moving forward at its own independent, unpredictable speed.

Ii. Definition of threads

Thread: A thread is an entity in a process that acts as the basic unit for system Dispatch and dispatch.

The nature of the thread:

1. A thread is a relatively independent executable unit within a process. If the process is called a task, then the thread is the execution of a subtask in the application.
2. Because the thread is the basic unit being dispatched, the process is not a dispatch unit. Therefore, at least one thread must be created for the process at a time when each process is created. That is, there must be at least one or more threads in the process, or the process cannot be scheduled to execute.
3. A process is a basic unit of resources that is allocated and owned. Multiple threads within the same process share the resources of the process, but the threads do not own resources, just use them.
4. The thread is the basic dispatch unit in the operating system, so the thread should contain the necessary information needed for scheduling and have a state change in the life cycle.
5. Because shared resources "include data and files", communication and synchronization mechanisms are required between threads, and threads can create other threads when required, but there is no parent-child relationship between threads.

Situations where multithreading is used:

Front desk and backstage work situation;

asynchronous processing of work;

The speed of implementation needs to be accelerated;

The situation of organizing complex work;

There are multiple User Service requests in the same situation.

Advantages of the threading mechanism:

Multithreading runs within the same address space of the same process, and has the following advantages over multiple processes:

1. The overhead of creating and revoking threads is less than the process. When creating a thread, you only need to establish the appropriate table of the thread control table, or about the queue, while creating the process, you create the PCB table and initialize it, enter about the process queue, establish its address space and the required resources, and so on.
2.CPU switches between threads are much less expensive than processes. Because the switch threads are all in the same address space, only the thread control table or queue needs to be modified, and no address space or other work is involved.
3. The threading mechanism also increases the effectiveness of the communication. Inter-process communication often requires the participation of the kernel to provide communication mechanism and protection mechanism, while the communication between threads is in the address space of the same process, sharing main memory and files without kernel involvement.

Iii. the relationship between process and thread

In a sense, a process is a process that an application performs on a processing machine, which is a dynamic concept, and a thread is a part of a process that contains multiple threads running.

In the operating system in which threading is introduced, the process is usually used as the basic unit for allocating resources , while threads are used as the basic unit of independent operation and independent dispatch . Because the thread is smaller than the process, and basically does not have the system resources, the cost of its scheduling will be much smaller, can be more efficient to improve the degree of concurrent execution among the multiple programs within the system.

To illustrate:

Process: Beijing Metro Unit

Threads: Beijing metro line Line 1, line Line 2, line Line 5 ..... Line Line 15

All subway lines in Beijing Metro unit share all the tourists from Beijing Metro (equivalent to the resources in the process, so the process belongs to the resource unit), and each line is responsible for transporting the source to the designated place (performing operations, so the thread belongs to the executing unit).

  Iv. The difference between a process and a thread 1. Dispatch:

        in traditional operating systems, the basic unit of CPU dispatch and dispatch is the process. In the introduction of the threading operating system, the thread as the basic unit of CPU dispatch and dispatch, the process as the basic unit of resource ownership, so that the traditional process of two attributes separate, thread programming light running, which can significantly improve the concurrency of the system. Switching between threads in the same process does not cause the process to switch, thus avoiding expensive system calls, but switching to a thread in another process by a thread in one process still causes the process to switch.
 
2, Concurrency:
      in an operating system that introduces threads, not only can the processes be executed concurrently, but also can be executed concurrently between multiple threads in a process. Thus, the operating system has better concurrency, which can improve the throughput of system resources and system more effectively. For example, in a single-CPU operating system that introduces 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 the operating system in which threading is introduced, you can set up multiple service threads in a file service process. When the first thread waits, the second thread in the file service process can continue to run, and when the second thread is blocked, the third thread can continue to execute, significantly improving the quality of the file service and the throughput of the system.

3, owning resources:
      Whether it's a thread-introducing operating system or a traditional operating system, a process is a separate unit with system resources, and he can have his own resources. Generally, a thread cannot own resources (and 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.) that can be shared by all other threads of the same process.

4, Independence:
        The independence of different threads in the same process is much lower than the independence between different processes. This is because the To prevent interference and destruction between processes, each process has a separate address space and other resources that do not allow access to other processes in addition to sharing global variables. However, different threads in the same process are often created to increase concurrency and co-operate with each other, sharing the memory address space and resources of the process, such as every thread can access all addresses in the address space of the process to which they belong, such as a thread's stack that can be read, written, or even completely erased by other threads.

5. System Overhead:

Because a process is created or revoked, the system allocates or reclaims resources, such as memory space, I/O devices, and so on. As a result, the operating system is paying significantly more for this than the cost of creating or revoking a thread. When the process is switched on, it involves the setting of the environment of the entire current process CPU environment and the setting of the new CPU environment that is scheduled to run, while the thread switching only saves and sets the contents of a small number of registers, and does not involve the operation of memory management, so the cost of process switching is much larger than the cost of thread switching. In addition, because multiple threads in the same process have the same address space, the synchronization and communication implementations between them become easier. In some systems, off-the-shelf switching, synchronization, and communication do not require the intervention of the operating system kernel.

6, Support multiprocessor system:

in multiprocessor system, for a traditional process, a single-threaded process, no matter how many processors, the process can only be run on a single processing machine. However, for multithreaded processes, multiple threads in a process can be assigned to multiple processing machines, allowing them to execute in parallel, which will undoubtedly accelerate the completion of the process. As a result, the modern processor OS has introduced multithreading without exception.

Definitions, relationships, and differences between processes and threads

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.