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: