Linux-exec function family and system functions

Source: Internet
Author: User
Reference: http://qzone.qq.com/blog/119994997-1236688022 http://hi.baidu.com/colin719/blog/item/f6ea44e782e1152fb938205c.html

 

The exec function family contains six functions:

# 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, const char * envp []);
Int execv (const char * path, const char * argv []);
Int execve (const char * path, const char * argv [], const char * envp [];
Int execvp (const char * file, const char * argv []);


Parameter description:

The first parameter of execl is the executable file including the path, followed by the list parameter. The first parameter in the list is the command path, followed by the parameter list, and must end with null.
The first parameter of execlp can use relative or absolute paths.
Execle, which includes a pointer to a list of custom environment variables. The list must end with null.
Execv, V indicates that a vector is received after the path, that is, a pointer to a parameter list. Note that the last item in this list must be null.
Execve, path receives a parameter list vector and can specify an Environment Variable list vector.
Execvp: the first parameter can use a relative or absolute path. V indicates that a parameter list vector is received later.

When exec is called, it replaces the code segment and Data Segment of the process that calls it (but the file descriptor remains unchanged) and returns directly to the parent process of the process that calls it. If an error occurs, return-1 and set errno.


Example:
# Include <unistd. h>
Int main (INT argc, char * argv [])
{
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 two common Linux system commands, ECHO and Env. Echo will print out the command line parameters that follow, and env will be used to list all environment variables.
As the execution sequence of each sub-process cannot be controlled, a chaotic output may occur-the printed results of each sub-process are mixed together, rather than strictly following the order listed in the program.
The most common errors:
In normal programming, if the exec function family is used, you must add an error judgment statement. Compared with other system calls, exec is prone to injury, and the execution file location, permissions, and many other factors can cause the call to fail.
The most common errors are:
1) the file or path cannot be found, and errno is set to enoent;
2) If the array argv and envp forget to end with null, errno is set to efault;
3) You are not authorized to run the file to be executed. errno is set to eacces.







System function:



Prototype:
# Include <stdlib. h>
INT system (const char * string );
Function:
The system function runs the command passed to it
String and waits for it to complete. The command is
Executed as if the command $ sh −c string has been given
To a shell.
[That is to say, system needs to use a shell to start the specified program]


Example:
# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Printf ("Running ps with system/N ");
System ("PS-Ax ");
Printf ("done./N ");
Exit (0 );
}

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.