Talking about the process and multithreading

Talking about the processes and threads

1. Operating system development

Speaking of the process, first of all talk about the operating system .

Before computers were invented, people handle a large number of calculations are handled by manual, labor-intensive, and error costs a lot more. In order to handle a large number of mathematical problems, people invented the computer . The initial computer can only accept certain commands, the user enters a command, the computer will make an action. When you think about or enter, the computer waiting. Apparently so inefficient, in many cases, computers are in a wait state.

Improve efficiency - batch operating system

Can you need to operate a series of instructions written down, to form a list of one-time to the computer, the computer accordingly operation by constantly reading instruction? In this way, batch operating system was born. Users will need to perform multiple procedures are written on the disk, and then handed over to the computer to read and execute the programs one by one, and outputs the result written on another disk.

Although the birth of a batch operating system improves the ease of handling the task, but there is still a big problem :

If there are two tasks A and B, need to read large amounts of data input (I / O operations), but in fact only in the CPU wait state, and other tasks A finished reading the data in order to proceed, so wasted CPU resources. So people thought, whether in the process of the task A reading of data, so the task B to execute when the task is finished reading the data A, B pause task, so the task A continues?
This time I had a few problems : memory is always only one program is running, and want to solve the problem, will have to load multiple programs in memory, how to handle it? How to use the data to identify more than one program? When a program pause, and then how to restore it to the state before executing it?

Process of the invention:

Run the program generated process.

At this time, people invented the process, with the process corresponds to a program, each process corresponds to a certain memory address space , and can only use its own memory space, no interference between the various processes. The process also saved the program running each time for the process of switching possible. When the process is suspended, it will save the current state of the process (process ID, the process used resources, etc.), according to the next switch to restore previously saved state back, and then continue.

Modern operating systems are multi-process or single process? 多进程

2. The concept of concurrency

It allows the operating system to perform multiple tasks at the same time period from the macro looks. In other words, the process allows concurrent operation of decency as possible .

Although there are multiple concurrent tasks from a macro perspective in the implementation, but in fact, the single-core CPU for any particular moment only one task in CPU resources.

General operating system by CPU时间片轮转concurrency

thread

Thinking 1 : between modern operating systems in parallel or concurrent?

Parallel: Process the same time, the need to support multiple cpu.

Reflection 2 : Process to solve the problem?

  • CPU utilization is greatly improved

3. Why is there a thread

Real-time requirements:

Then came the process, the performance of the operating system has been greatly improved. Although the process appears to solve the problem of concurrent operating system, but people are not satisfied, and gradually to the real-time has been required. Because a process in a period of time can only do a thing, if a process multiple sub-tasks, sub-tasks can only be performed one of these, it is affecting the efficiency.

For example:

For this process monitoring systems: not only to communicate with the server-side image data acquisition and image information displayed on the screen, but also to deal with interaction with the user. If at one time the system is acquiring image data communication with the server, the user clicks a button on the surveillance system, the system can only interact, etc. After acquiring an image of the user. If you need to acquire an image 10s, the user must wait 10s. Obviously such a system can not meet people's needs.

Thread the invention:

That can not be separated from these subtasks enforce it? That is the moment the system is to obtain the server to communicate with the image data, and the user clicks a button on the surveillance system, then the system pause acquire images to interact (user operation is generally performed for a short time) with the user, and then continue to acquire image.
To handle this situation, people invented a thread, so that a thread executing a sub-task, such a process contains multiple threads, each responsible for a separate sub-tasks. When the user clicks the button, you can pause the thread acquiring image data, so that the CPU resources, allowing UI thread gets CPU resources, respond to user actions, complete response and then switch back to obtain image data of the thread to reacquire CPU resources. It makes the user feel the system to do many things at the same time to meet user requirements for real-time.

In other words: the process allows concurrent operating systems as possible, and let the internal processes concurrent threads as possible.

Case:

Class can be seen as a process.

No subtasks (wireless Cheng) [1] teacher in the class ---> Teachers want to drink water ----> Students must wait ---> teachers come back to school.

There subtasks (there are threads) [2] teacher in the class ---> teacher continued the lecture ----> teacher also continue to give lectures

​ | |

---> A classmate then water

Note: A process contains multiple threads, but these threads share the process memory address space and resources. The basic unit of the process is the operating system for resource allocation (interference between processes), and the thread is the basic unit of the operating system scheduling (each switch between threads).

4. The difference between processes and threads

A process is an independent (self contained) operating environment, it can be seen as a program or an application. The thread is a task carried out in the process.

The biggest difference is: whether alone occupy memory address space
(sharing, synchronization, reliability, cost)

1. process alone occupy a certain memory address space, so there is a memory isolation between processes, data is separate from the data sharing complex but simple synchronization , no interference between the various processes; and thread to share your process occupy memory address space and resources , data sharing simple, but complex synchronization.
2. The process alone occupy a certain memory address space, a process problem does not affect other processes, does not affect the stability of the main program, high reliability; a thread crash may affect the stability of the whole program, the reliability is low.
3. The process alone occupy a certain memory address space, creation and destruction process requires not only save the registers and stack information, but also need to allocate and recover page scheduling of resources, large overhead; threads only need to save registers and stack information, the smaller overhead .

The basic unit of the process is the operating system for resource allocation (interference between processes), and the thread is the basic unit of the operating system scheduling (each switch between threads).

The multi-process and multi-threaded

Why monitoring system has been proposed to use a process to achieve (multi-threaded) rather than multiple processes to achieve it?
A: The multi-process can also be achieved, but there is no thread communication simple process communication, multi-threaded want inside a shared resource (image data), so the whole becomes a process.

High process reliability but the big overhead, small thread overhead but low reliability. Select different models under different circumstances. In need concurrent processing and need to share some variables in the case selects multithreading. In other cases, a better process robustness.

Guess you like

Origin www.cnblogs.com/clown123456/p/11695045.html