Linux exec function Family

Source: Internet
Author: User

The fork () function creates a process that is almost identical to the original process (the parent process) through a system call (the child process is a copy of the parent process, which obtains a copy of the parent process's data space, heap, stack, and so on. Note that the subprocess holds a "copy" of the above storage space, which means that these storage spaces are not shared between parent and child processes. Linux copies the address space content of the parent process to the child process, so that the child process has a separate address space, which is what the two processes do exactly the same thing.
Using the EXEC function family in a sub-process after fork, you can load and run other programs (the child process replaces the original process, and the parent process does something different)
Fork creates a new process to create a new pid,exec to start a new program, replacing the original process, so the PID of the new exec-executed process does not change (as with the PID of the exec process)

exec function Family:

intEXECL (Const Char*path,Const Char*arg, ...);intEXECLP (Const Char*file,Const Char*arg, ...);intExecle (Const Char*path,Const Char*arg,...,Char*ConstEnvp[]);intExecvConst Char*path,Char*ConstArgv[]);intEXECVP (Const Char*file,Char*ConstArgv[]);intEXECVPE (Const Char*file,Char*ConstArgv[],Char*ConstEnvp[]);

function return value
The success is not returned, the failure returns 1, and the reason for the failure is stored in errno.

In the EXEC function family, the suffix L, V, p, e specifies that the function will have some operational capability

Example:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include < Errno.h>intMainintargcChar*argv[]) {//null-terminated pointer to an array of strings, suitable for the exec function parameter containing v    Char*arg[] = {"ls"," -a", NULL};/** * Create child process and call function Execl * execl you want to receive a comma-delimited list of parameters and a null pointer to the end flag */    if(fork () = =0) {//In Clildprintf"1------------execl------------\ n");if(Execl ("/bin/ls","ls"," -a", NULL) = =-1) {perror ("Execl Error"); Exit1); }    }/** * Create a child process and call a function execv a pointer in the *EXECV that you want to receive a null-terminated array of strings * /    if(fork () = =0) {// in childprintf"2------------execv------------\ n");if(Execv ("/bin/ls", ARG) <0) {perror ("EXECV Error"); Exit1); }    }/** * Create a child process and call EXECLP *EXECLP in *l want to receive a comma-delimited list of parameters, with a null pointer as the end flag *p is a null-terminated string array pointer, the function can find a PATH variable dos Order file * /    if(fork () = =0) {//In Clhildprintf"3------------execlp------------\ n");if(EXECLP ("ls","ls"," -a", NULL) <0) {perror ("EXECLP Error"); Exit1); }    }/** * Create a sub-mileage and call EXECVP *v to receive a pointer to a null-terminated array of strings *p is a null-terminated string array pointer, the function can find the subroutine file in the DOS PATH variable * /    if(fork () = =0) {printf ("4------------execvp------------\ n");if(EXECVP ("ls", ARG) <0) {perror ("EXECVP Error"); Exit1); }    }/** * Create a child process and call Execle *l want to receive a comma-delimited list of parameters, the list with a null pointer as the end flag *e function pass the specified parameter envp, allowing the environment to change the child process, no suffix e, the child process uses the current program's environment */     if(fork () = =0) {printf ("5------------execle------------\ n");if(Execle ("/bin/ls","ls"," -a", NULL, NULL) = =-1) {perror ("Execle Error"); Exit1); }    }/** * Creates a child process and calls Execve * V to receive a pointer to a null-terminated array of strings * E function passes the specified parameter envp, allowing the environment to change the child process, no suffix e, the child process uses the current program's environment * /    if(fork () = =0) {printf ("6------------execve-----------\ n");if(Execve ("/bin/ls", arg, NULL) = =0) {perror ("Execve Error"); Exit1); }    }returnexit_success;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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