Introduction to multi-process programming under Linux

zhaozj2021-02-16  73

Two years ago, the article took over the facade.

---------------------------------------------------------------------------------------------------------------------------------------

Introduction to multi-process programming under Linux

(Author: mikespook | Published: 2002-12-8 | Views: 272)

Keywords: Linux, multi-process, fork (), wait () preface: This article is just a guideline that is just the same as me. If you are a master, or is not interested in programming. Please don't waste time here. Multi-process is a very useful thing. Remember the TCP Connect scanner I introduced? Is it very slow? If you use a multi-process segmentation to scan port, you will find the speed greatly improve. Let's take a look at how to make multi-process programming under Linux. First, briefly introduce the functions we have to use: fork (), Wait (). The Fork () function is a very interesting function. He can build a new process and divide the current process into a parent process and sub-process. All pages of the original process are divided into two copies when calling the fork () function, so the parent process and sub-process use the same image. The difference between this function and the normal function is that the function will return twice if the call success will return, and the PID of the sub-process in the parent process is returned in the child process. After success, the parent process and sub-process will continue after the fork () function. If the function call is unsuccessful, return once, the return value is -1. Because the child process will exit when the process is running, it will not be cleared from the list of processes. To send a sigchld (or sigcld) signal to the parent process, the parent process is confirmed that the sub-process will only exit. The child process is in the "Zombie" status during waiting for the parent process to confirm. So we need to use the wait () function. If there is a child process in the "zombie" state when calling the wait () function, the function is returned immediately, and the child process is cleared from the memory; otherwise, the main process will hang until one of the processes exits. There is a significant disadvantage that the WAIT () function is directly called is that the parent process will hang and cannot do other tasks. The solution is to intercept the signal sigchld (or sigcld), which I will give you a simple story in the article that will be described later. Old rules, learn multi-process program through source code. / *--------------------------------------------------------------------------------------------- ----------- * // * mikespook * // * EXERCISE FUNCTION fork () and Wait () * // * 2002.5.28 * / # include #include #include

#DEFINE FAC_N 65535

/ * Sub-process calling function, here I use a loop to simulate a large background operation. * /

Void Big_Loop (INT N);

/ * The function of the parent process call is actually not put in the function, but it is better to look better for the program's structure or put it in the function. * /

Voidinfut_information ();

int main ()

{

/ * Process number * /

PID_T PID;

/ * The program is "bifurcated" here, the new process is created * /

PID = fork ();

/ * Judgment is a parent process or a child process * / by the return value of fork ().

Switch (PID) {

/ * Return -1, very unfortunate, create a process failed. Maybe there is not enough memory space, or there may be too many processes. * /

Case -1:

PERROR ("fork / n");

Break;

/ * Return 0, now run in the child process, then call the operator of the child process. * / cas 0:

/ * A loop running 65535 times, if your machine is too fast, you can't see the effect of both processes simultaneously, then increase the number of cycles. Or use a SLEEP () function * /

BIG_LOOP (FAC_N);

/ * Get the PID of the child process, you can see that the PID of the child process and the parent process is different (the PID of the child process is larger than the parent process, because it is created after the parent process is running). * /

Printf ("PID:% D / N", getPid ());

Break;

/ * Haha, the return is not a mistake, it is not a child process, that is the parent process. * /

DEFAULT:

/ * Here you have entered 4 numbers * /

INPUT_INFORMATION ();

/ * Acquire the PID of the child process. * /

Printf ("PID:% D / N", getPid ());

Break;

}

/ * Wait, the child process does not quit, your parent process cannot be exited. * /

Wait ();

exit (0);

}

/ * BIG_LOOP: Simple, I will understand, I don't explain it. * /

Void Big_Loop (INT N)

{

INT I;

For (i = 0; i

Switch (i% 4) {

Case 0:

PUTCHAR ('-');

Break;

Case 1:

Putchar ('/');

Break;

Case 2:

PUTCHAR ('|');

Break;

Case 3:

PUTCHAR ('//');

Break;

}

PUTCHAR ('/ b');

}

}

/ * INPUT_INFORMATION: Simple, I will understand, nor is it. * /

Void Input_information ()

{

INT N_TABLE [4], i;

For (i = 0; i <4; i ) {

Printf ("Number% D: / T", I);

Scanf ("% D", & n_table [i]);

}

Printf ("NUMBER1 / TNUMBER2 / TNUMBER3 / TNUMBER4 / N");

Printf ("% D / T% D / T% D / T% D / N", N_TABLE [0], N_TABLE [1], N_TABLE [2], N_TABLE [3]);

}

/ *--------------------------------------------------------------------------------------------- ----------- * /

Similarly, I added a few more for everyone for reference.

The advantage of multi-process is to run multiple tasks in parallel. Since each independent memory space is used, it is not easy to make mistakes due to conflicts. But this will bring a certain amount of trouble to communications between processes. Of course, there are many ways, such as pipelines, messages, etc. can solve this problem. There is still a problem with multiple processes to waste in memory space. A process is a complete memory image, there are some data repeatedly, so that the memory space is very serious (I think this is the reason why the multi-thread is more than the multi-process, but unfortunately I haven't fully understood Linux. Multi-threading, or also discuss with you. After a while!). Also, I want to prompt is the above example I have used the wait () function so that the parent process runs back and waits for a sub-process to exit. You can try Wait (); this statement, look at what effect? The parent process runs out, we return to the prompt [MikesPook @ lazycat] $, and the child process continues to run. Sometimes we can use this to put a process in the background to run (such as Trojans ... Of course, I don't recommend you to make a wooden horse!). Ok, how is the multi-process programming under linux? Is it very simple? In fact, let two processes run independently, key difficulties are parent processes and sub-process sharing data for communication. I will slowly discuss with you in the future article (in fact, there are some things that have not been enlighten, I don't dare to take it out of people ^% ^).

Give everyone a good station: http://www.opengroup.org/onlinepubs/007908799/ Online MAN manual, what do not understand, you can query here. Unfortunately, it is E text.

Since I am a rookie, maybe there is anything wrong. I may also have some details I have not considered. If you know what you want to advice. The younger brother is grateful! !

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

New Post(0)