Program: A set of valid sets of instructions
Process: The execution of a program is a process. The process can also be seen as a separate program, in memory has its corresponding code space and data space, a process has the data and code only belong to themselves. Process is the basic unit of resource allocation, and also the basic unit of dispatch operation.
Threads: Threads are thought to be lightweight processes, which are the one-way programs that run alone in a process. In other words, a process can contain multiple threads and at least one main thread, while threads of the same process share the process's code and data. At the same time, each thread has its own stack, and these stacks are private to the thread. Threads are the basic unit of processor scheduling.
So why introduce threads?
1. Easy to dispatch
2. Threads can share process data and code, making it easier than a process needs to communicate through a message. The speed of starting and switching is also faster than the process.
3, with high concurrency, you can start multiple threads to execute different parts of the same program.
4, make full use of the function of the processor. Run different threads on each processor to achieve the concurrency of the application.
Differences in procedures and processes
1, the program is static, it knowledge of a set of cold instructions, does not have any operational significance. The process is a dynamic process running on the dataset, the process is a dynamic entity, it should be created, should be scheduled to execute, should wait for resources or events to wait, should complete the task is revoked, it has its own life cycle, it reflects the program on the data set to run the full dynamic process.
2, the process and the program is not one by one corresponding relationship, the same program runs on different data sets is a different process.
3, the process also has the concurrency and the intercourse, but the procedure is closed.
The difference between a process and a thread
1, a process can have multiple threads, and a thread can only be owned by a single process.
2, the process is the basic unit of resource allocation, the thread is the basic unit of the processor dispatch, all the threads share all the resources and code of the process to which they belong.
3, the process of thread execution is easy to synchronize, and the process needs to be synchronized through message communication.
4, the thread partition scale is smaller, the concurrency is higher.
5, threads share the process of data at the same time, have their own private stack.
6. Threads cannot be executed individually, but each thread has a program's entry, execution sequence, and program exit. It must compose a process to be executed.
connections and differences between programs, processes, and threads