Detailed analysis of multi-process programming in Linux (1)

Source: Internet
Author: User

1. Understand the process structure in Linux

The next process in Linux has three parts of data in the memory: "data segment", "Stack segment", and "code segment". In fact, people who have learned assembly languages must know that, generally, the CPU is like I386 and has the above three register segments to facilitate the operation of the operating system. "Code segment", as its name implies, stores the data of program code. If several processes on the machine run the same program, they can use the same code segment.

The stack segment stores the return address of the subroutine, the parameters of the subroutine, and the local variables of the program. The data segment stores the global variables of the program, constants, and dynamic data space allocated, such as space obtained using functions such as malloc ). There are many details here, so we will not discuss them much here. If the system runs several identical programs at the same time, the same stack segment and data segment cannot be used between them.

Ii. How to use fork 

In Linux, the system call that generates a new process is the fork function. This function name means "Forks" in English. Why is this name used? Because a process is running, if fork is used, another process is generated, so the process is "Forked", so this name is very good. The following describes how to use fork. This program demonstrates the basic framework of using fork:

Void main (){
Int I;
If (fork () = 0 ){
/* Sub-process program */
For (I = 1; I <1000; I ++)
Printf ("This is child process \ n ");
Else {
/* Parent process program */
For (I = 1; I <1000; I ++)
Printf ("This is process \ n ");

After the program runs, you can see that the screen displays one thousand pieces of information each printed by the child process and the parent process. If the program is still running, you can use the ps command to see that there are two running programs in the system.

So what happens when this fork function is called? When a program calls the fork function, the system prepares the preceding three segments for a new process. First, the system allows the new process and the old process to use the same code segment, because their programs are the same, the system copies a copy of the data segment and stack segment to the new process. In this way, all data of the parent process can be left to the child process. However, once a child process starts running, it inherits all the data of the parent process, but in fact the data has been separated and there is no impact between them, that is, they no longer share any data. If the two processes want to share any data, they need to use another set of functions, such as shmget, shmat, and shmdt. Now there are two processes. For the parent process, the fork function returns the process Number of the subroutine, and for the subroutine, the fork function returns zero. In this way, for the program, as long as you determine the return value of the fork function, you will know whether you are in the parent process or child process.

Readers may ask, if a large program is running and its data segments and stacks are large, and a fork will be copied once, isn't the system overhead of fork very high? In fact, UNIX has its own solution. As you know, CPU generally allocates space in units of "pages", such as INTEL's CPU, the page of which is usually 4 K bytes, both the Data Segment and the stack segment are composed of many "pages". The fork function copies these two segments, but they are logical and not physical. That is to say, when fork is actually executed, the data segments and stack segments of the two processes in the physical space are still shared. When a process writes data, at this time, the data between the two processes is different, and the system physically separates the different "pages. The space overhead of the system can be minimized.

A little humorous: The following shows a small program that is enough to "Screw Up" Linux. Its source code is very simple:

void main() 
for(;;) fork();

This program does nothing, that is, fork in an endless loop. The result is that the program continuously produces processes, and these processes continuously generate new processes. Soon, the process of the system is full, the system is "overwhelmed" by so many constantly generated processes ". No need for root. Anyone running the above program is enough to let the system die. Haha, but this is not the reason for Linux's insecurity, because as long as the system administrator is smart enough, he or she can set the maximum number of processes that can be run for each user in advance, as long as it is not root, the number of processes that can run may be less than 10 of the total number of processes that the system can run, so that the system administrator can deal with the above malicious programs.

3. How to start execution of another program

Next let's take a look at how a process can start the execution of another program. In Linux, exec functions are used. There are more than one exec function, but they are roughly the same. In Linux, they are: execl, execlp, execle, execv, execve and execvp. I will only use execlp as an example. What is the difference between other functions and execlp? Please use the manexec command to learn about their details.

Once a process calls the exec function, it is "dead". The system replaces the code segment with the code of the new program and discards the original data segment and stack segment, and allocate new data segments and stack segments for the new program. The only difference is the process number. That is to say, for the system, it is the same process, but it is already another program. However, some exec functions can inherit information such as environment variables .)

So what if my program wants to start the execution of another program but still wants to continue running? That is, combined with fork and exec. The following code starts other programs:

Char command [256];
Void main ()
Int rtn;/* the return value of the sub-process */
While (1 ){
/* Read the command to be executed from the terminal */
Printf ("> ");
Fgets (command, 256, stdin );
Command [strlen (command)-1] = 0;
If (fork () = 0 ){
/* The sub-process executes this command */execlp (command, command );
/* If the exec function returns, the command is not executed normally and the error message is printed */
Perror (command );
Exit (errorno );
Else {
/* Parent process. Wait until the child process ends and print the return value of the child process */
Wait (& rtn );
Printf ("child process return % d \ n",. rtn );

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: 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.