Execl learning in Linux

Source: Internet
Author: User

Linux header files#Include <unistd. h> Function Definition int execl (const char * path, const char * arg ,...); the function description is execl (). The suffix "l" indicates list, that is, the parameter list. The first parameter path character Pointer Points to the file path to be executed, the following parameters represent the list of parameters passed when the file is executed: argv [0], argv [1]... the last parameter must end with a NULL pointer.

If the return value of the function is successful, no value is returned. If the failure is returned,-1 is returned. The cause of the failure is stored in errno. You can print it through perror ().
Instance 1:

Root @ wl-MS-7673:/home/wl/desktop/c ++ # cat-n execl. cpp 1/* Run/bin/ls-al/ect/passwd */2 # include <unistd. h>/*** File: execl. c **/3 # include <iostream> 4 using namespace std; 5int main () 6 {7 // ls in the execution/bin directory. The first parameter is the program name ls, the second parameter is "-al", and the third parameter 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 -- 1 root 1801 November 28 09:46/etc/passwdroot @ wl-MS-7673:/home/wl/desktop/c ++ #
You can clearly see that the first parameter of the ls under the/bin directory is the program name ls, and the second parameter is "-al ", the third parameter is "/etc/passwd", but success is not output !!
Why?
Features of the execl function:
When a process calls an exec function, the process is completely replaced by a new program, and the new program starts to run from its main function. Because calling exec does not create a new process, the process ID does not change. Exec only replaces the body, Data, heap, and stack segments of the current process with another new program.
Replace the text, Data, heap, and stack segments of the current process with another new program.
If the body of the current process is replaced, the statement after execl will not be executed even if execl exits.
Let's look at another 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 5int main (int argc, char * argv []) 6 {7if (argc <2) 8 {9 perror ("you haven, t input the filename, please try again! \ N "); 10 exit (EXIT_FAILURE); 11 12} 13if (execl (". /file_creat "," file_creat ", argv [1], NULL) <0) 14 perror (" execl error! "); 15 return 0; 16} 17root @ 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! \ N ", filename); 13 exit (EXIT_FAILURE); 14} 15 else 16 {17 printf (" create file % s success! \ N ", filename); 18} 19} 20 21int main (int argc, char * argv []) 22 {23 if (argc <2) 24 {25 printf ("you haven't input the filename, please try again! \ N "); 26 exit (EXIT_FAILURE); 27} 28create_file (argv [1]); 29 exit (EXIT_SUCCESS); 30} 31 32root @ 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 success! Root @ wl-MS-7673:/home/wl/desktop/c ++ #




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.