Linux system Programming-process substitution: Exec function family

Source: Internet
Author: User

Under the Windows platform, we can run the executable program by double-clicking, let this executable program become a process, and on the Linux platform, we can pass ./ Run, let an executable program become a process.

However, if we are running a program (process), how do we start an external program inside the process, and the kernel reads the external program into memory so that it executes as a process? Here we implement through the EXEC function family.

The EXEC function family, as the name implies, is a cluster of functions, in Linux, there is no exec () function, exec refers to a set of functions, a total of 6:

  1. int execl (const Char *path, const char *arg, ...);
  2. int EXECLP (const Char *file, const char *arg, ...);
  3. int execle (const Char *path, const char *arg, ..., char * const envp[]);
  4. int Execv (const Char *path, char *const argv[]);
  5. int EXECVP (const Char *file, char *const argv[]);
  6. int Execve (const Char *path, char *const argv[], char *const envp[]), where only Execve () is the true meaning of system tuning and others are packaged library functions on this basis.

    The EXEC function family provides six ways to start another program in the process . The function of the EXEC function family is to find the executable file based on the specified file name or directory name, and use it to replace the contents of the calling process, in other words , execute an executable file inside the calling process.

    When a process calls an EXEC function, the process is completely replaced by the new program, and the new program executes from its main function. Because calling exec does not create a new process, the process ID of the front and back (and of course the parent process number, the process group number, the current working directory ...). ) has not changed. exec simply replaces the body, data, heap, and stack (process substitution) of the current process with another new program.

    The 6 functions of the EXEC function family seem to be complex, but in fact they are very similar in function and usage, with only a small difference.

    L (list): The parameter address list, ending with a null pointer.

    V (vector): The address of an array of pointers that hold the address of each parameter.

    P (Path): Searches the executable file by the directory specified by the PATH environment variable.

    E (Environment): The address of an array of pointers containing the environment variable string addresses.

    The EXEC function family loads and runs the executable program Path/file and passes the parameter arg0 (arg1, Arg2, argv[], envp[]) to the program.

    The EXEC function family differs from the normal function in that the function of the EXEC function family does not return after successful execution, and thecode below the EXEC function family does not execute . Only if the call fails, they return 1, and then execute from the original program's point of invocation after the failure. execl () sample code:

    1. #include <unistd.h>
    2. int main (int argc, char *argv[])
    3. {
    4. printf ("before exec\n\n");
    5. / */bin/ls: External program, here is the LS executable of the/bin directory, must take the path (relative or absolute)
    6. LS: There is no point, if you need to send this external program, you must write the string, as the string content arbitrary
    7. -a,-l,-h: parameter to the external program LS Pass
    8. NULL: This must be written on behalf of the external program LS to end the argument
    9. */
    10. Execl ("/bin/ls", "ls", "-a", "-L", "-H", NULL);
    11. //If EXECL () execution succeeds, the following cannot be performed because the current process has been executed by LS replaced by the
    12. Perror ("execl");
    13. printf ("after exec\n\n");
    14. return 0;
    15. }

      The results of the operation are as follows:

      EXECV () Sample code:

      The usage of EXECV () and execl () is basically the same, instead of using a pointer array to pass the list to the parameter.

      1. int main (int argc, char *argv[])
      2. {
      3. The use of//EXECV () and execl () is basically the same, but instead of using a pointer array to pass a list of arguments
      4. //Execl ("/bin/ls", "ls", "-a", "-L", "-H", NULL);
      5. / * pointer array
      6. LS: There is no point, if you need to send this external program, you must write the string, as the string content arbitrary
      7. -a,-l,-h: parameter to the external program LS Pass
      8. NULL: This must be written on behalf of the external program LS to end the argument
      9. */
      10. char *arg[]={"ls", "-a", "-L", "-H", NULL};
      11. ///BIN/LS: External program, here is the LS executable of the/bin directory, must take the path (relative or absolute)
      12. //ARG: The pointer array address defined above
      13. Execv ("/bin/ls", Arg);
      14. Perror ("Execv");
      15. return 0;
      16. }EXECLP () or EXECL () sample code:

        The difference between EXECLP () and Execl () is that the executable program specified by EXECLP () may not have a pathname, and if it does not have a pathname, it will look for the executable in the directory specified by the environment variable path, and the executable program specified by EXECL () must take the path name.

        1. #include <stdio.h>
        2. #include <unistd.h>
        3. int main (int argc, char *argv[])
        4. {
        5. //The first parameter "ls", without path name, in the environment variable path search for this executable program
        6. //Other parameter usage same as EXECL ()
        7. EXECLP ("ls", "ls", "-a", "-L", "-H", NULL);
        8. /* 
        9. char *arg[]={"LS", "-a", "-L", "-H", NULL};
        10. EXECVP ("LS", ARG);
        11.     */  
        12. Perror ("EXECLP");
        13. return 0;
        14. }

          Execle () or EXECVE () sample code:

          Execle () and Execve () change the environment variables of the exec-initiated program (which only alters the environment variables of the process, does not affect the environment variables of the system ), and the other four function-initiated programs use the default system environment variables.

          Execle () Sample code:

          1. #include <stdio.h>
          2. #include <unistd.h>
          3. #include <stdlib.h>//getenv ()
          4. int main (int argc, char *argv[])
          5. { //getenv () Gets the value of the specified environment variable
          6. printf ("before exec:user=%s, home=%s\n", getenv ("USER"), getenv ("HOME"));
          7. //Pointer data
          8. Char *env[]={"User=mike", "Home=/tmp", NULL};
          9. /*/mike: External program, current path of the Mike program, compiled by GCC mike.c-o Mike
          10. Mike: There's no point here.
          11. NULL: End of argument to Mike Program
          12. ENV: Change the environment variables of the Mike program, correctly, let the Mike program keep only env environment variables
          13. */
          14. Execle ("./mike", "Mike", NULL, env);
          15. /* 
          16. Char *arg[]={"Mike", NULL};
          17. Execve ("./mike", ARG, env);
          18.     */  
          19. Perror ("execle");
          20. return 0;
          21. }
          22. External program, MIKE.C sample code:
            1. #include <stdio.h>
            2. #include <stdlib.h>
            3. #include <unistd.h>
            4. int main (int argc, char *argv[])
            5. {
            6. printf ("\nin The mike Fun, after exec: \ n");
            7. printf ("user=%s\n", getenv ("USER"));
            8. printf ("home=%s\n", getenv ("HOME"));
            9. return 0;
            10. }
            11. The results of the operation are as follows:

Linux system Programming-process substitution: Exec function family

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.