Why do we need to create a thread and a thread?

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.