Exploration of exec function family in Linux

Source: Internet
Author: User
It is mainly explained by calling the execution file generated by another C code in one C program code. It is called by the exec system. In fact, there is no exec () function in Linux. exec refers to a group of six functions, including & lt; unistdh & gt;

It is mainly explained by calling the execution file generated by another C code in one C program code.

It is called by the exec system. In fact, in Linux, there is no exec () function form. exec refers to a group of functions, with a total of six functions:

# Include

Extern char ** environ;

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 []);

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.

L indicates calling in the form of a parameter list

V indicates calling in the form of parameter arrays

E indicates that environment variables can be passed

P indicates the file to be searched for in the PATH. if the given PATH is not an absolute PATH, the file with the corresponding name will be searched in the PATH. if the PATH is not set, it will be in/bin by default, search under/usr/bin.

Example:

# Include
 
  
Void main () {int I; if (fork () = 0) {/* sub-process PROGRAM * // for (I = 1; I <10; I ++) printf ("This is child process \ n");} else {/* parent process PROGRAM * // for (I = 1; I <10; I ++) printf ("This is parent process \ n ");}}
 

Running result:



Call the above to generate a binary file:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
// Char command [256]; void main () {int rtn; /* return value of the sub-process * // * read the command to be executed from the terminal */printf (">"); // fgets (command, 256, stdin ); // command [strlen (command)-1] = 0; if (fork () = 0) {/* sub-process executes this command * // execlp ("test", NULL); // execlp ("test", NULL); // execlp ("ls ", "-al", NULL); execl ("/home/buyingfei888/test", NULL);/* If the exec function returns, the command is not executed normally, print the error message * // perror (); exit (errno);} else {/* parent process. wait until the child process ends, and print the sub-process return value */wait (& rtn); printf ("child process return % d \ n", rtn );}}
    
   
  
 

Running result:


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.