Why do we need to create a thread and a thread?
I personally think it is to improve the CPU utilization. The creation, maintenance, and management burden of threads on the operating system is light. Because there is little thread-related information, the burden of friendship means that the cost or overhead of the thread is relatively small. Context switching occurs when the processor is in addition to one process and activates another process. For context switching, the operating system must start and restart the information required by each process. This means that information about the existence status of the process must be saved. when the process is activated again, the execution can continue from the exit location. What information does the system need to save during process switching?The following list:
1. The required information includes executable programs, stacks, and pointers to static and dynamic variable memory allocation.
2. The register group contains information such as the pointer for executing the next instruction.
3. Process status (suspended or blocked) and priority are required for process re-appointment. The I/O status of the program is also saved.
4. Save the Process Planning Information, memory management information, and counting information.
5. The process needs a file descriptor and a read/write pointer to continue using resources.
The thread also needs context. Context switching also occurs when the thread is preemptive.
Compared with processes, threads have the following advantages:
1. The thread does not need address space. The thread is included in the address space of the process. (So no threads are required when the process is re-appointed)
2. The thread context contains only one stack, one register group, and one priority.
3. Register groups contain program or command pointers and stack pointers.
4. The thread text is included in the process text.
5. All resources owned by the process belong to the thread. Therefore, all information related to resources is not part of the thread context.
6. Other information, such as planning and counting, is defined by the process. It does not need to be included in the context of the thread.
Threads and processes share the same ID, register group, status, and priority. The thread shares the resources of the parent process with the child process.
Their differences are:
1. The thread does not have its own address space. If a process creates multiple threads, all the threads will be included in its address space.
2. The Parent and Child processes must communicate through the inter-process communication mechanism. Multiple Threads in a process communicate by reading and writing data to process variables.
3. The sub-process does not control other sub-processes, while the thread of the process is seen as a peer and controls other threads of the process.