Community ServerTopic Appendix 1: What is threads & Processes
Please take a critical look at the following words:
Thread and process (threads and processes), simply put, when you start a program, you start a process. In the early days, the win3.x system was in process units (Hey, in fact, I have never used win3.x). After Win98, each process can start several threads, so the thread is used as the unit. In today's operating systems, threads run independently, but they share some memory fragments, such as global variables.
Question: Is CS running on a server multi-thread? What does each user's HTTP request CS do? When the CS system is accessed for the first time, the Asp.net operating mechanism starts a CS Process. To start a process, you need to allocate an independent memory address to the process, this address is not affected and operated by other processes. That is to say, if you have other web programs that cannot access this memory space (under normal circumstances, the process and process are secure ). The process with CS is not enough. As mentioned earlier, the minimum unit execution thread of the operating system after Win98 is not a process. Therefore, a thread will be started for this request (for web programs, the HTTP request thread startup process does not require you to use system. the method in the threading namespace, the START process is completed by the right Asp.net Running Mechanism). Because all threads of a process share its memory address space, global variables, and operating system resources, therefore, this request can create a context for itself based on the shared resources, and then process related request operations. After the request is complete, the thread will be released, the lifecycle ends. (therefore, HTTP requests are stateless, because two different threads are created for two HTTP requests of the same user, and their context is different, and there is a forward and backward order. However, cookies and sessions are available to save some necessary status information ). When a second HTTP request is sent, it is very likely that the first HTTP request has not been processed completely. Therefore, thread security issues under multi-thread execution are involved.
Thread security means that a method can securely and effectively access process-level data in a multi-threaded environment (the data is shared with other threads ). The key to ensuring thread security is synchronization, which ensures that multiple threads start to execute at the same time, run in parallel, access the same object instance at the same time, and execute the same code at the same time ,. net, we can use some measures to ensure the security of our data, such as using system. threading. monitor locks to ensure synchronization of shared data access. In fact, in msdn, I often see that a method is thread safe, that is, thread security. Thread security is an advanced topic. You can't talk about it any more, or you'll just get to the bottom.
In fact, as long as IIS is not restarted, the CS Process will always exist, and there is only one. However, there are more CS threads, and there will be multiple threads according to different HTTP requests. At the same time, timer is also used in Cs and it will start a new processing thread, the life cycle of this thread continues until the timer is dispose.
We can see that CS is a complex multi-threaded program. Of course, all web apps are multi-threaded. Therefore, it is necessary to understand the concept of thread and process before analysis.