[Reprint] programming ideas as much as a thread and multi-process (1) - to tell the operating system point of view threads and processes

Programming ideas as much as a thread and multi-process (1) - to tell the operating system point of view threads and processes
original luoweifu Posted on 2015-06-22 20:05:28 reading number 75442 Collection
launched

Original: HTTP: // blog.csdn.net/luoweifu/article/details/46595285 
Author: luoweifu 
reprint please mark the name of the source 

in fact, I have a very clear place a process should only exist in a nuclear above it 

a process multiple threads should not cross the CPU core work, right ? 

not quite understand.

 


What is the thread
What is a thread? Threads and processes to have anything to do? This is a very abstract issue, but also a particularly broad topic, involving a lot of knowledge. I'm not sure that it can say, and there is no guarantee about the contents of all correct. Even so, I also want him as much as possible to speak plainly, speaking understand that, because this is has been bothering me for a long time, bewildering field of knowledge, and hope that through my understanding uncover its mysterious veil layer by layer.

 

Task scheduling
What threads are? To understand this concept, we need to first look at some of the concepts of the operating system. Most operating systems (such as Windows, Linux) task scheduling is a preemptive round-robin scheduling, that a short time after performing a task force a break to the next task, each task in turn execute. A short time task execution is called the time slice, the state of the task being performed called running, force a break to the next task after task execution time, the suspended task is in the ready state waiting for the time slice belonging to it of a arrival. So that each task can be implemented, due to the very high efficiency of CPU time slice is very short, quickly switch between tasks, gives the impression that multiple tasks at the "same time", which is what we are He said concurrency (do not feel how advanced concurrency, its implementation is complex, but its concept is simple, in one sentence: to perform multiple tasks at the same time). Schematic multitasking process are as follows:

 

Figure 1: operating system task scheduling

 

 

Course
we all know that is the core of the computer CPU, it assumed all computing tasks; while the operating system is a computer administrator, which is responsible for scheduling, resource allocation and management tasks, commanding the entire computer hardware; the application side is having a functions of the program, the program is running on the operating system.

Process is a certain function independent of a dynamic procedure performed on a data set, the operating system is an independent unit of resource allocation and scheduling, is the carrier of applications running. Process is an abstract concept, never uniform standard definition. Usually the process is controlled by the three-part program block, the data collection process and composition. Procedure is used to describe the process functions to be performed, the control process is executed by the instruction set; data set is necessary to execute the program data and a work area; Program Control Block (Program Control Block, referred to the PCB), the description of the process comprising and control information, is the only sign of the presence of the process.

The process has the characteristics:

Dynamic: the process is a process of implementation of the program, it is temporary, has its lifetime, is dynamically generated, dynamic extinction;

Concurrency: any process can execute concurrently together with other processes;

Independence: the system process is an independent unit of resource allocation and scheduling;

Structural: process control block of three parts by a program, data and processes.

 

Thread
in the earlier operating system and no concept of threads, the process is the smallest unit can operate independently and have the resources, but also the smallest unit of program execution. Preemptive task scheduling uses a round-robin scheduler in the embodiment, and the process is the smallest unit of scheduling, each process has an independent memory, so that the memory addresses between the respective processes isolated.

Later, with the development of computers, CPU requirements increasingly high cost of switching between processes is large, has been unable to meet the requirements of the increasingly complex procedures. Thus invented threads, which is a program executed in a single sequential flow of control, is the minimum unit of the program execution flow, the processor is a basic unit of scheduling and dispatching. A process can have one or more threads, memory sharing program between threads (that is, where the process memory space). A standard thread by the thread ID, the current instruction pointer (PC), registers and stack components. And the process by the memory space (code, data, process space, open files) and one or more of threads.

 

 

The difference between processes and threads
speaking in front of the processes and threads, but you may also feel confused, they feel very similar. Indeed, process and thread are inextricably linked, let us reason together for a reason:

1. A thread is the smallest unit of program execution, and the process is the smallest unit of the operating system of allocating resources;

2. A process of one or more of threads, the thread is a different process routes execution code;

3. The processes are independent, but the program memory space shared between threads in the same process (including code segments, data sets, stack, etc.) and some of the process-level resources (such as open files and signals), within a process the thread is not visible in the other process;

4. Scheduling and Switching: thread context switching is much faster than the process context switch.

 

Schematic diagram of the relationship between threads and processes:

 

Figure 2: Resource Sharing relationship processes and threads

 

 

Figure 3: Relationship single-threaded and multi-threaded

 

In short, threads and processes is an abstract concept, is a thread smaller than the process of abstraction, threads and processes can be used to achieve concurrency.

 

In earlier operating systems, and no concept of threads, the process is the smallest unit can operate independently and have the resources, but also the smallest unit of program execution. It is equivalent to only one thread in a process, the process itself is a thread. So threads are sometimes called lightweight processes (Lightweight Process, LWP).

 

Figure 4: The earlier operating system processes only, no thread

Later, with the development of computers, the efficiency of context switching between multiple tasks have become increasingly demanding, it is a less abstract concept - a thread, a process usually have more (but also a) thread.

 

Figure 5: The emergence of thread, so that a process can have multiple threads

 

Multi-core multi-thread
time slice of the above-mentioned rotation, said scheduling tasks after a short time forced to suspend execution to perform the next task, each task in turn executed. Many operating systems of the books say "the same point in time there is only one task to perform." That some people might want to ask what dual-core processor? Do not run it at the same time two nuclear?

In fact, "the same point in time there is only one task to perform" This sentence is not accurate, at least it is not comprehensive. That case of multi-core processors, the thread is how to implement it? This requires understanding of kernel threads.

Multi-core (core) processor means a plurality of processor cores integrated on a single processor, thereby increasing computing power, i.e. a plurality of parallel computing real processing cores, each corresponding to a kernel thread processing core. Kernel thread (Kernel Thread, KLT) is directly supported by the operating system kernel thread, the kernel thread such thread switching is done, the kernel thread by the schedulers to schedule the thread and is responsible for mapping the tasks to the respective processor . A processing core generally corresponds to a kernel thread, such as a single core processor corresponding to a kernel thread, two dual-core processors corresponding to kernel threads, corresponding to the four quad-core processor kernel thread.

Now the computer typically four threads, four eight-core thread, is the use of a physical Hyper-Threading Technology analog processing core into two logical processing cores, two cores corresponding to the threads, the number of CPU seen in the actual operating system twice the number of physical CPU, such as your computer is a dual-core four threads, open the "task Manager \ performance" can be seen four CPU monitor, quad-core eight threads can see eight CPU monitor.

 

Figure 6: Results of four threads View in Windows8

Hyper-Threading Technology is the use of special hardware instructions, the simulation of a physical chip into two logical processing cores, so that a single processor can be calculated using the thread-level parallelism, and thus compatible with multi-threading operating systems and software, reduces CPU idle time, improve the operational efficiency of the CPU. This hyper-threading technology (such as dual nuclear power plant thread) is determined by the processor hardware, but also need the support of the operating system in order to show it in the computer.

 

Programs generally do not go directly to the kernel threads, but to use an advanced interface to kernel threads - LWP (Light Weight Process, LWP), lightweight process that we talked about in the usual sense of the thread (we in this call user thread), since each lightweight process consists of a kernel thread support, so only the first support kernel threads, in order to have a lightweight process. Correspondence between user threads and kernel threads, there are three models: one model, many-to-model-many model, three models is described in this kernel threads to four, three user threads, for example.

One model
for one model, one user thread uniquely corresponds to a kernel thread (not necessarily vice versa, there is a kernel thread does not necessarily correspond to the user thread). Thus, if the CPU is not using Hyper-Threading Technology (four threads, such as four computer), a user thread is uniquely mapped to a physical CPU thread between concurrent threads is true concurrency. One model allows the user threads and kernel threads have the same advantages, a thread execution is not affected due to blocking some reason other threads; here, one model also allows multi-threaded programs in multiprocessor systems on better performance.

However, one model also has two drawbacks: 1. Many operating systems limit the number of kernel threads, the number one cause of the user thread model is limited; 2 many operating system kernel thread scheduling, context switching overhead larger, resulting in the efficiency of the user thread decreases.

 

 

Figure 7: one model

 

 

Many-to model
many-to-model mapping to the plurality of user threads a kernel thread, the thread switching between the user mode is performed by the code, and thus the switching speed of the thread opposite one model, many-to-model faster many; in addition, the number of many-to-user model thread is almost unlimited. But many-to-model also has two disadvantages: 1 If a user thread is blocked, then all other threads will not be executed, because the kernel threads also will be blocked; 2 on multiprocessor systems, processor. there will be no significant increase in the number of increased many-thread performance model, because all user threads are mapped to a processor of.

 

Figure 8: Many Model

 

Model many-
to-many model combines the advantages of models and many-to-one model, mapping the plurality of user threads to a plurality of kernel threads. Model-many advantages are: 1 a blocked user thread does not result in the blocking of all threads, because at this time there are other kernel threads are scheduled to perform; 2-many model is no limit on the number of user threads. ;. 3 multiprocessor operating system, the thread-many models can be obtained to some performance improvements, but not as good as one model to enhance the range of high.

In the now popular operating systems, mostly using the model many to many.

 

Figure 9: Model-many

 

 

View processes and threads
an application may be multi-threaded, multi-process may be, how to view it? We have only to open the Windows Task Manager will be able to view the process of an application and the number of threads. Press "Ctrl + Alt + Del" or the context-sensitive toolbar to open the Task Manager.

View the number of processes and threads:

 

Figure 10: View the number of threads or processes

In the "process" tab, we can see a number of threads contained in the application. If an application has multiple processes, we can see each process, such as in the figure above, Google's chrome browser there is more than one process. At the same time, if you open multiple instances of an application will have multiple processes, such as the image above I opened two cmd window, there are two cmd processes. If you do not see the number of threads this column, you can click on "View \ Select Columns" menu, increase listening column.

 

View CPU and memory usage:

On the Performance tab, we can see the CPU and memory usage, but also see the number of logical processing cores, four threads as my computer monitor, there are four records in accordance with the number of CPU usage monitor device.

 

Figure 11: View CPU and memory usage

 

 

Thread of the life cycle
when the number of processors is less than the number of threads, concurrent threads is true concurrency, different threads running on different processors. But when the thread is greater than the number of processors, thread concurrency will be some obstacles, this time not a real concurrency, because at this time there is at least one processor runs multiple threads.

When a single processor to run multiple threads, concurrency is a state of the simulated. Operating system uses round-robin manner execute each thread turn. Now, almost all modern operating systems are used in a preemptive way round-robin scheduling, such as we are familiar with Unix, Linux, Windows and Mac OS X and other popular operating systems.

We know that the thread is the smallest unit of program execution, it is the smallest unit of task execution. In earlier operating systems, only the process, the process has five states, create, ready, running, blocking (waiting) to exit. Early in the process the equivalent of only a single thread process now, so now there are five states multi-threaded, multi-threaded and now the life cycle of the life cycle of the process is similar to earlier.

 

Figure 12: Early Life Cycle process

 

During operation process has three states: ready, running, blocking, and create exit status describes the process of creating and exit procedure process.

Created: process is created, it can not run. When you create a working operating system process to be undertaken, including the allocation and establish process control block entry form and establish resource allocation of resources, and the establishment loader address space;

Ready: time slice has run out, this thread is forced to pause, waiting for the next part of his time slice arrival;

Run: This thread is executing, that is occupying the time slice;

Obstruction: Also called a wait state, waiting for an event (such as IO or another thread) execution completed;

Exit: The process has ended, so also known as the end of the state, the release of the operating system resource allocation.

 

Figure 13: Life Cycle thread

 

Create: a new thread is created, the thread is waiting to be called to perform;

Ready: time slice has run out, this thread is forced to pause, waiting for the next part of his time slice arrival;

Run: This thread is executing, that is occupying the time slice;

Obstruction: Also called a wait state, waiting for an event (such as IO or another thread) execution completed;

Exit: a thread to complete the task or other termination condition occurs, the thread termination state entry and exit, the exit status of the thread releases the resources allocated.

 

Threads and processes related articles:

Programming ideas as much as a thread and multi-process (2) - thread priority thread safety

Programming ideas as much as a thread and multi-process (3) - Multithreading in Java

Programming ideas as much as a thread and multi-process (4) - multithreading in C ++

 


Original: http: //blog.csdn.net/luoweifu/article/details/46595285 
Author: luoweifu 
reprint please mark the name of the source

 

======================== programming ideas ===================== series review ===


Message mechanism of programming ideas

Logging of programming ideas

Exception handling of programming ideas

Positive programming ideas of expression

Iterator of programming ideas

Recursive Programming Ideas

Callback programming Thought

 

[Attention] SunLogging 
 
press and sweep the yard or two-dimensional code, the phone side Read more


----------------
Disclaimer: This article is CSDN blogger "luoweifu 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/luoweifu/article/details/46595285

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12165989.html