We run the LS program in the bin directory. When arg0 is used, the path of the LS program is-L, so that arg1 can list the current directory as a list, the program output on my computer is as follows:
The ls program is successfully run. But have you noticed? There is no output of the "app end" string. The reason is simple. Instead of starting a new process, we directly overwrite the process where the main function is located with the LS program.
Next, use fork to avoid affecting the original process.
int main ()
{< br> printf ( " app start... \ n " );
If (Fork () = 0 )
{< br> execl ( " /bin/ls " , " /bin/ls " , " -L " , null );
}< br>
printf ( " app end \ n " );
return 0 ;
}
We used fork to create a new process. After it is successfully created (the return value is 0), we used execl to load the LS program and run it.
The program output is as follows:
APP start...
APP end
Zhouyh @ Ubuntu :~ /Documents $ total 12
-RW-r -- 1 Zhouyh 229 2010 - 09 - 06 15 : 59 Temp. c
-Rwxr-XR-x 1 Zhouyh 7211 2010 - 09 - 06 16 : 00 Temp.exe
All the output of the program is OK, but one thing that may be different from what we think is that the "app end" string has been output very early rather than at the end. In fact, there is nothing wrong with it, "app end" is output when the program (temp.exe) of the mainprogram is about to end, while the LS listing the file directory is completely in another process, two asynchronous processes, no one of them should start and end.
If we want to output "app end" after all the work is completed, that is, after LS is executed and played, we can use the wait and waitpid functions. Here we will briefly describe wait, for details, refer to "Linux Process thread learning notes: Process Control.
Pid_t wait (int * status); // contained in <sys/Wait. H>
The wait function indicates that the current process is sleep until a sub-process of the process ends or a specific signal is sent to wake up. If the child process ends normally, the child process ID (PID) is used as the return value. If an error occurs,-1 is returned, and the status parameter indicates the end status of the child process.
For the example above, refer to the following code:
# Include < Stdio. h > // For printf (const char)
# Include < Unistd. h > // For fork ()
# Include < Sys / Wait. H > // For wait (int * Status)
Int Main ()
{
Printf ( " APP start... \ n " );
If (Fork () = 0 )
{
Execl ( " /Bin/ls " , " /Bin/ls " , " -L " , Null );
}
Int Status;
Wait ( & Status );
Printf ( " APP end \ n " );
Return 0 ;
}
The program output is as follows:
APP start...
Total 12
-RW-r -- 1 Zhouyh 337 2010 - 09 - 06 16 : 22 Temp. c
-Rwxr-XR-x 1 Zhouyh 7247 2010 - 09 - 06 16 : 22 Temp.exe
APP end
Now let's look at the other functions except execl:
Int execlp (const char *File, Const char *Arg0,.../*, (Char *) 0 */);
Execlp is similar to execl, but the difference is that the former will go to the system environment variable to find the location of the program referred to by file, so if you can find the executable file through the environment variable, the file may not be an absolute path, such as execlp ("ls", "ls", "-l", null );
Int execle (const char *Path, Const char *Arg0,.../*, (Char *) 0, char * constEnvp[] */);
Unlike execlp, the last parameter is passed in as your custom environment variable parameter, rather than searching for system environment variables.
Char* Env [] = {"Home =/usr/home","LOGNAME = home",(Char*)0};
Execle("/Bin/ls","Ls","-L",Null, ENV );
Int execv (const char *Path, Char * const Argv[]);
Int execvp (const char *File, Char * constArgv[]);
Int execve (const char *Path, Char * const Argv[], Char * const Envp[]);
These three functions are similar to the preceding three functions. The function name is changed from the suffix L to v. The meaning of the function is that the parameter is not transmitted using the parameter list, but is transmitted using a parameter array argv []. of course, the last element of the array must also be char * 0
Functions with similar names are easy to confuse, so let's differentiate them from extensions such as l, V, p, and E:
L: a list of parameters separated by commas (,), ending with char * 0.
V: The parameter is a string array. The last element of the array is char * 0.
P: You can use the system environment variable to find the file location.
E: The caller displays the input environment variables.
Http://www.cnblogs.com/zhouyinhui/archive/2010/09/07/1819338.html