The difference between processes and threads

1. The basic unit of concurrent execution of a program that allocates and manages resources during execution, and is a dynamic concept that competes with the basic unit of computer system resources: the thread: A process without threads can be considered single-threaded. Threads are sometimes referred to as lightweight or lightweight processes, and are also a basic unit of CPU scheduling distinction: Process is the smallest unit of resource allocation, thread is the smallest unit of program execution (the smallest unit of resource scheduling); (2). The process has its own independent address space, every start a process, the system will address space for its distribution, to establish the data table to maintain the code segment, the stack segment and data segment, the operation is very expensive, and thread is in the process of the sharing of data, using the same address space, so the cost of the CPU switches a thread is far smaller than the process a lot, It is also much cheaper to create a thread than a process; (3). Communication between threads is more convenient, under the same process of the thread congratulations global variables, static variables and other data, and communication between processes to communication (IPC); (4). Multi-process program is more robust, multi-threaded program as long as there is a thread dead, the whole process also died, and a process dead will not affect the other process, because the process has its own independent address space;Copy the code

Reference link: juejin.cn/post/684490…