Run execl, execle, execlp, execv, execve, and execvp functions in the process, execleexecvp

Source: Internet
Author: User

Run execl, execle, execlp, execv, execve, and execvp functions in the process, execleexecvp
Abstract:This article describes how to run new code in the process and the basic usage of exec functions.

Run new code in the process

After using the fork function to create a sub-process, if you want to run a new program in the current sub-process, you can call the exec function to execute another program. when a process calls the exec function, the user space resources (body, Data, heap, and stack) of the process are completely replaced by the new program, and the new program is executed from the main function. because no new process is created when the exec function is called, the process ID is not changed, that is, the kernel information is not modified.
There are a total of 7 exec functions available. The difference between these functions is that they indicate whether the path or file name is used for the new program. If the file name is used, search for the program in the PATH described by the system's PATH environment variable. When using parameters, use the parameter list or the argv [] array.
1.exe c series Functions Function Definition:
# Include <unistd. h>
Int execl (const char * pathname, const char * arg0,.../* (char *) 0 */);
Int execv (const char * pathname, char * const argv []);
Int execle (const char * pathname, const char * arg0,.../* (char *) 0, char * const envp [] */);
Int execve (const char * pathname, char * const argv [], char * const envp []);
Int execlp (const char * filename, const char * arg0,.../* (char *) 0 */);
Int execvp (const char * filename, char * const argv []);
Int fexecve (int fd, char * const argv [], char * const envp []);
Return Value:If the execution is successful, no-1 is returned. The failed code is stored in errno.
The first four functions take the path name as the parameter, the last two take the file name as the parameter, and the last one uses a file descriptor as the parameter.
2. function analysis when filename is specified as the parameter:
1) if filename contains/, it is regarded as the path name.
2) Otherwise, search for executable files in the directories indicated by PATH.
2.1 execl () function int execl (const char * pathname, const char * arg0,.../* (char *) 0 */);
The execl () function is used to execute the program pointed to by the parameter path string. The second and later parameters represent the list of parameters passed during the execution file, the last parameter must be a null pointer to indicate that the parameter list is null.
Example 1: demonstrate the basic usage of the exec () function.
#include <unistd.h>#include <stdio.h>#include <sys/types.h>#include <stdlib.h>int main(){        pid_t pid;        pid = fork();        if(pid<0)        {                printf("error fork:%m\n");                exit(-1);        }        else if(pid==0)        {                //                execl("/bin/ls","ls","-l","/etc",(char *)0);        }        else        {                printf("parent process\n");        }        return 0;}

Output:

 
2.2 execle () function int execle (const char * pathname, const char * arg0,.../* (char *) 0, char * const envp [] */);
The execle () function is used to execute the program pointed to by the parameter path string. The second and later parameters represent the list of parameters passed during the execution file, the last parameter must point to a new environment variable array, that is, the environment variable of the new execution program.
Example 2:
#include <unistd.h>int main(int argc, char *argv[], char *env[]){        execle("/bin/ls","ls","-l", "/etc",(char *)0,env);        return 0;}

Output:

 
2.3 execlp () function int execlp (const char * filename, const char * arg0,.../* (char *) 0 */);
The execlp () function searches for the string indicated by the first parameter in the directory indicated by the PATH environment variable and then runs the file, the second and later parameters represent the list of parameters passed during the execution file. The last parameter must be a null pointer.
Example 3:
#include <unistd.h>int main(){        execlp("ls", "ls", "-l", "/etc", (char *)0);        return 0;}

Output:

 
2.4 execv () function int execv (const char * path, char * const argv []);
The execv () function is used to execute the program pointed to by the path string parameter. The second is the list of program parameters maintained by the array pointer. The last member of the array must be a null pointer.
Example 4:
#include <unistd.h>int main(){        char *argv[] = {"ls", "-l", "/etc", (char *)0};        execv("/bin/ls", argv);        return 0;}

Output:

 
2.5 execvp () function int execvp (const char * file, char * const argv []);
The execvp () function searches for the string indicated by the first parameter in the directory indicated by the PATH environment variable and then runs the file, the second and later parameters represent the list of parameters passed during file execution. The last member must be a null pointer.
Example 5:
#include <unistd.h>int main(){        char *argv[] = {"ls", "-l", "/etc", (char *)0};        execvp("ls", argv);        return 0;}

Output:

 
Several functions are very similar, but they do not have a better way to remember them. For the moment, let's take a look at the different calling methods between them through a simple example. letter associations are also unreliable:
The letter p indicates that the function takes filename as the parameter and uses the PATH environment variable to find the executable file.
The letter l indicates that the function takes a parameter table, which is mutually exclusive with the letter v.
The letter v indicates that this function is used to obtain an argv [] vector.
The letter e indicates that this function is used to retrieve the array of envp.

To be continued ......

Author: my personal abilities are limited, just for reference. If the reader finds any errors in this article, please submit them.
-- Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Do not build a high station in the sand float, calm down and slowly accumulate -----------------------------------------Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------
C Language Environment Variables

Execve (Execution file)
In the parent process, fork a child process and call the exec function in the child process to start a new program. There are a total of six exec functions, of which execve is a kernel-level system call. Other (execl, execle, execlp, execv, execvp) are the library functions that call execve.
Header file
# Include <unistd. h>
Define functions
Int execve (const char * filename, char * const argv [], char * const envp []);
Function Description
Execve () is used to execute the file path represented by the filename string parameter. The second parameter is passed to the execution file using an array pointer and ends with a NULL pointer, the last parameter is an array of new environment variables passed to the execution file.
Return Value
If the execution is successful, the function will not return. If the execution fails,-1 will be returned. The cause of the failure is stored in errno.
Error Code EACCES
1. the file to be executed does not have the executable permissions of the user.
2. The file system of the file to be executed is mounted in noexec mode.
3. The file or script translator to be executed is not a general file.
EPERM
1. The process is in tracing mode, and the executor does not have the root permission. The file to be executed has the SUID or SGID bit.
2. The file system of the file to be executed is mounted in nosuid mode. The file to be executed has SUID or SGID, but the performer does not have root permission.
The E2BIG parameter array is too large.
ENOEXEC cannot determine the format of the execution file to be executed. It may be a format error or cannot be executed on this platform.
The string address specified by the EFAULT parameter filename is out of the accessible space range.
The length of the string specified by the ENAMETOOLONG parameter filename is too long.
The file specified by the ENOENT parameter filename string does not exist.
Insufficient ENOMEM core memory
The directory path contained in the filename string of the ENOTDIR parameter is not a valid directory.
The directory path contained in the filename string of the EACCES parameter cannot be accessed and the permission is insufficient.
Too many symbolic connections in ELOOP
The file to be executed by ETXTBUSY has been opened by other processes and is writing data to the file.
Eio I/O Access Error
ENFILE has reached the total number of open files allowed by the system.
EMFILE has reached the total number of files that the system allows a single process to open.
EINVAL: the ELF execution format of the file to be executed is not only one PT_INTERP section.
The eisdir elf translator is a directory.
The elibbad elf translator has a problem.
Example
# Include <unistd. h>
Main ()
{
Char * argv [] = {"ls", "-al", "/etc/passwd", (char *) 0 };
Char * envp [] = {"PATH =/bin", 0}
Execve ("/bin/ls", argv, envp );
}
Run
-Rw-r -- 1 root 705 Sep 3 13: 52/etc/passwd
Reference: Baidu encyclopedia

Where does Execl allocate hyperlinks?

Execl (Execution file)
Related functions:
Fork, execle, execlp, execv, execve, execvp
Header file:
# Include <unistd. h>
Define functions:
Int execl (const char * path, const char * arg ,....);
Function Description:
Execl () is used to execute the file path represented by the path string parameter. The following parameter indicates that the previous argv (0), argv [1]…, The last parameter must end with a NULL pointer.
Return Value:
If the execution is successful, the function will not return. If the execution fails,-1 will be returned. The cause of the failure is stored in errno.
Example:
# Include <unistd. h>
Main ()
{
Execl ("/bin/ls", "ls", "-al", "/etc/passwd", (char *) 0 );
}
Run/* Run/bin/ls-al/etc/passwd */
-Rw-r -- 1 root 705 Sep 3 13: 52/etc/passwd
Www.docin.com/p-11461545.html

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.