Threads and processes

Source: Internet
Author: User

What is a thread?

The program does not run alone, and the program is assigned resources to run only if it is loaded into memory, and this execution is called a process. The difference between a program and a process is that a program is a set of instructions, which is a static descriptive text of the process, and a process is an execution activity of a program, which belongs to the dynamic concept.

An executing instance of a program is called a process.

Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique proce SS identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of Execution.

Each process was started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

The start of each process starts with a thread that is called the master thread. A process can create multiple threads from one thread.

A thread is a context of execution, while a process is a bunch of the resources associated with a computation. A process can has one or many threads.

A process has multiple threads

Advantages of the process:

    • Multi-channel programming allows multiple programs to load into memory at the same time, enabling concurrent execution of multiple processes, with the user feeling that they have exclusive CPU

Disadvantages of the process:

    • Within a process, you can only do one thing at a time.

    • If a process is blocked during execution, such as waiting for input, the entire process hangs, even if some work in the process does not depend on the input data, it cannot be executed.

What is a thread?

A thread is the smallest unit that the operating system can perform operations on. It is included in the process and is the actual operating unit of the process. A thread refers to a single sequential control flow in a process in which multiple threads can be concurrently, each thread performing different tasks in parallel

A thread is a execution context, which are all the information a CPUs needs to execute a stream of instructions.

Understanding: CPU Time-sharing

Suppose you ' re reading a-book, and you want-to-take a-break right now, but you want-be-able-come back and resume Rea Ding from the exact point where you stopped. One-to-achieve-is-jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.

If you have a roommate, and she's using the same technique, she can take the book while you ' re not using it, and resume re Ading from where she stopped. Then you can take it back, and resume it from the where you were.

Threads work in the same. A CPU is giving the illusion that it's doing multiple computations at the same time. It does that is spending a bit of time on each computation. It can do this because it has a execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.

On a more technical level, an execution context (therefore a thread) consists of the values of the CPU ' s registers.

The difference between a thread and a process:

    1. Threads share the address space of the process that created it; Processes has their own address space.

Multiple processes created by the same process, sharing the address space of the process. And each process has its own separate address space.

2. Threads has direct access to the data segment of its process; Processes has their own copy of the data segment of the parent process.

Threads can access other data areas in their processes directly. A process has a proprietary copy of its parent process data area.

3. Threads can directly communicate with other Threads of its process; Processes must use interprocess communication to communicate with sibling processes.

A thread can communicate directly with other threads under its process. A process must use an intermediate process to communicate with its sibling process.

4. New threads is easily created; New processes require duplication of the parent process.

It is easy to create a new thread. Creating a new process requires a full copy of its parent process

5. Threads can exercise considerable control over Threads of the same process; Processes can only exercise control over the child processes.

Threads can control other threads of the process in which they reside. A process can only control its child processes.

6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the PR ocess; Changes to the parent process does isn't affect child processes.

Modifications to the main thread may affect the behavior of other threads in the process in which they are located. Changes to the parent process cannot affect child processes

Threads and processes

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.