5 process primitives: execl (), execlp (), execle (), execv (), execvp (), execvp (), execve (), execlpexecle

Source: Internet
Author: User

5 process primitives: execl (), execlp (), execle (), execv (), execvp (), execvp (), execve (), execlpexecle
Zookeeper

Header files dependent on the 1.exe c family

# Include <unistd. h>

Extern char ** environ;

2 function declaration

// The first parameter is the absolute location of the executable program.

// The second parameter: The parameter required when the executable program runs. It is a variable parameter, separated by commas (,).

// When the parameter ends, the last parameter is: NULL

Int execl (constchar * path, const char * arg ,...);

 

// Note: p is added to the function name, which indicates that the PATH configured in the system's environment variable is called. Run Echo $ PATH to view the environment variable.

 

// The first parameter: you only need to write the file name. You do not need to write the full path. The following parameters are the same as the preceding parameters.

Int execlp (constchar * file, const char * arg ,...);

 

// The first parameter: full path

// Envp [], which indicates that an array of environment variables is passed.

Int execle (constchar * path, const char * arg,

..., Char * const envp []);

 

// The difference between this and the first exec function is that the parameters here are not variable parameters, but passed through an array.

Int execv (constchar * path, char * const argv []);

 

// The difference between execvp and execv is only the difference between file names and paths.

Int execvp (constchar * file, char * const argv []);

 

// Transfer the environment variable at this time

Int execvpe (constchar * file, char * const argv [],

Char * const envp []);

 

3. Note:

A: If these functions are called successfully, A new program is loaded and executed starting from the startup code. If an error occurs,-1 is returned, therefore, the exec function only returns error values but does not return successful values.

B: These function prototypes seem easy to mix, but they are easy to remember as long as they have mastered the rules. Without the letter p

Path). The first parameter of the exec function must be the relative or absolute path of the program, such as "/bin/ls" or "./

A. out "instead of" ls "or" a. out ". For functions with letters p:

If the parameter contains/, it is regarded as the path name.

Otherwise, the program name without a PATH is considered to be searched in the directory list of the path environment variable.

The exec function with the letter l (list) requires that each command line parameter of the new program be passed as a parameter.

Given it, the number of command line parameters is variable, so the prototype of the function contains ...,... The last variable parameter in should be

NULL indicates the role of sentinel. For a function with the letter v (vector), you should first construct a point

The pointer array of each parameter, and then pass the first address of the array as a parameter to it. The last pointer in the array should also

It is NULL, just like the argv parameter of the main function or the environment variable table.

C: For exec functions ending with e (environment), you can pass a new environment variable table to it. Other exec functions still use the current environment variable table to execute new programs.

D: exec call example:

Char * const ps_argv [] = {"ps", "-o", "pid, ppid, pgrp, session, tpgid, comm", NULL };

Char * const ps_envp [] = {"PATH =/bin:/usr/bin", "TERM = console", NULL };

Execl ("/bin/ps", "ps", "-o", "pid, ppid, pgrp, session, tpgid, comm", NULL );

Execv ("/bin/ps", ps_argv );

Execle ("/bin/ps", "ps", "-o", "pid, ppid, pgrp, session, tpgid, comm", NULL, ps_envp );

Execve ("/bin/ps", ps_argv, ps_envp );

Execlp ("ps", "ps", "-o", "pid, ppid, pgrp, session, tpgid, comm", NULL );

Execvp ("ps", ps_argv );

E: in fact, only execve is a real system call, and the other five functions finally call execve. Therefore, execve is in man Manual section 2nd, and other functions are in man Manual section 3rd. Shows the relationship between these functions.

 

4. Case study:

Running result:

5. Because the exec function only has an error returned value, an error is returned, so you do not need to judge its

Return value. You can call perror directly later. Note that two "ps" parameters are passed when execlp is called.

"Ps" is the program name, execlp function needs to find the program in the PATH environment variable and execute it, and second

"Ps" is the first command line parameter. The execlp function does not care about its value, but simply passes it to the ps program.

Ps program can obtain this parameter through main function argv [0.

After exec is called, the original file descriptor is still open. This can be used to achieve I/O redirection.

Let's take a look at a simple example to convert the standard input into uppercase and then print it to the standard output:

Running result:

Running result:

Analysis:

The wrapper program opens the command line parameter as a file name, redirects the standard input to this file, and then calls

Exec executes the upper program. At this time, the opened file descriptor is still open. The upper program is only responsible for converting the characters read from the standard input to uppercase, and does not care whether the standard input corresponds to a file or a terminal.

Exec

L command line parameter list

Use the path variable when searching for p files

V use the command line parameter Array

E. Use the environment variable array instead of the original environment variable of the process. Set the environment variable for the newly loaded program.

 

 

 

 

 

 

 

 

 

 


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

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

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.