Processes and threads are a description of the time period and are the description of the CPU working time period.
here 's the background:
cpu+ram+ a variety of resources (such as graphics cards, optical drives, keyboards, GPS, and so on) constitute our computer, but the operation of the computer is actually CPU and related registers and RAM between the things.
one of the most basic facts : The CPU is too fast, too fast, too fast, the register can only catch up with his footsteps, Ram and other devices hanging on each bus is completely a candle. What happens when multiple tasks are to be executed? In turn? or who is the highest priority? No matter what kind of strategy, a word is the CPU seems to be taking turns.
A must know of the fact : the implementation of a program code, the implementation of a function of the process, when the CPU, the relevant resources must also be in place, that is, the graphics card ah, GPS ah what must be in place, and then the CPU began to execute. Everything except the CPU here is the execution environment of this program, which is the program context we define. When the program is finished, or the CPU allocated to him is running out of time, it is going to be switched out and waiting for the next CPU. The last step in being switched out is to save the program context, because this is the next time that he is run by the CPU and must be saved.
the fact of concatenation.: In front of the CPU it appears that all tasks are performed in a single rotation, with the specific rotation method:
load the context of program a first, then start executing A, save the context of program A, move to the next program context to execute program B, and then start execution B, save the context of program B ... .
========= important things came up ========
Processes and Threads
That's the background .
, two nouns are just a description of the corresponding CPU time period, which is the function of nouns.
- the process is the sum of the program execution time of the shifting context switch = CPU Load context +cpu execution +cpu Save context
What is a thread?
The granularity of the process is too large, each time to have the top and bottom of the call, save, recall. If we liken the process to a computer-run software, then the execution of a software can not be a logical execution, there must be more than one branch and a number of program segments, like to implement program A, is actually divided into a,b,c, such as a combination of multiple blocks. Then the concrete execution here can become:
Program A gets CPU = "CPU loading context, starts executing program A's a small segment, then executes A's B-segment, then executes the C-segment of a, and finally the CPU holds the context of a."
The execution of A,b,c here is to share the context of a, and the CPU does not have context switching when it executes. This
The a,b,c is a thread, which means that the thread is a much smaller CPU time period that shares the context of the process.
To this end of the full text, one more summary:
processes and threads are a description of the time period, which is the description of the CPU working time period, but the particle size is different.
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. Threads (Thread) is an entity of the process and is CPU the basic unit of 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.
Processes and Threads