Address: http://www.zhihu.com/question/19901763
Original:
so explain the question:
1. One-process single- threaded : A person eats food on a table.
2. single-Process multithreading : Many people eat food together on the same table.
3. multi-process single-threaded : More than one person eats vegetables on their own table.
Multi-Threading problem is that many people at the same time eat a dish when it is easy to compete, for example, two person at the same time to clip a dish, a person just out of chopsticks, the results reached the time has been sandwiched away vegetables ... At this point, you have to wait for a person to take a bite, in return to another person to the dish, that is, the sharing of resources will conflict.
1. For Windows Systems, the "open table" is expensive, so Windows encourages everyone to eat on a table. Therefore, the focus of Windows multithreaded learning is to face the problem of resource scrambling and synchronization.
2. For Linux Systems, the "open table" is very inexpensive, so Linux encourages everyone to open their own table for food. This brings a new problem: sitting on two different desks, it's inconvenient to talk. Therefore, the learning focus of Linux is to learn the methods of inter-process communication.
opening a table means creating a process. The overhead here mainly refers to the time overhead.
you can do an experiment: Create a process, write some data to memory in the process, read the data, and then exit. This process repeats 1000 times, which is equivalent to creating/destroying the process 1000 times. The test results on my machine are:
Ubuntulinux: 0.8 seconds
Windows7:79.8 seconds
The cost of the two is about 100 times times the difference.
This means that in Windows, the overhead of process creation cannot be overlooked. In other words, it is not recommended that you create a process in Windows programming, and if your program architecture requires a lot of process creation, it is best to switch to a Linux system.
A typical example of a large number of creation processes is two, one of which is the GNU Autotools Toolchain, which compiles a lot of open source code that is slow to compile under windows, so it's best for software developers to avoid windows. The other is the server, some server frameworks rely on a lot of creation process to work, even for each user request to create a process, these servers run under Windows is inefficient. This "possible" is also the reason why Linux servers are much larger than Windows servers worldwide.
If you are writing server-side applications, in fact, in the current network service model, the cost of opening the table is negligible, because it is generally popular in accordance with the CPU core number of open processes or threads, after the completion of the number has been maintained, The process and thread internally use either a coprocessor or asynchronous communication to handle multiple concurrent connections, so the overhead of open processes and threads can be ignored.
Another new kind of overhead is put on the agenda: core switching overhead.
modern systems, the general CPU will have multiple cores, and multiple cores can run multiple different threads or processes at the same time.
when each CPU core runs a process, there is no need to consider the context when switching between CPU cores because each process's resources are independent.
when each CPU core runs a thread, because each thread needs to share resources, the resources must be copied from one core of the CPU to the other to continue the operation, which consumes additional overhead. In other words, in the case of multi-core CPUs, multithreading is less performance than multi-process.
therefore, in the current server-side programming for multicore, it is necessary to be accustomed to multi-process rather than multithreading.
1013-----apue----------thread to handle what problems (turn to know the answer)