Understanding of processes, threads, and application domains

Source: Internet
Author: User
One of the questions interviewed a few days ago is to talk about the differences between processes, threads, and application domains. Although I can talk about the following, I feel that my understanding is superficial and not thorough, in my spare time, I rummaged out the "Operating System" (Fortunately, I was not willing to sell it when I graduated) from the book box, turning over the Chapter of the process. I listed the following three questions:
1. Background of these concepts (technologies) and problems to be solved (what to do)
2. Relationships and differences between them
3. Development context between the three

Process:
(Review your homework and read all the relevant content. You forgot about it)
The process is created and managed by the operating system. If you leave the operating system, you will not talk about any processes. First, let's look at the four basic features of the operating system:
1. Concurrence)
The two concepts of parallelism and concurrency are both similar and different. Parallelism refers to the occurrence of two or more events at the same time. This is a microscopic concept, that is, physical events occur simultaneously; concurrency refers to the occurrence of two or more events at the same time interval. It is a macro concept. In a multi-channel program environment, concurrency means that multiple threads run simultaneously within a period of time, but in a single processor system, only one program can be executed at a time, therefore, these programs are executed alternately at the micro level. It should be noted that common programs are static entities and they cannot be executed concurrently. In order for the program to run concurrently, the system must create processes for each program separately. A process, also known as a task, is a basic unit that runs independently in the system and acts as a resource allocation entity. Multiple processes can concurrently execute and exchange information. A process needs to run resources such as CPU, storage space, and I/O devices. The purpose of introducing processes in the operating system is to enable concurrent execution of programs.
2. Sharing)
Sharing means that resources in the system can be used by multiple concurrent processes in the memory. Because resources have different attributes, multiple processes share resources in different ways. They can be divided into mutual exclusion and simultaneous access modes.
3. Virtual)
It means that a physical entity is converted into several logical counterparts through technology. In the operating system, the virtual implementation mainly uses the time-sharing method. Obviously, if n is the number of virtual logical devices corresponding to a physical device, the speed of the virtual device must be 1/N of the physical device.
4. asynchronism)
In a multi-channel programming environment, concurrent execution of multiple processes is allowed. Due to resource restrictions, process execution is generally not "in one breath ", instead, it runs in the form of "Stop and stop. In the memory, it is unpredictable when each process is executed, when it is paused, how it is pushed forward, and how long each program takes to complete. Or, the process runs in one step. However, as long as the running environment is the same and the job is run multiple times, the same results will be obtained. Therefore, the asynchronous running mode is run.

It can be seen that the operating system generates a process for concurrent execution of the program.

Process Definition: the running process of a program that can be executed concurrently on a dataset.
Process features:
1. Since dynamic processes are the execution processes of process entities, processes have a certain life cycle. The program is just a set of ordered commands and placed on a certain medium. It has no running meaning, so the program is a static entity.
2. Concurrency
3. Independence means that the process entity is a basic unit that can run independently, and also the basic unit for obtaining resources and scheduling independently by the system.
4. Asynchronization
5. Structural features in terms of structure, the process entity consists of three parts: the program segment, data segment, and process control block.
(Process Control Block (PCB): A process control block is part of a process entity. It records all information required by the operating system to describe the process and control the process. OS controls and manages concurrent processes based on PCB)

Summary of processes:
Definition: a program that can be run concurrently in a closed dataset. Each process has its own address space and a single control flow.
Problem to be solved: in order to make the program execute concurrently (to execute concurrently, you must isolate the process so that the process is independent. That is, each process has its own data segment, program segment, and process control block)

Thread appearance:

First, let's review the two basic attributes of a process: (1) a process makes an independent unit that can have resources (2) a process is also a basic unit that can be independently scheduled and assigned. Because of these two basic attributes, the process becomes a basic unit that can run independently, thus forming the basis for concurrent execution of the process.
To enable concurrent execution of a program, the system must perform the following operations:
(1) create a process. When creating a process, you must assign all necessary resources, except the processor, such as memory space, I/O devices, and establish the corresponding PCB.
(2) undo the process. When the system revokes the process, it needs to first recycle these resources and then undo the PCB.
(3) process switching. When switching a process, it takes a lot of time for the CPU environment of the current process to be retained and the CPU environment of the newly selected process to be set.
In short, because a process is the owner of a resource, the system must pay a large amount of time and space overhead during process creation, revocation, and switching, because of this, the number of processes set in the system should not be too large, and the frequency of process switching should not be too high, but this also limits the further improvement of concurrency. To solve this problem, many operating system scholars think that the two attributes of the process are separated by the operating system. That is, as the basic unit for scheduling and allocation, and not as the unit for independent resource allocation, it is easy to run. However, the basic unit for resource ownership is not frequently switched, under the guidance of this idea, the concept of thread is generated.

Reason for thread Introduction: to reduce the time-space overhead of concurrent program execution, the OS has better concurrency.

In the OS where threads are introduced, a thread is an entity in a process (one or more command execution streams in the process) and the basic unit for independent scheduling and distribution by the system. Threads basically no longer have system resources (only have a few resources that are essential for running, such as program counters, registers, and stacks ), however, it can share all resources of a process with other thread functions of the same process. One thread can create and withdraw another thread. multiple threads in the same thread can be concurrently executed.

Comparison between threads and processes:
A thread has many characteristics of a traditional process, so it is also called a light thread or process element. a traditional process is called a heavy process. In the OS where threads are introduced, a process usually has several threads. The following describes how to compare threads and processes.
1. Scheduling
In the "primitive" OS, the basic unit of resources and the basic unit of independent scheduling and allocation are processes. In the OS where threads are introduced, threads are used as the basic unit of scheduling and dispatch, while processes are used as the basic unit of resource source, separating the two attributes of traditional processes, the thread can be run with ease, which can significantly improve the system concurrency. In the same process, thread switching does not cause process switching. When the thread in one process switches to the thread in another process, process switching will occur.
2. Concurrency
In the OS where threads are introduced, not only can concurrent execution be performed between processes, but also between multiple threads in a process, so that the OS has better concurrency, in this way, system resources can be used more effectively and system throughput can be improved.
3. Have resources
Whether it is an "original" OS or an operating system with threads, a process is an independent unit of resources and can own its own resources. A thread no longer owns system resources, but it can access the resources of its processes.
4. System overhead
When creating or canceling a process, the system must allocate or recycle resources for it, such as memory space and I/O devices. This is because the overhead of the OS is significantly greater than the overhead for creating or canceling a thread. Similarly, during process switching, the CPU environment of the entire process is saved and the CPU environment settings of the newly scheduled process are involved. Thread switching only needs to save and set a small number of registers, and does not involve memory management operations. It can be seen that the overhead of process switching is much higher than that of thread switching. In addition, because multiple threads in the same process have the same address space, it is easier to implement synchronization and communication between them.
The implementation of this mechanism in modern operating systems can be divided into two main categories. It is divided into kernel threads and user threads based on whether the operating system kernel is aware of threads.
1. The creation, cancellation, and switching of kernel threads, whether in the user process or in the system process, are implemented by the kernel. A thread control block is reserved in the kernel. Based on this control block, the kernel perceives the existence of the thread and controls the thread.
2. User threads exist only at the user level. The creation, cancellation, and switching of such threads are not implemented by system calls. Therefore, such threads are irrelevant to the kernel. Correspondingly, the kernel does not know the existence of user-level threads. (Scheduling is implemented by adding a Runtime Library in the user space. These runtime libraries are called "thread packages". Each time a user's process obtains CPU control, the thread Runtime Library determines where to start running)

(In fact, the thread mentioned above is the basic unit for Operating System Scheduling. It actually refers to the kernel thread. During scheduling, the operating system makes a scheduling decision based on the thread running conditions in each process. If there is no ready-state thread in a process, the process will not be scheduled to occupy the CPU.
In Windows 2000, when the Operating System Schedules jobs, it simply does not determine which process the thread belongs to. Instead, all the ready threads are arranged in a number of priority queues for scheduling. In this case, the thread is indeed the smallest unit of scheduling ).

Thread Summary:
Background: As a process is the owner of a resource, the system must pay a large amount of time and space overhead during process creation, cancellation, and switching, limits the degree of concurrency to be further improved.
Problems to be Solved: In the process of Process Creation, cancellation, and switching, the system must pay a large amount of time and space overhead.
Solution: separate the two attributes of the process and process them separately by the operating system. The attribute "basic unit of independent scheduling and allocation" is separated as a thread, and the process is used as the basic unit of resource ownership, and the thread exists as an entity in the process.

Application domain:
(From msdn)
In. before the emergence of net, only one application can be run under a process. After the emergence of net, multiple applications can be run under a process, this is because of the emergence of application domains.
Previously, process boundaries were used to isolate applications running on the same computer. Each application is loaded into a separate process, which isolates the application from other applications running on the same computer.
The reason for isolating these applications is that the memory address is related to the process. In the target process, the memory pointer from one process to another cannot be used in any meaningful way. In addition, you cannot directly call between two processes. You must replace it with a proxy, which provides a certain degree of indirect.
The application domain provides secure and universal processing units, which can be used by the Common Language Runtime Library to provide isolation between applications. You can run several application domains in a single process with the same isolation level (in a separate process, this does not cause additional overhead for inter-process calls or inter-process switching. The ability to run multiple applications within a process significantly enhances the scalability of the server.
Isolating applications is also important for application security. For example, you can run several web application controls in a single browser process, and make these controls inaccessible to each other's data and resources.

Isolation provided by application domains has the following advantages (Reasons for introduction ):
Errors in one application do not affect other applications. Because the type-safe code does not cause memory errors, you can use the application domain to ensure that the code running in one domain does not affect other applications in the process.
A single application can be stopped without stopping the entire process. The application domain allows you to uninstall code running in a single application.
Application domains form the isolation, uninstallation, and security boundaries of managed code. A thread is the operating system structure used by the Common Language Runtime Library to execute code. At runtime, all managed code is loaded into an application domain and run by a specific operating system thread.
There is no one-to-one correlation between application domains and threads. At any given time, several threads can be executed in a single application domain, and the specific threads are not limited to a single application domain. That is to say, threads can freely span application domain boundaries, without creating new threads for each application domain.
At any given time, each line is executed in an application domain. The Runtime Library tracks which threads are running in which application domains.

Related interview questions:

Q: What do you know about windows multi-threaded programming?
Answer:
(1) [What are processes and threads ?] A process is the owner of system resources, and a thread is the smallest unit for Windows task scheduling.
(2) [What are processes and threads ?] In Windows, processes occupy 4 GB of address space; threads only occupy the stack required for running.
(3) [What is the relationship between processes and threads ?] A process is a thread container and must run in the process. A main thread is created when a process is created.
(4) [compare the number of threads] The number of threads is much greater than the number of processes. A process can have one or more threads.
(5) [about thread scheduling] Two Methods of Thread Scheduling: preemptible (such as Win9x, UNIX, OS/2), non-preemptible (DOS, win3.x
(6) [How to implement multi-thread concurrency in Windows] use time slice to simulate multi-thread concurrency in Windows
(7) [Why multithreading ?] In order to reduce the time-space overhead of concurrent program execution, the OS has better concurrency.
(8) [advantages of multithreading] Improve concurrency and Efficiency
(9) [disadvantages of multithreading] it is difficult to make data synchronization, Development and debugging difficult. Any use of multithreading will reduce the efficiency {too far}, rather than using multithreading at will

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.