Definition and Characteristics
process
Refers to the system can run independently and as the basic unit of resource allocation, it is composed of a set of machine instructions, data and stack, is an independent operation of the active entity.
Process characteristics:
- 1. Dynamic: The essence of a process is an execution process of the program. The process is dynamically generated and dies out.
- 2. Concurrency: Any process can be executed concurrently with other processes.
- 3. Independence: A process is a basic unit that can run independently, as well as an independent unit for system resource allocation and scheduling.
- 4. Asynchrony: Due to the mutual restriction between processes, the execution of the process is intermittent, that is, the process moves forward at an independent and unpredictable speed.
thread
A thread is an entity in a process that serves as the basic unit of system scheduling and dispatching.
Multithreading running in the same address space of the same process has the following advantages compared with using multiple processes:
Advantages over process threads
-
1. Threads are less expensive to create and destroy than processes. When creating a thread, it is only necessary to establish the corresponding table entries of the thread control table, or the relevant queue, while creating a process, to create PCB table and initialization, enter the relevant process queue, establish its address space and required resources, etc..
-
2.CPU switching between threads costs much less than a process. Because the switching threads are all in the same address space, you only need to modify the thread control table or queue, without involving the address space and other work.
-
3. Threading mechanism increases the effectiveness of communication. The communication between processes usually requires the participation of the kernel to provide communication mechanism and protection mechanism, while the communication between threads is in the address space of the same process, sharing main memory and files, without the participation of the kernel.
The difference between a process and a thread
(1) Scheduling
In traditional operating systems, the basic unit of CPU scheduling and dispatch is the process. In the operating system that introduces thread, the thread is regarded as the basic unit of CPU scheduling and dispatching, and the process is regarded as the basic unit of resource owning, so that the two attributes of the traditional process are separated, and the thread programming runs lightly, which can significantly improve the concurrency of the system. Switching threads in the same process does not cause process switching, thus avoiding expensive system calls, but switching threads in one process to threads in another can still cause process switching.
(2) Concurrency
In an operating system that introduces threads, not only processes can execute concurrently, but also multiple threads in a process can execute concurrently, so that the operating system has better concurrency, thus improving system resource utilization and system throughput more effectively. For example, in a single-CPU operating system without threads, if only one file server process is set up, when it is blocked for some reason, there are no other file server processes to serve it. In thread-introduced operating systems, multiple server threads can be set up in a single file server process. While the first thread waits, the second thread in the file server process can continue running; When the second thread blocks, the third thread can continue executing, significantly improving the quality of file services and system throughput.
(3) Have resources
In both traditional and threaded operating systems, a process is an independent unit that owns system resources and can own its own resources. Generally speaking, a thread cannot own its own resources (there are some essential resources), but it can access the resources of its own process, that is, a process’s code segments, data segments, and system resources (such as open files, I/O devices, and so on) that can be shared by all other threads of the same process.
(4) Independence
The independence between different threads in the same process is much lower than the independence between different processes. This is because to prevent interference and destruction between processes, each process has an independent address space and other resources, in addition to sharing global variables, other processes are not allowed to access. But different threads in the same process is often in order to improve the concurrency and the cooperation between each other and create, they share the process of the memory address space and resources, such as each thread can access all the address in the address space to which they belong process, such as a thread’s stack can be other threads to read, write, or even completely cleared.
(5) System overhead
When a process is created or destroyed, the system allocates or reclaims resources for it, such as memory space and I/O devices. Therefore, the overhead for the operating system to do this is significantly greater than the overhead when creating or canceling threads. Similarly, in the process of switching, involving the whole process of the current save CPU environment Settings and new be scheduled to run CPU environment Settings, and only save to thread and set up a small amount of the contents of a register, is not involved in memory management operation, visible, process switch overhead than thread overhead. In addition, because multiple threads in the same process have the same address space, it is easier to implement synchronization and communication between them. On some systems, threads can be switched, synchronized, and communicated without the intervention of the operating system kernel.
(6) Support multi-processor system
In multiprocessor systems, a traditional process, that is, a single-threaded process, can only run on one processor, no matter how many processors there are. However, for multi-threaded processes, multiple threads in a process can be distributed to multiple processors and executed in parallel, which will undoubtedly speed up the process completion. Therefore, modern processor OS without exception has introduced multithreading.