"Reprint" Linux Process Control-EXEC series exec system call

Source: Internet
Author: User

Inux Process Control-exec series is called the exec system call, actually in Linux, there is no EXEC () function form, exec refers to a set of functions, a total of 6, respectively:

#include <unistd.h>
int execl (const char *path, const char *arg, ...);
int EXECLP (const char *file, const char *arg, ...);
int execle (const char *path, const char *arg, ..., Char *const envp[]);
int execv (const char *path, char *const argv[]);
int EXECVP (const char *file, char *const argv[]);
int Execve (const char *path, char *const argv[], char *const envp[]);

Only EXECVE is the real system call, and the others are packaged library functions on this basis.
The function of the EXEC function family is to find the executable file according to the specified file name and use it to replace the contents of the calling process, in other words, execute an executable file inside the calling process. The executable file here can be either a binary file or a script file that can be executed under any Linux.
Unlike the general case, the function of the EXEC function family does not return after successful execution, because the entity that invokes the process, including the code snippet, the data segment, and the stack have been replaced by the new content, leaving only some surface information, such as the process ID, to remain as it is, rather "Jinchantuoqiao" in the "36 gauge". It looks like an old shell, but it's already infused with a new soul. Only the calls fail, they return a-1, which is then executed from the point of invocation of the original program.
Now we should understand how Linux executes the new program, and whenever there is a process that thinks that it cannot contribute to the system or its support, he can take the last bit of heat and call any exec to regenerate himself with a new face, or, more generally, If a process wants to execute another program, it can fork out a new process and call any exec, which looks like a new process is generated by executing the application.
In fact, the second situation is so pervasive that Linux is optimized for it, and we already know that fork will copy all the contents of the calling process into the newly generated subprocess, which is time consuming, and if we call exec immediately after the fork is finished, These hard copies will be erased immediately, which looks very uneconomical, so people have designed a "copy-on-write (Copy-on-write)" technology that does not immediately replicate the contents of the parent process when the fork is finished, but only when it is really practical, so if the next The statement is exec, it will not be wasted effort, but also improve efficiency.
/* EXEC.C */
#include <unistd.h>
Main ()
{
Char *envp[]={"Path=/tmp",
"User=lei",
"Status=testing",
NULL};
Char *argv_execv[]={"echo", "excuted by Execv", NULL};
Char *argv_execvp[]={"echo", "executed by EXECVP", NULL};
Char *argv_execve[]={"env", NULL};
if (fork () ==0)
if (Execl ("/bin/echo", "echo", "executed by Execl", NULL) <0)
Perror ("Err on Execl");
if (fork () ==0)
if (EXECLP ("echo", "echo", "executed by EXECLP", NULL) <0)
Perror ("Err on EXECLP");
if (fork () ==0)
if (Execle ("/usr/bin/env", "env", NULL, ENVP) <0)
Perror ("Err on Execle");
if (fork () ==0)
if (Execv ("/bin/echo", Argv_execv) <0)
Perror ("Err on Execv");
if (fork () ==0)
if (EXECVP ("echo", ARGV_EXECVP) <0)
Perror ("Err on EXECVP");
if (fork () ==0)
if (Execve ("/usr/bin/env", Argv_execve, ENVP) <0)
Perror ("Err on Execve");
}


The program calls 2 Linux common system commands, ECHO and Env. echo prints out the command line arguments that follow, and Env is used to list all environment variables.

Because the order in which each sub-process executes is uncontrollable, it is possible to have a confusing output-the results of each child process being printed mixed together rather than strictly in the order listed in the program.

Compile and run:
$ cc Exec.c-o EXEC
$./exec
Executed by execl
Path=/tmp
User=lei
Status=testing
Executed by EXECLP
Excuted by Execv
Executed by EXECVP
Path=/tmp
User=lei
Status=testing

From:http://blog.chinaunix.net/u1/53053/showart_425199.html

"Reprint" Linux Process Control-EXEC series exec system call

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.