Summary of linux--threads and processes __linux

Source: Internet
Author: User
what is a process.

Process: A process is a computer program on a data set of a running activity, is the system for resource allocation and scheduling of the basic unit, is the basis of operating system structure.
To put it simply: a process is a program that is being executed. is shifting. Context Switch program execution time sum = CPU Load Context +cpu execution +cpu save context.

All information about the process is placed in the Process Control block (PCB), and it maintains management-related information.
In Linux, this is called task_struct.
Task_struct is a data structure of the Linux kernel that is loaded into RAM and contains information about the process.
Each process puts its information in the TASK_STRUCT data structure. You can find it in the include/linux/sched.h.

The task_struct contains the contents:
Identifier: A unique label that describes this process, used to differentiate between other processes.
Status : Task status, exit code, exit signal, etc.
Priority : The priority relative to other processes.
Program Counter : The address of the next instruction that will be executed in the program.
Memory pointers : Pointers that include program code and process-related data, and pointers to memory blocks shared with other processes
Context Data : The data in the register of the processor when the process executes.
I/O state information : includes an I/O request that is shown, an I/O device assigned to the process, and a list of files that are made by the process.
Billing information : May include the total processor time, the total number of clocks used, time limit, accounting number, etc. What is called a thread.

Threads (TCB), sometimes referred to as lightweight processes, are the smallest unit of program execution flow.
A thread is an entity in a process, the basic unit of a system that is independently dispatched and dispatched, and the thread itself does not own system resources and has only a bit of the necessary resources in operation, but it can share all the resources owned by the process with other threads of the same process.
One thread can create and undo another thread, which can be executed concurrently between multiple threads in the same process.
Each program has at least one thread, and if the program has only one thread, it is the program itself.

Threads are running within the process address space, emphasizing resource sharing. (Multiple threads share an address space).

There is no real thread under Linux, it is using processes to simulate threads

Multiple threads within an address space share resources and environments:
1. File Description Chart
2. How each signal is processed
3. Current working environment
4. User ID and Group ID
But some resources are one for each thread.
1. Thread ID
2. Context, including the values of various registers, program counters and pointers.
3. Stack space
4. Signal Shielding Word
5. the difference between the priority scheduling level process and the thread.

The main difference between processes and threads is that they are different ways of managing operating system resources.

1. Whether it interacts with each other: the process has a separate address space, and once a process crashes, it does not affect other processes in protected mode. A thread is just a different execution path in a process.

2. Robustness: Threads have their own stack and local variables, but there is no separate address space between threads, one thread dead is equal to the whole process dead, so many process programs are more robust than multithreaded programs.

3. Efficiency: Process switching, the resource consumption is greater, the efficiency of some. However, for concurrent operations that require simultaneous and share certain variables, only threads can be used, and processes cannot be used. And the process has a separate memory unit in the process of execution, while multiple threads share the memory, which greatly improves the efficiency of the program.

4. A program has at least one process, and a process has at least one thread.

5. Process is the basic unit of allocating resources, and threads are the basic unit of scheduling resources.

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.