About processes and threads and handles

process and thread

A process is an independent unit for resource allocation and scheduling by the system . Thread is the basic unit of CPU scheduling and dispatching, which is smaller than a process and can run independently. The thread itself basically does not own system resources, but only has some essential resources in operation (such as program counter, a set of registers and stack) , but it can share all the resources owned by the process with other threads belonging to the same process **. In short, in the operating system, a process is the smallest unit of resource allocation, and a thread is the smallest unit of cpu scheduling. **

process

A process is an independent unit for resource allocation and scheduling by the system

thread

A thread is the basic unit of CPU scheduling and dispatching. It is a basic unit that is smaller than a process and can run independently. A thread has the following characteristics:

  • The WIN32 platform supports multi-threaded programs, allowing multiple threads in a program. In a single-CPU system, the system allocates CPU time slices to each thread according to the scheduling algorithm, so each thread is actually executed in time-sharing. In a multi-CPU Windows NT system, different threads of the same program can be assigned to Execute on different CPUs. Since each thread of a program runs in the same address space, it involves how to share memory, how to communicate, etc., so it is necessary to deal with the synchronization problem between threads, which is a difficulty in multi-threaded programming.
  • Threads, also known as lightweight processes. A computer science term referring to the scheduling unit of a running program.
  • A thread is an entity in a process. A process can have multiple threads, and a thread must have a parent process. A thread does not own system resources, only some data structures necessary for running; it shares all resources owned by the process with other threads of the parent process. Threads can be created and revoked to implement concurrent execution of programs. Generally, a thread has three basic states: ready, blocked, and running.
  • In a multi-CPU system, different threads can run on different CPUs at the same time, even when they belong to the same process. Most operating systems that support multiprocessors provide a programming interface that allows a process to control the affinity between its own threads and each processor.

the difference:

  • A program has at least one process, and a process has at least one thread.
  • The division scale of threads is smaller than that of processes, which makes the concurrency of multi-threaded programs high . For example, in the process of handling housework, a person puts clothes in the washing machine for automatic washing, puts rice in the rice cooker, and then starts cooking. When the dishes are ready, the rice is cooked and the clothes are washed.
  • A thread can create and destroy another thread; multiple threads in the same process can execute concurrently.
  • There is still a difference between a thread and a process during execution. Each independent process has an entry point for program execution , a sequential execution sequence , and an exit point for the program . However, threads cannot be executed independently, and must depend on the application program, and the application program provides multiple thread execution control.
  • From a logical point of view, the meaning of multithreading is that in an application, multiple execution parts can be executed at the same time . However, the operating system does not regard multiple threads as multiple independent applications to implement process scheduling and management and resource allocation. This is the important difference between process and thread.

the handle

The so-called handle is actually a data, which is a Long (integer long) data.

The handle is a unique integer used by WINDOWS to identify objects created or used by applications. WINDOWS uses various handles to identify such as application instances, windows, controls, bitmaps, GDI objects, and so on. WINDOWS handles are a bit like file handles in C language.

From the above definition, we can see that a handle is an identifier, which is used to identify an object or project. It is like our name. Everyone will have one. Different people have different names. However, There may also be someone with the same name as you. From the data type point of view it is just a 16-bit unsigned integer. Applications almost always obtain a handle by calling a WINDOWS function, which can then be used by other WINDOWS functions to refer to the corresponding object.

If you want to understand the handle more thoroughly, I can tell you that the handle is a pointer to a pointer. We know that the so-called pointer is a memory address. After the application starts, the objects that make up the program reside in memory. If we simply understand it, it seems that as long as we know the first address of this memory, we can use this address to access objects at any time. However, if you really think so, then you are very wrong. We know that Windows is an operating system based on virtual memory. In this system environment, the Windows memory manager often moves objects back and forth in memory to meet the memory needs of various applications. An object being moved means that its address has changed. If the address keeps changing like this, where do we go to find the object?

In order to solve this problem, the Windows operating system frees up some internal storage addresses for each application program to specifically register the address changes of each application object in the memory, and this address (the location of the storage unit) itself is unchanged. After the Windows memory manager moves the location of the object in memory, it notifies the handle address of the object's new address to save. In this way, we only need to remember the handle address to indirectly know where the object is in memory. This address is allocated by the system when the object is loaded (Load), and released to the system when the system is unloaded (Unload).

Handle address (stable) → records the address of the object in memory → the address of the object in memory (unstable) → actual object

Essence: WINDOWS programs do not use physical addresses to identify a memory block, file, task or dynamically loaded module. On the contrary, WINDOWS API assigns certain handles to these items and returns the handle to the application program, and then passes the handle to operate.

But it must be noted that every time the program is restarted, the system cannot guarantee that the handle assigned to the program is still the original handle, and most of the cases are indeed different. If we regard entering a cinema to watch a movie as the startup and operation of an application, then the handle assigned by the system to the application is always different, which is the same as the fact that the tickets sold to us by the cinema are always a different seat every time. .

Processes and Applications

The difference between a process and an application program is that the application program is stored as a static file in the storage space such as the hard disk of the computer system, while the process is a system resource management entity maintained by the operating system under dynamic conditions.

Processes and threads under Linux

Executable files consist of instructions and data. A process is an instance of an executable file running on a computer for specific input data. If the same executable program file operates on different input data, it is two different processes.
A thread is an execution path of a process, which contains an independent stack and CPU register state. Each thread shares all the resources of the process it is attached to, including open files and page tables (so it also shares the entire user mode address space) , signal identification and dynamically allocated memory, etc. The relationship between threads and processes is: threads belong to processes, threads run in the process space, and threads generated by the same process share the same physical memory space. When the process exits, the threads generated by the process will be forced to exit and cleared.
Linux adopts a 1:1 thread model outside the core, that is, one core process (lightweight process) corresponds to one thread, and thread scheduling is equivalent to process scheduling, which is handed over to the core to complete, while other tasks such as thread cancellation and synchronization between threads , are all done in the out-of-core thread library. Therefore, a process can be regarded as a group of threads. This group of threads has the same thread group number (TGID). This TGID is the ID number of the process attached to this group of thread programs. See the LWP number.
For convenience, from now on we use tasks to replace processes and threads, that is, whenever we mention tasks, we refer to threads and processes, unless we want to emphasize the difference between threads and processes. The cycle of a task begins when it is forked until it disappears from the process table. A process includes: text segment (text), data segment (data), stack segment (STACK) and shared memory segment (SHARED MEMORY).
Reprint Statement : This article is reprinted from: http://www.cnitblog.com/Patrick/archive/2006/12/23/20997.html

Guess you like

Origin blog.csdn.net/qq_41841073/article/details/127769643