Execl learning under Linux

  
        

Linux under file
#include <unistd.h>

Function definition
int execl(const char *path, const char *arg, ...);

Function description
execl() where suffix "l" stands for list is also the meaning of the parameter list, the first parameter path character pointer points to the file path to be executed, the next parameter represents the list of parameters passed when the file is executed: argv[0 ], argv[1]... The last parameter must end with a null pointer NULL.

Function return value
Success does not return a value, failure returns -1, the reason for failure is stored in errno, and can be printed by perror():

root@wl-MS-7673:/home/Wl/desktop/c++# cat -n execl.cpp 1 /* Execute /bin/ls -al /ect/passwd */2 #include <unistd.h>/*** File: execl.c**/3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 //Execute ls in the /bin directory, the first parameter is the program name ls, and the second parameter is "-al", third The argument is "/etc/passwd" 8 9 if(execl("/bin/ls", "ls", "-al", "/etc/passwd", (char *) 0) < 0) 10 11 { 12 cout<<"execl error"<<endl; 13 } 14 else 15 { 16 cout<<"success"<<endl; 17 } 18 return 0; 19 }root@wl-MS-7673:/home/wl/desktop/c++# g++ execl.cpp -o execlroot@wl-MS-7673:/home/wl/desktop/c++# ./execl -rw-r-- R-- 1 root root 1801 November 28 09:46 /etc/passwdroot@wl-MS-7673:/home /wl/desktop/c++# You can clearly see that the ls in the /bin directory is executed, the first parameter is the program name ls, the second parameter is "-al", and the third parameter is "/etc /passwd", but no output success! ! Why is this? Features of the execl function: When a process calls an exec function, the process is completely replaced by the new program, and the new program is executed from its main function. Because calling exec does not create a new process, the process IDs before and after have not changed. Exec simply replaces the body, data, heap, and stack segments of the current process with another new program. Replace the body, data, heap, and stack segments of the current process with another new program. The body of the current process has been replaced, then the statement after execl, even if execl exits, will not be executed. Look at a piece of code: 
root@wl-MS-7673:/home/wl/desktop/c++# cat -n execl_test.cpp 1 #include <unistd.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main(int argc,char *argv[]) 6 { 7 if(argc<2) 8 { 9 perror("you haven,t input the filename,please try again! \ "); 10 exit(EXIT_FAILURE); 11 12 } 13 if(execl("./file_creat","file_creat",argv[1],NULL)<0) 14 perror("execl error! "); 15 return 0; 16 } 17 root@wl-MS-7673:/home/wl/desktop/c++# cat -n file_creat.cpp 1 #include <stdio.h> 2 3 #include <stdlib .h> 4 5 #include <sys/types.h> 6 #include <sys/stat.h> 7 #include <fcntl.h> 8 void create_file(char *filename) 9 { 10 if(creat( Filename,0666)<0) 11 { 12 printf("create file %s failure!\ ",filename); 13 exit(EXIT_FAILURE); 14 } 15 else 16 { 17 printf("create file %s success!\ ",filename); 18 } 19 } 20 21 int main(int argc,char *argv[]) 22 { 23 if(argc<2) 24 { 25 printf("you haven't input the filename,please try again!\ "); 26 exit(EXIT_FAILURE); 27 } 28 create_file(argv[1]); 29 exit(EXIT_SUCCESS); 30 } 31 32 root@wl-MS-7673:/home/wl/desktop/c++# g++ execl_test.cpp -o execl_testroot@wl-MS-7673:/home/wl/desktop/c++# g++ file_cfile_copy file_copy.cpp file_creat.cpp root @wl-MS-7673:/home/wl/desktop/c++# g++ file_creat.cpp -o file_creatroot@wl-MS-7673:/home/wl/desktop/c++# ./execl_test you haven,t input the filename, Please try again!: Successroot@wl-MS-7673:/home/wl/desktop/c++# ./execl_test filecreate file file success!root@wl-MS-7673:/home/wl/desktop/c++#
Copyright © Windows knowledge All Rights Reserved