More than a simple understanding CPU, multi-core, multi-process, multi-thread as well as processes and threads and differences

When faced with these problems, there are two key words can not be bypassed, and that is parallel and concurrent .

First of all, we must first understand a few concepts:

  1, a process is executing a program.

  2, the process is the basic unit of resource allocation.

  3, a process can include multiple threads.

  4, in a single CPU computer, a resource is not used by a plurality of parallel programs : CPU.

  5, the operating system scheduler: Split CPU running time slice a section, not to turn assigned the same procedure.

  6, the operating system memory management module: management of physical memory, virtual memory-related matters.

  Since the CPU can only execute a process with time, if we are not controlled, it is possible to use a process CPU until the end of the run, so there is a operating system scheduler, and the process has become a dispatch unit.

  Running processes requires not only CPU, but also a lot of other resources, such as memory ah, ah card, GPS, ah, ah disk, etc., collectively referred to as the execution environment program, which is the application context.

  Here arises the concept of concurrent scheduler to switch to a different speed CPU used by the process of very fast, so it seems in the user program is running at the same time, this is complicated by, but actually only running a CPU at the same time process. Concurrent just gives a false phenomenon, the reason is because the cpu to work quickly, feeling constantly loop occur simultaneously, actually is a single execution.

  The process can not share the same CPU time, but we must appear to share the CPU needs it? At this time, the concept of threads emerged. Thread is included in the process among different threads of a process share the CPU and program context. (Sharing of resources allocated to the process)

  CPU time for a single scheduling process, it is necessary to read the context + + context saving program execution , i.e., the process of switching.

  If this is a single-core CPU, then the different threads in the process in order to use CPU core, it will be a thread switch, but due to the shared program execution environment, the thread switching process switching overhead than a lot less. Here it is still complicated by the only core the same time can only execute one thread.

        The reason is because the process of switching thread requires a lot of resources to switch threads are still resources are shared, fast switching speed, small footprint, the content of the thread is privacy, to store some context information, and stack information, take up very little memory switch quickly, the same process in different threads to switch the process of sharing resources, quickly switch.

  If this is a multi-core CPU, then the process in different threads can use different core, true parallel appeared. Is a parallel multi-core, single core can only be said to be concurrent, parallel fake

  A thread is CPU scheduling and distribution of basic units , be sure to  process the operating system allocates resources (including cpu, memory, disk IO, etc.) is the smallest unit of  difference clear. There is a saying CPU can only see the thread, so to understand, I suppose CPU, I close my eyes, the operating system scheduler will assign a process to me after I got the process opened his eyes, what I see? I see many threads in the process, then what I now scheduling and allocation is? process? No, because I see no other process, how to schedule the distribution, scheduling those threads I can see, if I was a 4-core, then the thread assigned to the ABCD core 1234, other threads still have to wait for allocation, as the wait how long, how to allocate, they will not in the scope of this article. Then the CPU thread scheduling and allocation of the basic unit.

  Finally, talk about the operating system memory management module here to do: Before this, programmers need to arrange for space to run each program, space here refers to the physical address of the memory, but such a problem is that each program how to negotiate the use of the same memory space is different, but programmers should care about the underlying memory allocation problem. The solution is to put forward the concept of the process, each process using the same virtual address space, the CPU adds MMU module converts virtual addresses and physical addresses, virtual address after the operating system and MMU, virtual addresses are mapped to different physical address, different processes can be obtained independently of the physical memory space.

  Also in some operating systems, the process is not scheduling unit, the thread is the basic unit of scheduling, the scheduler schedules threads only, not the scheduling process, such as VxWorks.

  to sum up:

  1, a single process can only be complicated by the CPU, multi-CPU computer can process in parallel.

  2, a single core single CPU thread can concurrently, a single multi-core CPU in the parallel threads.

  3, either concurrent or parallel, the user point of view, see the multi-process, multi-threaded.

The fundamental difference: the process is the basic unit of the operating system resource allocation, and the thread is the basic unit of task scheduling and execution of

In terms of overhead: each process has its own code and data space (application context), switching between programs have larger overhead; thread can be seen as lightweight processes, threads share the same type of code and data space each thread has its own independent runtime stack and program counter (PC), small switches between threads overhead.

The environment: run multiple processes (programs) in the operating system at the same time; and there are multiple threads in the same process (program) simultaneously perform (by CPU scheduling, the film has only one execution thread at each time)

Memory allocation: When the system is running a different memory space will be allocated for each process; and thread, in addition to the CPU, the system will not (from that resource belongs to the process of resource thread used) to allocate memory for the thread can share resources, thread group.

Containment relationship: no thread can be seen as a single-threaded process, if there are multiple threads within a process, perform the procedure is not a line, but a plurality of lines (threads) together to complete; the thread is part of the process, so the thread is also known as light weight process or LWP.

 

Original articles published 0 · won praise 27 · views 80000 +

Guess you like

Origin blog.csdn.net/yimenglin/article/details/103567326