What is the difference between a process and a thread

Source: Internet
Author: User

1. Processes and Threads

1.1 Overview:

A process is a program with a certain independent function about a single run activity on a data set , a process that is an independent unit of the system's resource allocation and scheduling.

A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. The thread itself basically does not own the system resources, only has a point in the operation of the necessary resources (such as program counters, a set of registers and stacks), However, it can share all of the resources owned by the process with other threads that belong to one process.

One thread can create and revoke another thread , and can execute concurrently between multiple threads in the same process.

Relative to a process, a thread is a concept that is closer to the execution body, it can share data with other threads in the process, but has its own stack space and has a separate execution sequence.

Threads and processes are introduced on the basis of serial programs to improve the concurrency of programs, thus improving program efficiency and response time.

1.2 Differences:

The main difference between processes and threads is that they are different ways to manage operating system resources. The process has a separate address space, and after a process crashes, it does not affect other processes in protected mode, and the thread is just a different execution path in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, a thread dead is equal to the entire process dead, so the multi-process program is more robust than multithreaded programs, but in the process of switching, the cost of large resources, efficiency is worse. but for some concurrent operations that require simultaneous and shared variables, only threads can be used, and processes cannot be used.

1) In short, a program has at least one process, and a process has at least one thread.

2) The thread partition scale is smaller than the process, which makes the multi-thread procedure high concurrency.

3) In addition, the process has a separate memory unit during execution, while multiple threads share memory, which greatly improves the efficiency of the program operation.

4) threads are still different from the process in the execution process. Each separate thread has a program run entry, sequence of sequence execution, and exit of the program. However, threads cannot be executed independently, and must be dependent on the application, which provides multiple threads of execution control.

5) from a logical point of view, the meaning of multithreading is that in an application, there are multiple execution parts that can be executed concurrently. However, the operating system does not consider multiple threads as separate applications to implement scheduling and management of processes and resource allocation. This is the important difference between processes and threads.

1.3 Advantages and disadvantages:

Threads and processes have advantages and disadvantages in use: the overhead of thread execution is small but not conducive to the management and protection of resources, while the process is the opposite. At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines.

2. Multi-process, multi-threaded

2.1 Overview:

Process is a program run by the CPU abstraction, a program run is abstracted as a process, but the thread is separated from a process, because the CPU processing process is the use of time slice rotation, so a large process to split into multiple threads, For example: The documents in the Internet Express are divided into 100 parts, 10 thread files are divided into 10 parts to download at the same time 1-10 accounted for a thread 11-20 accounted for a thread, and so on, the more threads, the more files are divided, and download of course the faster the speed

a process is an execution activity on a computer by a program. When you run a program, you start a process. Obviously, a program is just an ordered set of instructions, and it doesn't have any meaning to run, it's just a static entity. The process is different, it is the execution of a program on a data set, is a dynamic entity. It is created because it is scheduled to run, waiting for a resource or event to be in the waiting state, because the completion of the task is undone, reflecting a program on a certain set of data run on the entire dynamic process. a process is the unit in which the operating system allocates resources. under Windows, the process is then refined to a thread, which means that there are several smaller units under a process that can run independently. A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch. threads cannot be executed independently, must be dependent on the application, and the application provides multiple threads for execution control.

the relationship between threads and processes is that threads are part of a process, threads run in process space, threads generated by the same process share the same memory space, and threads generated by the process are forced to exit and clear when the process exits. Threads can share all the resources owned by a process with other threads that belong to the same process, but they do not inherently have system resources and have only a bit of information that is essential in the run (such as program counters, a set of registers, and stacks).

At the same time, if two or more processes are allowed to run in the same computer system, this is multitasking. Modern operating systems are almost always multitasking operating systems that can manage multiple processes at the same time. the benefits of multitasking are obvious, such as you can listen to the Internet while listening to MP3, and even print the downloaded documents without interfering with each other. So here is the question of parallelism, as the saying goes, one cannot use two, this is the same for computers, in principle a CPU can only be assigned to a process in order to run the process. We usually use a computer with only one CPU, that is, only one heart, to make it multitasking, running multiple processes at the same time, you must use concurrency technology. The implementation of concurrency technology is quite complex, the most easy to understand is "time slice rotation process scheduling algorithm", its ideas are briefly described as follows: Under the management of the operating system, all running processes take turns using the CPU, each process is allowed to take up CPU time is very short (such as 10 milliseconds), This way the user does not feel that the CPU is in turn serving multiple processes as if all the processes are running continuously. But there is actually only one process that occupies the CPU at any one time.

If a computer has multiple CPUs, the situation is different, if the number of processes is less than the number of CPUs, then different processes can be assigned to different CPUs to run, so that multiple processes are really running concurrently, which is parallel. However, if the number of processes is greater than the number of CPUs, concurrency technology is still required.

in the in Windows, where CPU allocation is done in threads , a process may be composed of multiple threads, and the situation is more complicated, but simply, there are the following relationships:

Number of bus <= CPUs: running in parallel

Number of bus threads > number of CPUs: concurrently running

The efficiency of parallel operation is obviously higher than that of concurrency, so multitasking is more efficient in multi- CPU computers. However, if you run only one process (thread) in a multi-CPU computer, you will not be able to take advantage of multiple CPUs.

The rationale for a multitasking operating system (such as Windows) is that the operating system allocates the CPU's time slices to multiple threads, each of which is completed within the time slice specified by the operating system (note that multiple threads here are part of a different process). The operating system constantly switches from one thread to another execution of the thread , and so on, on the macroscopic level, it seems as if multiple threads are executing together. Because these threads are separate processes, it seems to us that multiple processes are executing simultaneously, so multitasking is achieved.

2.2 Classification

Depending on the process and thread settings, the operating system is broadly divided into the following types:

(1) Single-process, single-threaded, MS-DOS is roughly the operating system;

(2) Multi-process, single-threaded, most Unix (and Unix-like Linux) is this operating system;

(3) Multi-process, multi-threading, Win32 (Windows nt/2000/xp, etc.), Solaris 2.x and OS/2 are the operating systems;

(4) Single-process, multi-threading, VxWorks is this operating system.

2.3 Key benefits of introducing threading:

(1) In-process creation, termination line turndown Create, terminate the process faster;

(2) Switching between threads within the same process is faster than switching between processes, especially between user-level threads.

What is the difference between a process and a thread

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.