Linux Fork EXEC, etc.

Source: Internet
Author: User

Http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html

Http://www.cnblogs.com/jimwind/archive/2012/12/26/2834147.html

Http://www.cnblogs.com/hicjiajia/archive/2011/01/20/1940154.html

Linux the next process in memory has three parts of the data, namely "code Snippet", "Stack segment" and "Data segment". In fact, people who have learned assembly language must know that the general CPU has the above three kinds of segment registers, in order to facilitate the operation of the operating system. 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). There are a lot of details here, which are limited to a few pages. If the system runs several identical programs at the same time, they cannot use the same stack segment and data segment.

1. Fork creates a child process with two return values. Returns 0 for the child process, which returns greater than 0 for the parent process.

The fork function starts a new process, as we said before, that this process is almost a copy of the current process: the child process and the parent process use the same Code snippet, and the child process replicates 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.

2. Exec runs the new executable file, replacing the data segment, code snippet, and stack segment of the original calling process. In general, after you run the fork, exec executes in the child process.

Let's see how a process can initiate 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, the following I only take EXECLP as an example, what other functions are different from EXECLP, Use the Manexec command to understand their specifics.

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. (But some of the Exec class functions also allow the inheritance of information such as environment variables.) )

An example of using together

1#include <errno.h> 2#include <stdio.h> 3#include <stdlib.h> 4 5char command[256];  6void Main () 7{8   int RTN;/* The return value of the child process */9   while (1) {Ten       * * reads the command to execute from the terminal */11       printf (">");       command, stdin);       command[strlen (command)-1] = 0;14       if (fork () = = 0) {/* child process executes this command */15          EXECLP (comma nd, NULL);          /* If the EXEC function returns indicating that no command was executed properly, print error message */17          perror (command);          exit (errno);       }20       else {/* parent process, wait for the child process to end, and print the return value of the subprocess */21          Wait (&RTN);          printf ("Child process return%d\n", RTN);   }25}

  

Linux Fork EXEC, etc.

Related Article

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.