Embedded Linux Learning---process (1)

Source: Internet
Author: User

What is a process? When a user-typed command executes a program, it initiates a process for the system. However, unlike the program, in this process, the system may need to start one or more processes to complete independent multiple tasks. The main content of multi-process programming includes process control and interprocess communication.

1 structure of the process under Linux

Linux the next process in memory has three parts of the data, namely "code Snippet", "Stack segment" and "Data segment". These three parts are also necessary to form a complete sequence of executions.

"Code Snippets", as the name implies, is the data stored in the program code, if the machine has a number of processes running the same program, then they can use the same code snippet. The "stack segment" holds the return address of the subroutine, the parameters of the subroutine, and the local variables of the program. Data segments store The program's global variables, constants, and data spaces for dynamic Data allocation (such as those obtained with functions such as malloc). If the system runs several identical programs at the same time, they cannot use the same stack segment and data segment.

2 Process Control under Linux

Linux environment, there are two basic operations for creating and modifying processes: the function fork () is used to create a new process, which is almost a full copy of the current process, and the function family exec () is used to start another process to replace the currently running process.

2.1 Fork ()

Fork is the meaning of "fork" in English. Why do you take that name? Because a process is running, if fork is used, another process is created, and the process is "forked", so the name gets very image. Here's a look at how to use fork specifically, a program that demonstrates the basic framework for using fork:

void Main () {

int i;

if (fork () = = 0) {

/* Child Process Program */

for (i = 1; I <1000; i + +) printf ("This is a child process/n");

}

else {

/* Parent Process Program */

for (i = 1; I <1000; i + +) printf ("This is Process process/n");

}

}

Once the program is running, you can see the 1000 messages printed on the screen alternating between the child process and the parent process. If the program is still running, you can see that there are two of them running in the system with the PS command.

So what happens when we call this fork function? The fork function starts a new process, which is almost a copy of the current process: the child process and the parent process use the same Code snippet, and the child process copies the stack segment and data segment of the parent process. In this way, all the data for the parent process can be left to the child process, but once the child process starts running, it inherits all the data from the parent process, but the data is actually separated and no longer affects each other, that is, no data is shared between them. When they want to interact with the information, only through interprocess communication, this will be our content below.

Since they are so alike, how will the system differentiate them? This is determined by the return value of the function. For the parent process, the fork function returns the subroutine's process number, and for the subroutine, the fork function returns zero. In the operating system, we can see the different process number with the PS function, for the parent process, its process number is given by the system call lower than it, and for the child process, its process number is the fork function on the parent process return value. In programming, both the parent and child processes invoke the code below the function fork (), and we use the fork () function to if......else the different return values of the parent-child process ... Statement to enable a parent-child process to accomplish different functions. We see that the above example executes when two messages are printed out interactively, which is the result of the independent execution of the parent-child process, although our code seems to be no different from the serial code.

The reader may ask, if a large program is running, its data segments and stacks are large, and one fork is copied once, then is the system overhead of fork very large? Whether the data segment or the stack segment is made up of many "pages", the fork function copies the two segments, just "logical", not "physical", that is, the physical space on the two process data segments and stack segments are still shared, when a process has written a certain data, At this point the data between the two processes is different, the system will have a different "page" from the physical separation. The overhead of the system can be minimized.

Here is a small program that is enough to "kill" Linux, and its source code is very simple:

void Main ()

{

for (;;) fork ();

}

This program does nothing, is the death of the Loop fork, the result is the process of continuous production, and these processes continue to generate new processes, and soon, the system is full of processes, the system is so many constantly produced by the process "to die." Of course, as long as the system administrator sets the maximum number of processes that can be run in advance for each user, this malicious program will not be able to complete the attempt.

2.2 Exec () function family

Let's take a look at how a process starts execution of another program. You want to use the EXEC function family in Linux. The system call EXECVE () replaces the current process with a specified program whose parameters include the file name (filename), the argument list (argv), and the environment variable (ENVP). The EXEC function family is of course more than one, but they are roughly the same, in Linux, they are: Execl,execlp,execle,execv,execve and EXECVP, below I only take EXECLP as an example.

Once a process calls the Exec class function, it is itself "dead", the system replaces the code snippet with the new program's code, discards the original data segment and stack segment, and assigns new data segment and stack segment to the new program, the only one left is the process number, that is, for the system, or the same process, But it's already another program.

So what if my program wants to start another program's execution but I still want to run it? That is the use of the combined fork and exec. The following code shows how to start running other programs:

Char command[256];

void Main ()

{

int RTN; /* Return value of child process */

while (1) {

/* Read the command to execute from the terminal */

printf (">");

Fgets (command, stdin);

Command[strlen (command)-1] = 0;

if (fork () = = 0) {

/* Child process executes this command */

EXECLP (command, command);

/* If the EXEC function returns, the command is not executed correctly, printing the error message */

perror (command);

Exit (Errorno);

}

else {

/* Parent process, wait for the child process to end, and print the return value of the child process */

Wait (&RTN);

printf ("Child process return%d/n",. Rtn);

}

}

}

The program reads the command from the terminal and executes it, and after execution completes, the parent process continues to wait for the read-in command from the terminal.

In this section, we also talk about the system () and Popen () functions. The system () function calls fork () before calling exec () to execute the user's login shell, which is used to find the executable's command and parse the parameters, and finally it waits for the end of the child process using one of the wait () function families. The function Popen () is similar to the function system (), but it calls the pipe () function to create a pipeline through which to complete the standard input and standard output of the program.

Embedded Linux Learning---process (1)

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.