Server clinics: use and send increase speed
English original
content:
Concurrent two aspects of threading limitations other concurrent models multi-way difficulties reference author introduction to this article
related information:
1.Expect exceeds expectation 2. A must-have book for Linux developers 3. Application deployment 4. Lightweight web technology 5. Run: context switch 6. Cute Python: Based-based state machine 7. Generation POSIX thread
In the Linux area:
Tutorial Tools & Product Codes & Component Articles
Not just threaded Cameron laird (claird@phaseit.net) Vice President, Phaseit, Inc. 2002 August
It is generally misunderstandings. The "Server Clinic" of this month will introduce the basic concurrency concept, in order to make your business safely in the server cabinet, you need these concurrent concepts.
There are many misunderstandings for people to process this name. Most theoretical courses and many programming textbooks clearly explain the concurrency concept, but it is a difficult theme, almost all of us need training. And refer to the situation where there is more than one "application" running. Here I refer to "Application" is because it is dependent on the context. Linux hosts always put a group of more or less executable programs (network protocol daemon, cron manager, and often more often more) in the process table of the host. Linux is a multitasking operating system. It is built for such tasks. On a typical single processor host, the task is actually not executed simultaneously. The part called the scheduler is referred to as a part of the operation, so that all work is performed. In the same time interval, your browser is downloading, and at the same time you are editing the program source code, you are still playing music. It is often associated with this simultaneous appearance. Two aspects of concurrency, remember, from the perspective of "User View" or "Programming Model", concurrent is about the dispatch to an invisible resource. However, it is compared with it in the sense of "backend". People seeking primitive performance focus on this different aspect. In their view, "multiprocessing" means that a single task is divided into several parts, making different central processing units (CPUs) can work with the task. The idea is to complete a job in a shorter time in a short time in the pace of external clock, even in more complex hardware and programming. Both of concurrency with scheduling, so that the task is allocated to the CPU. Both affect availability. However, it is confusing these two aspects but is a difficult mistake of people. The people who plan to programs seem to be particularly prone to one of the most important concurrency methods - "Multi-Thread". "Multithread" is often referred to as "thread", and it includes the following ideas for it:
Threading makes the program faster. Threading is the only concurrent structure, or the only constructed concurrent configuration. The N-road host is approximately N times from the single-handed host. As long as you make a point in clarification, you can quickly correct these error ideas. Tianzhen developers often ask, "My program is too slow; how can I use threaded to speed up the run?" The answer is usually, "can't do." Simple in existing single task applications. Conversion to divide it into several multitasking parts that will always lead to more calculations. In general, "threading such a program will make the program for a longer time. This mistake will exist for a long time, of course, there is a reason. Many programs can be broken down into several parts, thereby reducing bottleneck problems. Put the intensive work - for example, the simulation of the aerospace aircraft returning to the atmosphere - disperse to eight CPUs instead of a CPU, its completion speed will be much better. More common is to refactor a program to avoid input / output (I / O) "block". If your consumer-level application can be entered waiting for the keyboard, wait for data from disk to be transferred from disk or wait for the news from the message, it seems like there is no price "improve the speed" Like. Limitations of threads However, it is believed that these acceleration is risky. These acceleration depends on deeper analysis; it is possible to increase speed only if there is unproduced resource available. In addition, threading is not the only way to achieve these concurrency, and often is not the best way. The academic literature has studied at least twelve important investment into the practical application of concurrent model. In addition to hearing, you have heard too much processing (from the sense of the program), collaborative routines and event-based programming, may also hear continuous, generators, and several more Mysterious and unknown construction. For example, if you have a language that supports the generator but does not support threads, you can use the generator to simulate the thread (vice versa), in this sense, all methods have a form of substantially right. s things. However, writing suitable procedures is different from abstract peer. There is indeed difference between the concurrent model when you work hard to deliver a reliable application. For example, threads have a vulnerability that people have known for many years. The thread is a relatively low level programming structure. It is difficult to write programs for threads; it is difficult to manipulate threads that can cause inconsistent data, deadlock, non-retractable lock, and inverted priority. Java was originally intended to support only multi-threaded, which used it as a concurrent core concept. Recently, Java gave up this plan due to threaded performance issues. The debugger overhead that can debug thread is huge, which has long been notorious. However, it is not all bad news. If you spend a clear understanding of some basic concepts, use threads like XML or LDAP or reliable as other specializies. More directly, there is a more secure for many situations - sometimes faster! - concurrent model. In many cases, the best way to make an application a multitasking is to break down into multiple processes that cooperate rather than threads. Programmers usually refuse to accept this reality. One reason is that history: The process is often much more than the thread "heavy", and it is still the case in most versions of Windows. However, in modern Linux, the context switch between the different processes is only more than 15% of the corresponding context between the threads of the same process. The return brought by the time spent is a more profound and more robust programming model. Many programmers can write secure independent processes. But the programmer who can write a safe thread is quite small.
When is it more suitable for using multiple processes instead of multithreading? For example, if you have a "Control Panel" graphical user interface (GUI), it monitors several large-scale results, retrieves, and updates database records, and may even report the status of external physical devices. You can put all these tasks in a process, with a separate thread for each task. In Windows, this is often a preferential practice. However, as far as my development practice, I usually put each task into its own process, communicating with each other by a socket, pipe or from time to time. Since you can use all your usual command line tools to automate the independent process, the above practice greatly simplifies unit testing. A process of crash will not endanger any other process. Its performance is usually not equal to the multi-thread, and sometimes better, depending on the specific hardware and programming status. Other concurrent models such a multi-process implementation often relies on event-based programming. Event is a different concurrent concept, which is useful for managing I / O and related multitasking duties. The event will be asynchronously "external action" with the callback (also known as signal, binding, etc.) on programming. Think about the GUI control panel mentioned earlier; when writing this program in UNIX, a high-performance approach is to update only when the SELECT () system calls detected data. Programmers using C often use SELECT to marke event-based methods. You may regard "collaborative routines" or "builder" as content outside the classroom. However, they have been built into the definitions such as MODULA and ICON, because they are still easy to understand while making multi-task programming and makes it easy to understand (so it is still safe). If you have a complex performance requirement, if your application is optimally modeled by hundreds of child tasks, especially if your server room places a lot of multiple hosts, you should study a wider range Compact model. You will find that each model has its most suitable case. One of the concurrent models may suit your own needs. At the same time, please realize the following, that is, you may seek support for any model you want to use in Linux. The following reference points to some implementations and experiments with various concurrent models, and other reference materials. Multi-way puzzle is the last thing to pay attention to: Don't assume your multi-tasking software to run well in your multi-process (often "Symmetric Multiprocessing" (SMP)) hardware. Especially for all older versions of Linux, useful results from SMP machines, often need to be expertise. When using multiple (up to 4, sometimes more) processors to handle different processes, Linux 2.4's default installation can be done well. However, multiple threads in a process may cause bottlenecks on a single processor, while other processors are idle. Other concurrent methods sometimes cause a similar problem. Avoid these resource waste depends on your specific situation. With Linux 2.4 and popular multi-channel hardware, you can reasonably expect the default "kernel thread" (see the LINUX thread in the reference) to properly dispatch the thread, the scheduling thread is assigned these threads. Give different CPUs. Please use TOP and other system management tools to verify scheduling is correct. For specific issues in the actual thread scheduling, please ask for your Linux vendor or user group. In your majority programming, you may be naturally decomposed into different logical tasks. Clearly understand the basic concurrency concept, you can apply them to meet your own requirements.
Keep in mind that there is only the front side aspects and rearward aspects: "User View" or "Programming Model" controls how you interact with the application, and "backend" is managed to assign tasks to hardware. jobs. Strictly distinguish your features and performance requirements. Finally, please remember, talk about concurrency is not just threads. You often take advantage of your server by using a model that is obviously not multi-threaded. Reference information Please click on the discussion of the top or bottom of the article to participate in the discussion forum on this article. I wrote an article on the common question of Comp.Programming.Threads with a very particular article. But there is no doubt that it is a valuable reference, especially if you have been challenged by the actual thread programming. My first main complaint about the previous common question is that it is so close to C / C programming so that there is no other language or concurrent model. There is a lot of other languages, including Java, and threads specific to their own language. I will collected the reference information about these contents and related information to my threaded personal jump page. The Linux thread home page does talk about other languages other than C / C . The main problem with this home page is that there is no maintenance for many years. Despite this, there is still a valuable discussion on user-level and kernel-level multi-threaded, non-seizuated scheduling to preemptive scheduling, etc. "Modern Concurrency Abstractions for C #" shows that the concurrent theoretical field is still increasing. Many researchers and engineers are continuing to design and apply new models to better perform multi-tasking. Several members of the IBM Linux Technology Center are doing the work of the Next Generation Posix Threading project. Make more information in the NGPT home page. Please read the previous column articles in Cameron on DeveloperWorks:
Expect exceeds expectations (developerWorks, April 2002) A must-have book for Linux developers (developerWorks, May 2002) Application deployment (developerWorks, June 2002) Lightweight Web techniques (developerWorks, July 2002) in developerWorks You can also read new ideas in the following:
Runtime: Context Switching (United States July 2002) Charming Python: Generator-based State Machines (DEVELOPERWORKS, JU 201U) In the developerWorks Linux zone, you can find more Linux articles.
About the author Cameron is a full-time consultant of Phaseit, Inc., he often writes an article on open source and other technical topics, and a speech. You can contact CAMERON via claird@phaseit.net.