Some simple understandings of processes and threads

Source: Internet
Author: User

I. Definition of processes and threads

A process is an executing program: A program is a text that describes an instruction, a static concept that runs the instruction, gets a process each time it runs, and the process is a dynamic concept; the operating system assigns resources to the process.

A thread is a piece of code that is actually executed in a process: it is also a dynamic concept; the operating system dispatches and dispatches threads, allocating CPU time slices to the threads for execution.

Two. Process-to-thread comparison

1. From the operating system perspective

A process is the basic unit of resource allocation: that is, the operating system assigns different resources to different processes, such as window handles, file system handles, kernel objects, virtual memory, and so on, and we can say that the operating system assigns the window handle to a process. Instead of saying that the operating system assigns a window handle to a thread. At the same time, all the threads in a process share all the resources allocated to the process, so the communication between threads is very rapid, and the communication between processes needs to pass through the IPC.

A process is the basic unit of Dispatch and Dispatch: The operating system allocates CPU time slices to threads so that multiple threads can run on different CPUs at the same time, or on different cores of multicore CPUs.

2. From the concurrency perspective

Processes can be executed concurrently: If you can run multiple Notepad programs, run multiple QQ.

Threads can also execute concurrently: when you run a Word program, a thread waits for user input, a thread checks for spelling, and a thread stores the data in a temporary file ...

Simply put: Both can be concurrent, but the level of concurrency is different.

3. From a system overhead perspective

When a process is created or revoked, the system allocates or reclaims resources, such as memory space, I/O devices, and so on, the operating system spends significantly more than the cost of creating or revoking threads, and the overhead of process switching is much greater than the cost of thread switching.

Three. Problems caused by multithreading

1. Because all the threads in the process share all the resources assigned to the process, if the concurrent threads access the same data, it will result in inconsistent data, and the synchronization mechanism must be implemented.

2. How to improve the utilization of multi-thread CPU.

In short: a process is a container in which a thread executes in the container of the process, which is the code that is actually executed. A process has at least one thread, which is often referred to as the main thread. threads, in addition to the essential resources (a set of registers and stacks of program counters), do not own system resources, and all threads in the process share all the resources that are allocated to the process.

Reference:

A simple explanation of the 1.http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html process and thread

2.http://www.cnblogs.com/hazir/archive/2011/05/09/2447287.html the difference between a process and a thread

3.http://jingyan.baidu.com/article/624e74598efcc834e9ba5a66.html the difference between a process and a thread

Some simple understandings of processes and threads

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.