Relationship between processes and threads in Windows

Source: Internet
Author: User

A process is the unit in which resources are allocated by the system. Each process corresponds to an active program. When a process is activated, the operating system allocates system resources, including memory, I/O, and CPU, for execution.
A thread is the unit of CPU allocation time. Each thread corresponds to a function in the process, that is, a code segment in the memory. When multiple threads are executed, the CPU allocates time based on their priority, enable them to complete their functions.
Generally, a process consists of at least one thread, one main thread, and other threads. Multiple Threads aim to share the CPU time slice to complete parallel tasks.
The following are my own documents:
Comparison of threads and processes:
A thread is a basic unit that can run independently less than a process. Generally, a process has several threads and at least one thread is required.
1. Scheduling
The basic unit of thread worker scheduling and dispatching. A process is the basic unit of resource ownership.
2. Concurrency
Concurrent execution can be performed between processes and between multiple threads in a process.
3. Have resources
A process is an independent unit that owns resources. A thread does not own system resources (and has a little less resources) but can access the resources of its processes.
4. System overhead
When creating or revoking a process, the system must allocate or recycle resources for it, such as memory space and I/O devices, the overhead of the OS is significantly greater than the overhead when the thread is created or withdrawn; the overhead of process switching is much higher than the overhead of thread switching.

A process is an application running in the system. A thread is the basic unit for the system to allocate processor time resources, or a unit that is executed independently within the process. For the operating system, the scheduling unit is a thread. A process includes at least one thread, which is usually called as the main thread. A process starts from the execution of the main thread and creates one or more additional threads.

What is the difference between a process and a thread? A process is an instance of the execution program. For example, when you run the upload program (nodepad.exe, you create a code used to form notepad.exe and the process that needs to call the dynamic link library. Each process runs in a dedicated and protected address space. Therefore, if you run two copies of notepad at the same time, the data used by the program is independent of each other in the respective instances. The data opened by the second instance of the program cannot be seen in a copy of notepad.

The sandbox is used as an example to describe. A process is like a sandbox. The thread is like a child in the sandbox. The children ran around in the sandbox, and may drag the sand into the eyes of other children. They would kick each other or bite each other. However, the difference between these sandboxes is that each sandbox is completely composed of walls.
Close to the ceiling, no matter how hard the children in the box are, they will not affect other children in the sandbox. Therefore, each process is like a protected sandbox. No one can go in or out without permission.

In fact, the thread runs but the process does not run. The only way for two processes to obtain dedicated data or memory is to share the memory block through the Protocol. This is a collaboration policy. Next let's analyze the process tab in the task manager.

The process here refers to a series of processes that are identified by the executable program instances they run. This is why the ing name is given in the first column of the Process tab. Note that there is no process name column. A process does not have a ing name independent of the instance to which it belongs. In other words
If you run 5 deployment scripts, you will see 5 processes called notepad.exe. How are they different from each other? One way is through their process ID, because each process has its unique encoding. The process ID is generated by Windows NT or Windows 2000 and can be used cyclically. Therefore, process IDs will not be larger and larger, and they can be recycled. The third column is the percentage of CPU time occupied by the threads in the process. It is not the number of the CPU, but the percentage of CPU time occupied by the process. At this time, my system is basically idle. Although the system seems to use only a small amount of CPU time every second or so, the idle process of the system still consumes about 99% of the CPU time.

The fourth column shows the CPU time, which is the hour, minute, And seconds occupied by threads in the process. Please note that I use the word "Occupy" for threads in the process. This does not necessarily mean that it is the total CPU time consumed by the process, because, as we will see later, the NT timing method is that when a specific clock interval is triggered, no matter who happens to be in the current thread, it will be calculated within the CPU cycle. Generally, the clock runs at 10 milliseconds in most NT systems. The heart beats every 10 ms of NT. Some driver code snippets run and show who is the current thread. Let's keep the last 10 milliseconds of CPU time in its account. Therefore, if a thread starts to run and is finished after 8 ms of continuous running, then the second thread starts to run and lasts for 2 ms, the clock is triggered, please guess which thread the entire 10 ms clock cycle is recorded in? The answer is the second thread. Therefore, there are some inherent inaccuracies in NT, and NT timing is in this way. The actual situation is also true. In most 32-bit operating systems, there is an interval-Based Timing mechanism. Remember this, because sometimes when you observe the total CPU usage of a thread, it may appear although the thread may have been running for more than 100,000 times, however, the CPU usage may be zero or very short-lived, so the above explanation is the reason. The above is the basic information column we can see in the process tab of the task manager.

According to the definition of the operating system, a process is the minimum unit of system resource management, and a thread is the minimum unit of program execution. The thread and process are very similar. The difference is that the thread is smaller than the process.

First, threads use the design idea that multiple threads can share resources. For example, most of their operations are performed in the same address space. Second, switching from one thread to another costs less than the process. Again, the information of the process itself occupies more space in the memory than the thread.

Therefore, threads can make better use of memory. A thread can be seen as a specified sequence of execution within a process. The biggest difference between a thread and a process is that the thread shares the same address space and runs on the same address.

A process is the unit in which resources are allocated by the system. Each process corresponds to an active program. When a process is activated, the operating system allocates system resources, including memory, I/O, and CPU, for execution.
A thread is the unit of CPU allocation time. Each thread corresponds to a function in the process, that is, a code segment in the memory. When multiple threads are executed, the CPU allocates time based on their priority, enable them to complete their functions.
A process is the basic unit for resource allocation and a thread is the basic unit for scheduling and execution.


 

 

 

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.