EXEC Replacement Process Impressions

Source: Internet
Author: User

Overview

Unix/linux takes a unique approach to creating a process that separates the process creation from the loading of a new process image ( the system call merges the two operations). The advantage is that there is more room to manage the two operations.

When we create a process, we usually replace the child process with a new process image, which can be done using the exec series function. Of course, theexec series functions can also replace the current process ( without calling fork, calling exec directly ).

function Family [Map page] information
       #include <unistd.h>       extern char **environ;       int execl (const char *path, const char *arg, ...);  L: Command list       int EXECLP (const char *file, const char *arg, ...); P: In the current environment variable path under       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 execvpe (const char *file, char *const argv[],                   char *const envp[]);       int Execve (const char *filename, char *const argv[],                  char *const envp[]);

Description

The number of arguments to execl,execlp,execle (all with "L") is Variable , and the parameter must end with a null pointer .

The second parameter of EXECV and EXECVP is an array of strings ("V" stands for "vector", the string array must be null-terminated), and the new program will pass the parameters given in the argv array to main at startup.

The last name of the "P" function searches the PATH environment variable to find the executable file for the new program. If the executable is not on the path defined by path, you must pass the absolute file name, including the subdirectory, as a parameter to these functions

/* Summary:l represents a mutable parameter list, and p represents the search for a file in the PATH environment variable. ENVP represents environment variable */

example EXECLP
#include <unistd.h> #include <iostream>using namespace Std;int main () {    EXECLP ("/bin/pwd", "/bin/pwd", NULL);    EXECLP ("ls", "ls", "-l", NULL);    cout << "after fork ..." << Endl;    return 0;}

example execve
int main () {    char *const args[] =    {        (char *) "/bin/date",        (char *) "+%f",        NULL    };    Execve ("/bin/date", args,null);    cout << "after fork ..." << Endl;    return 0;}

example execle
Main.cpp#include <unistd.h> #include <iostream>using namespace Std;int main () {    cout << "in main , PID = "<< getpid () << Endl;    Char *const environ[] =    {        "aa=11",        "bb=22",        "cc=33",        NULL    };    Execle ("./hello", "./hello", null,environ);//When environ is filled with NULL, nothing is passed    cout << "after fork ..." << Endl ;    return 0;} Hello.cpp#include <unistd.h> #include <iostream>using namespace Std;extern char **environ;int main () {    cout << "in Hello, pid =" << getpid () << Endl;    cout << "environ:" << Endl;    for (int i = 0; Environ[i]! = NULL; ++i)    {        cout << "\ T" << Environ[i] << Endl;    }}

/*in Main, PID = 3572//pid remains constant in Hello, pid = 3572environ:aa=11bb=22cc=33*/


EXEC Replacement Process Impressions

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.