Article Title: Server clinic: it is not just a thread. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
There are common misunderstandings about concurrency-Multi-processing. This month's "server clinic" will introduce basic concurrency concepts, which are required for your business to be securely carried out in server cabinets.
There are many misunderstandings about the name of Multi-processing. Most theoretical courses and many programming textbooks clearly explain the concept of concurrency, but concurrency is a very difficult topic, and almost all of us need to study.
Concurrency means that more than one "application" is running at a time. Here I reference the "application" because its meaning depends on the context. Linux Hosts always fill in a group of Programs (network protocol daemon, cron hypervisor, kernel itself, and more) that are executed more or less simultaneously in the host's progress table. Linux is a multi-task operating system. It is built for such a task.
On a typical single-processor host, tasks are not actually executed simultaneously. The part in the kernel, called the scheduler, switches work into and out, so that all work can be executed in one round. At the same time interval, your browser is downloading content, editing program source code, and playing music. Concurrency is often associated with such concurrent occurrence.
Concurrency
Remember, from the perspective of "User View" or "programming model", concurrency is about scheduling access to non-sharable resources. However, the opposite is concurrency in the sense of "backend. People looking for original performance focus on this different aspect. In their opinion, "Multi-processing" usually means dividing a single task into several parts, so that different central processing units (CPUs) can work together to complete the task. The idea is to complete a job in a shorter period of time at the pace of an external clock, even at the cost of more complex hardware and programming.
Both aspects of concurrency are related to scheduling, that is, assigning tasks to the CPU. Both of them affect availability. However, obfuscation of these two aspects is a common and tricky mistake. Programming beginners seem to be particularly superstitious about one of the most important concurrency methods-"multithreading ". "Multithreading" is often referred to as "thread-based", and its false impressions include the following ideas:
Thread-based program execution is faster.
Threadization is the unique concurrency structure or the only feasible concurrency structure.
The speed of N hosts is about N times that of a single processing host.
These incorrect ideas can be quickly corrected by a little clarification of the concept.
Naive developers often ask, "my program is too slow. How can I speed up program running through threadization ?" The answer is usually, "you cannot do it ." Simply converting an existing single-task application into multiple tasks always requires more computing. In general, "threading" such a program will take the program for a longer time.
Such a false statement will survive for a long time, and of course there is a reason. Many programs can be divided into several parts to reduce the bottleneck. Computing-intensive work-for example, the simulation of the shuttle's return to the atmosphere-is distributed to eight CPUs rather than one CPU, which is much faster. It is more common to refactor a program to avoid "blocking" of input/output (input/output, I/O ". If your consumer-level application can do some useful work while waiting for keyboard input, data to be transferred from the disk, or waiting for messages sent over the network, it looks like "increasing the speed" at no cost.
Limitations of threading
However, it is believed that threading will bring about these acceleration risks. Such acceleration relies on deeper analysis. It is possible to increase the speed only when resources that are not fully utilized are available. In addition, threading is not the only way to implement these concurrency, and is often not the best way.
Academic literature has studied at least 12 very important concurrency models that have been put into practical use. In addition to hearing about threading, you may also hear about excessive processing (in the program sense), collaborative routines, and event-based programming, or you may have heard of continuation) generator and several more mysterious structures. For example, if you have a language that supports generators but does not support threads, you can use generators to simulate threads (and vice versa). In this sense, all the above methods have a similar form.
However, writing appropriate programs is different from abstract equivalence. There are indeed differences between concurrency models when you try to deliver reliable applications on schedule. For example, threads have been known for many years. Threads are equivalent to low-level programming structures. It is difficult to write programs safely with threads. Programs that require thread control can easily cause inconsistent data, deadlocks, unscalable locks, and inverted priorities. Java initially intended to support only multithreading and use it as the core concept of concurrency. Recently, Java gave up this plan due to thread-based performance problems. The large debugger overhead for debugging threads has long been notorious.
However, it is not all bad news. If you spend some time understanding some basic concepts, the use of threads is as reliable as using XML or LDAP or in any other specialized field. More directly, it is safer for many situations-sometimes faster! -Concurrency model.
In many cases, the best way to turn an application into a multi-task is to break it into multiple cooperative processes rather than threads. Programmers usually refuse to accept this reality. One of the reasons is historical: The process used to be much heavier than the thread, and is still the case in most versions of Windows. However, in modern Linux, context switching between processes takes only 15% more time than Context switching between threads of the same process. The return of time consumption is a more profound and robust programming model. Many programmers can write secure independent processes. Few programmers can write secure threads.
When is it more suitable to adopt multi-process instead of multi-thread? For example, if you have a "Control Panel" graphical user interface (GUI ), it monitors the results of several large computations, retrieves and updates database records, and may even report the status of external physical devices. You can place all these tasks in one process and have an independent thread for each task. In Windows, this is often a preferred practice.
However, in my development practices, I usually put each task in its own process and communicate with each other through sockets, pipelines, or memory shared from time to time. Since you can use all commonly used command line tools to automate independent processes, the above approach greatly simplifies unit testing. The crash of a process does not endanger any other process. Its performance is usually comparable to that of multithreading, and sometimes better, depending on the specific hardware and programming conditions.
Other concurrency Models
Such a multi-process implementation often relies on event-based programming. Events are a different concept of concurrency and are useful for managing I/O and related multi-task responsibilities. Events associate asynchronous "External Actions" with programming callbacks (also called signals, bindings, etc. Think about the GUI Control Panel mentioned above. When writing this program in Unix, a high-performance approach is to update the display only when the select () system calls detect the data that has arrived. Programmers who use C often use select to mark event-based methods.
You may regard "collaborative routines" or "GENERATORS" as content outside the classroom. However, they have been built into the definition of languages such as Modula and Icon, because they make multitasking programming very powerful while making it easy to understand (and thus still safe ). If you have complex performance requirements, if your application models the best in the form of hundreds of sub-tasks, especially if your server room contains a large number of multi-host, then you should study a wider range of concurrency models. You will find that each model has its most suitable situation. A concurrency model may be suitable for your needs.
At the same time, be aware that you may want to seek support for any model you want to use in Linux. The following references point to the implementation and experiment of various concurrent models, as well as other references.
Multiple challenges
Note: Do not assume that your multitasking software runs well on your multi-processing (often called symmetric multiprocessing (SMP) hardware. Especially for earlier Linux versions, it is often necessary to use professional knowledge to obtain useful results from SMP machines. When multiple (up to four, sometimes more) processors are used to process different processes, the default installation of Linux 2.4 can be well done. However, multiple threads in a process may cause bottlenecks on a single processor, while other processors are idle. Other concurrent methods sometimes cause similar problems.
To avoid wasting these resources depends on the specific situation of your platform. With Linux 2.4 and popular multi-channel hardware, You can reasonably expect to use the default "kernel thread" (see the Linux thread FAQ in the references) to properly schedule threads, the scheduling thread allocates these threads to different CPUs. Use top and other system management tools to verify that scheduling is correct. For details about the actual use of thread scheduling, please contact your Linux supplier or user group.
In most of your programming work, you may naturally break down programs into different logical tasks. By clearly understanding the basic concurrency concepts, you can apply them to meet your own requirements. Remember that concurrency is both forward-oriented and back-oriented: "User View" or "Programming Model" controls how you interact with applications, the "backend" manages the jobs assigned to hardware. Your features and performance requirements are strictly divided. Finally, remember that speaking of concurrency is not just a thread. By using a model that is clearly not multi-threaded, you can often make full use of your server.
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