The EXEC series function (EXECL,EXECLP,EXECLE,EXECV,EXECVP) uses

Source: Internet
Author: User

objective of this section: exec replaces the process image of the EXEC Association function Group (EXECL, EXECLP, Execle, EXECV, EXECVP) One, exec replaces the process image on the process creation Unix takes a unique approach, It separates the process creation from the loading of a new process image. 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, the Exec series functions can also replace the current process. For example, executing the PS command on the shell command line is actually a shell process calling fork to copy a new subprocess, replacing the newly generated child process with the PS process with the EXEC system call. Second, the Exec series functions (Execl, EXECLP, Execle, EXECV, EXECVP) contain header files<unistd.h>function: The EXEC function can be used to replace the current process with a new process, and the new process has the same PID as the original process. The exec name is a complete series of multiple correlation functions, and the header file<unistd.h>extern Char**environ; prototype: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[]); parameter: The path parameter indicates that the name of the program you want to start includes the path name, the arg parameter indicates the parameter that the initiator takes, the first parameter is the command name to execute, not the path, and Arg must return the value with NULL: Successfully returned 0, failed return-1Note: The above EXEC series functions are implemented through EXECVE system calls: #include<unistd.h>intExecve (Const Char*filename,Char*ConstArgv[],Char*Constenvp[]);D Escription:execve () executes the program pointed to by filename. FileName must is either a binary executable, or a script starting with a line of the form above the EXEC series function difference:1, the EXEC function with L: execl,execlp,execle, which indicates that the following arguments are given in the form of variable arguments and are terminated with a null pointer. Example:
#include <stdio.h>#include<stdlib.h>#include<unistd.h>intMainvoid) {printf ("entering main process---\ n"); Execl ("/bin/ls","ls","- L", NULL); printf ("exiting main process----\ n"); return 0;}

Replace the current process main with EXECL, and all the last print statements will not be output
2, the EXEC function with P: EXECLP,EXECVP, which indicates that the first parameter path does not have to enter the full path, only give the command name, it will find the command example in the environment variable path: When the full path is not given with P: #include<stdio.h>#include<stdlib.h>#include<unistd.h>intMainvoid) {printf ("entering main process---\ n"); if(Execl ("ls","ls","- L", NULL) <0) perror ("Excl Error"); return 0;}

The result is not found, all replacements are unsuccessful, the main process continues execution

Now with P:

if (EXECLP ("ls", "ls", "-l", NULL) <0)

Replace succeeded
3, without the EXEC function of L: EXECV,EXECVP represents the arguments required by the command with char *arg[] form given and arg the last element must be a null example: #include<stdio.h>#include<stdlib.h>#include<unistd.h>intMainvoid) {printf ("entering main process---\ n"); intret; Char*argv[] = {"ls","- L", NULL}; RET= EXECVP ("ls", argv); if(ret = =-1) perror ("execl Error"); printf ("exiting main process----\ n"); return 0;}
Replace succeeded

4 , the EXEC function with E: Execle indicates that the environment variable is passed to the process that needs to be replaced from the function prototype described above, we find: extern Char * *environ; here environ is an array of pointers, each of which points to the char "xxx=xxx" Environ the data that holds the environment information can be viewed by the env command:

It is passed by the shell process to the current process, which is then passed to the replacement new process by the current process
Example: Execle.c#include<stdio.h>#include<stdlib.h>#include<unistd.h>intMainintargcChar*argv[]) {    //char * Const envp[] = {"aa=11", "bb=22", NULL};printf"Entering main ... \ n"); intret; RET=EXECL ("./hello","Hello", NULL); //execle ("./hello", "Hello", NULL, ENVP);    if(ret = =-1) perror ("execl Error"); printf ("Exiting main ... \ n"); return 0;} Hello.c#include<unistd.h>#include<stdio.h>extern Char**environ;intMainvoid) {printf ("Hello pid=%d\n", Getpid ()); inti;  for(i=0; Environ[i]!=null; ++i) {printf ("%s\n", Environ[i]); }    return 0;}

The original process does pass the environment variable information to the new process.

So now we can use the Execle function to give the environment variable information that needs to be passed:

Example Program: Execle.c#include<stdio.h>#include<stdlib.h>#include<unistd.h>intMainintargcChar*argv[]) {    Char*ConstEnvp[] = {"aa=11","bb=22", NULL}; printf ("Entering main ... \ n"); intret; //ret =execl ("./hello", "Hello", NULL);RET =execle ("./hello","Hello", NULL, ENVP); if(ret = =-1) perror ("execl Error"); printf ("Exiting main ... \ n"); return 0;} Hello.c#include<unistd.h>#include<stdio.h>extern Char**environ;intMainvoid) {printf ("Hello pid=%d\n", Getpid ()); inti;  for(i=0; Environ[i]!=null; ++i) {printf ("%s\n", Environ[i]); }    return 0;}

Actually passed the given environment variable.

http://bbs.csdn.net/topics/350057686

What to do to normal execution (mainly the EXECL function to run PHP files. ) and enter Exit to exit? EXECL will load the program you call, overwriting the original code snippet, which is equivalent to the code snippet of your original program being replaced with EXECL execution. So the back of the execl will not output. The correct one is to fork a child process and call execl in the child process. Would it overwrite the original code if I put execl in a thread created with Pthread_create? I tested it and I quit. Must I fork? EXECL is the code snippet that overwrites the process, so if your original program still needs to exit normally, fork a sub-process. In addition, if the program that requires the EXECL call executes, it needs to wait in the parent process and can call waitpid. Specific you man to look at. If the fork is used. Does execl not stop a child process after executing it? After fork, the child process copies almost all of the attributes of the parent process, including the PC, that is, in the subprocess, or from where you fork. Then you execl, overwriting the code snippet of the child process, regardless of the parent process. Pthread_create creates a child thread that share the same code snippet with the parent thread! Only registers and stacks are different! Execl not create a new process, Linux only fork can create a new process EXECL success is not returned, will execute the new program. EXECL Failure will return
View Code

Http://bbs.chinaunix.net/thread-4168834-1-1.html

 
I feel execl the first parameter and the second parameter overlap, help me ... Title, I learned a little bit about the usage of the EXECL function: #include<unistd.h>intMain () {execl ("ls","ls","-al"); return 0;} OK, I don't print anything, so I changed it a bit: #include<unistd.h>intMain () {execl ("/bin/ls","ls","-al",0); return 0;} It's strange, execl. The first argument must be path, and if I change the call statement to EXECL ("/bin","ls","-al",0); not found. Does that mean that the path parameter must contain the name of the executable program itself? But in this case, the second parameter of EXECL is the filename, and the second parameter is not redundant?very confused, asking for explanations!path contains the name of file, and what about the second file parameter??feeling redundant? The 2nd parameter will be passed to the process, i.e. argv[0], the process can be due by looking at how big the face is--oh, it's a improvise! Sure is a master! I've verified your statement, and the second parameter is argv[.0], can be anything, it doesn't have to be an executable file name. As shown in the following:
View Code





Third, the role of the fd_cloexec identifier in the Fcntl () function in the EXEC series function

#include <unistd.h>
#include <fcntl.h>

int fcntl (int fd, int cmd, .../* arg */);

File Descriptor Flags
The following commands manipulate the flags associated with a file
Descriptor. Currently, only one such flag is defined: fd_cloexec, the
Close-on-exec flag. If the fd_cloexec bit is 0, the file descriptor
Would remain open across an EXECVE (2), otherwise it'll be closed.

If the fd_cloexec identity bit is 0, then the FD is still open after the EXECVE call, otherwise the closed

F_getfd (void)
Read the file descriptor flags; ARG is ignored.

F_SETFD (Long)
Set The file descriptor flags to the value specified by ARG.

such as: Fcntl (FD, F_SETFD, fd_cloexec);


1 off (standard output off) LS-L Unable to display the results in standard output
Http://www.cnblogs.com/mickole/p/3187409.html



The EXEC series function (EXECL,EXECLP,EXECLE,EXECV,EXECVP) uses

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.