Always come to the process, thread muddled, today summarized under:
From TIPI
Process
What is the process? A process is an executing program, a process is an instance of a program that is executing on a computer, and a process is an entity that can be assigned to the processor and executed by the processor. Processes typically include instruction sets and system resource sets, where the instruction set refers to the program code, where the system resource set refers to I/O, CPU, memory, and so on. Together, we can also understand that a process is a program with a certain independent function in a data set on a running activity, the process is a system for resource allocation and scheduling of an independent unit.
As the process executes, the process can be represented by a unique representation, consisting of the following elements:
- Process Descriptor: A unique identifier for the process that distinguishes it from other processes. Called the process ID in Linux, generated during system call fork, but instead of its PID field returned by Getpid, its thread group number Tgid.
- Process state: We often say suspend, run, etc. state, which represents the current state.
- Priority: The execution schedule between processes is related, relative to other processes.
- Program counter: The address of the next instruction that will be executed in the program, which is the memory address in the kernel operation or in the user's memory space.
- Memory pointers: Includes pointers to program code and process-related data, as well as pointers to other processes that share memory blocks.
- Context data: The data of the processor's registers when the process executes.
- I/O status information: includes explicit I/O requests, I/O devices assigned to processes, etc.
- Accounting information: May include total processor time, total number of clocks used, time limit, etc.
All of these elements are placed in a data structure called the Process Control block. Process Control blocks are structures in which the operating system can support multiple processes and provide multi-processing. When the operating system makes a process switch, it performs a two-step operation, one that interrupts the process in the current processor, and the second executes the next process. The program counters, context data, and process state in the Process control block change, whether it is an interrupt or an execution. When a process is interrupted, the operating system saves the program counter and processor registers (contextual data in the corresponding process control block) to the appropriate location in the process control block, and the process state changes, which may go into a blocking state, or it may enter a ready state. When the next process is executed, the operating system sets the next process to run as a rule and loads the program context data and program counters that are about to execute the process.
Thread
The process has two feature parts: resource ownership and dispatch execution. Resource ownership refers to a process that includes resources such as memory space, I/O, and so on that the process is running. Dispatch execution refers to the execution path in the middle of the process execution, or the instruction execution flow of the program. These two feature parts can be separated, and, after separation, ownership of the data is often called a process, which has an distributable part of the execution code called a thread or a lightweight process.
Threads have "thread of execution" meaning inside, while processes are defined as resource owners in a multithreaded environment, and they store process control blocks for processes. The structure of a thread differs from the process, and each thread includes:
- Thread state: The current state of the thread.
- An execution stack
- Private data area: Static storage space for each thread local variable
- Register set: Some states of the storage processor
Each process has a process control block and a user address space, each with a separate stack and a separate control block, each with its own independent execution context.
Threads are somewhat different from the process during execution. Each separate thread has a program run entry, sequence of sequence execution, and exit of the program. However, threads cannot be executed independently, and must be dependent on the process, which provides multiple threads for execution control. From a logical point of view, the meaning of multithreading lies in a process where multiple execution parts can be executed concurrently. At this point, the process itself is not a basic operating unit, but a container for threads.
The advantage of a thread over a process is that it is fast, whether it is creating a new thread or terminating a thread, and whether it is switching between threads or sharing data or communication between threads, its speed has a greater advantage over the process.
Concurrency and parallelism
Concurrency, also known as a common line, refers to the ability to handle multiple simultaneous activities, and concurrent events do not necessarily occur at the same time. For example, a modern computer system can load multiple programs into memory in a process in the same time, and use the processor's division multiplexing to show the feeling of running simultaneously on a single processor.
Parallelism refers to the simultaneous occurrence of two concurrent events, with the meaning of concurrency, while concurrency is not necessarily parallel.
The difference between concurrency and parallelism is that a single processor handles multiple tasks simultaneously and multiple processors or multicore processors simultaneously handle many different tasks. The former is a logical simultaneous occurrence (simultaneous), while the latter is physically occurring simultaneously.
From Baidu Brother
- The difference between threads and processes is summarized:
A. address space and other resources : processes are independent of each other and are shared among threads of the same process. Threads within a process are not visible in other processes.
B. Communication: Inter-process communication IPC, between threads can directly read and write process data segments (such as global variables) to communicate--requires process synchronization and mutual exclusion means of support to ensure data consistency.
c. Scheduling and switching : Thread context switches are much faster than process context switches.
D. In a multithreaded OS, a process is not an executable entity.
A process is a program with a certain independent function about a single run activity on a data set, a process that is an independent unit of the system's resource allocation and scheduling. A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process. The thread itself does not own the system resources, it has only a few resources (such as program counters, a set of registers and stacks) that are essential in the run, but it can share all the resources owned by the process with other threads belonging to one process.
Process-----Thread