Run new code in the process

Source: Internet
Author: User

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 ----------------------------------------------------------------------------------------------------------------------------------------------------------

Run new code in the process

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.