Another article of processes and threads

xiaoxiao2021-03-19  200

First, process and thread

A prominent program is called a process (Process), and the process should take up system resources. The control leads during the process execution are called thread, the status of the program counter, and the status of other registers in the CPU showed the control clue.

Second, why need multiple processes / threads

I remember that there is a program that has been manipulated by everyone as a background, which is Print.exe (provided by MS), used for background printing, can achieve multiple tasks to a certain extent, but DOS is not a multi-task environment, So barely implement too much time limit. Subsequently, Win 3.X, although the OS supports multiple tasks, but strictly said that the support of multi-process is not enough, mainly in the process of supporting communication between processes, and some traditional IPC methods are not available. Later, in Winnt fully implemented multi-process / multi-threaded support, of course, Win9x / 2k now provides support in this area.

What is a process: an ordinary explanation is that the process is an execution of the program.

What is thread: thread can be understood as a segment of a segment executed in the process. In multi-tasking environments, the following concept can help us understand the difference between the two:

The process is independent, which is manifested in memory space, context environment, and thread runs in the process space. Generally, the process is unable to break through the memory space within the process boundary to access other processes, and the thread is in the process space, so the thread generated by the same process shared the same memory space.

The two codes in the same process cannot be executed at the same time, unless the thread is introduced. Threads are processed, and when the process exits, the threads generated by the process will be forced to exit and clear. Thread occupied resources less than the resources occupied by the process.

Process and threads can have priority. In the thread system, the process is also a thread. The process can be understood as the first thread of a program. One of the simplest examples is to paint multiple beats on the screen, and we can use a thread to do with a thread. But there is little independently of such threads, and there is very few cases of data exchange.

Let's take an example, an application to complete two tasks: Each time you generate 1000 random number write files and read the data from the file and the random number is a round card, do 100 times, and Save files with 100 different files. The traditional approach is as follows:

Void do_this (void) {

For (int i = 0; i <100; i ) {

/// Step 1: generate 1000 randam number; Write to File;

/// Step 2: read from file; draw circle;

}

}

If the concept of multi-process is introduced, the implementation method can be changed to:

Void do_this (void) {

CreateProcess ("do_rand.exe", ...);

CreateProcess ("Draw_Circle.exe", ...);

}

//do_rand.exe

Void do_rand (void) {for (INT i = 0; i <100; i ) {

/// Step 1:

Generate 1000 Randam Number;

Write to file; Wait Draw_circle Finish Last Tasktell Draw_Cricle Data Ready

}

}

//draw_circle.exevoid Draw_circle (void) {

For (int i = 0; i <100; i ) {

/// Step 2

Set Flag of Last Task FinishWait Data ReadyRead from File; Draw Circle;}

In the multi-process, we introduce more control methods. First, do_rand must wait for DRAW_CIRCLE to be idle after preparing data, this is: only one process is in progress, so it must ensure that the current submitted Data Ready request Can be received. Due to the use of different files, it is not necessary to judge the usage of the file.

If you use a thread, we can further transform the program, unused the file to save the data, and save the data with global variables:

Void do_this (void) {

CreateThread ("Do_Rand", ...); // Parameter is a thread entry instead of executing program CreateThread ("Draw_Circle", ...);

} Global Int girandnum [1000]; void do_rand (void) {

For (int i = 0; i <100; i ) {

/// step 1

Local IRANDNUM [1000];

Generate 1000 Randam Number;

Get Access of girandnum;

Memcpy (Girandnum, Irandnum, ...);

Release Access of Girandnum;

WAIT DRAW_CIRCLE FINISH LAST TASK

Tell Draw_Cricle Data Ready}

} void Draw_circle (void) {

For (int i = 0; i <100; i ) {

/// Step 2

Set Flag of Last Task FinishWait Data Readylocal Irantnum [1000]; Get Access of GirandNum; Memcpy (Irandnum, Girandnum, ...); Release Access of Girandnum; Draw Circle;

}

Here, the global variables are saved, and the resources used by the program are smaller than the case where the process is used, and the efficiency is the same. It is introduced here to the judgment of global data usage, which is to ensure that global data is modified by DO_RAND when read by DRAW_CIRCLE. This is the concept of a data synchronization.

By the above example, you can see how efficient and save resources when using multithreading. The last thread is not efficient to increase the efficiency over a single CPU host, and different thread code on the multi-CPU host can be assigned to different hosts. However, the advantages of multi-process / threads in efficiency and speed compared to single processes are very obvious.

As the multi-line process / program uses simultaneously, there will be many other problems, such as how data exchanges (in the above example we use files to save intermediate data, there are still many ways to exchange data between processes), Data How to synchronize (guarantees that some data is written only by one code), how to coordinate the operation between the process / thread (the continuation of a process is to wait for other processes to complete certain operations).

转载请注明原文地址:https://www.9cbs.com/read-130046.html

New Post(0)