Linux under Execl Learning

Source: Internet
Author: User
Tags function definition printf

Linux Bottom file

#include

function definition

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

Function description

Execl () where the suffix "l" represents the list is the meaning of the argument lists, the first parameter path character pointer points to the file path to execute, and the next argument represents the parameter list passed when the file was executed: argv[0],argv[1] ... The last parameter must be NULL to end with a null pointer.

function return value

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

Example 1:

root@wl-ms-7673:/home/wl/Desktop/c++# cat-n execl.cpp

1/* Implementation/BIN/LS-AL/ECT/PASSWD * *

2 #include /*** file:execl.c**/

3 #include

4 using namespace Std;

5 int Main ()

6 {

7//execute/bin directory of LS, the first parameter is 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 {

cout<< "Execl Error" <

13}

Or else

15 {

cout<< "Success" <

17}

return 0;

19}

root@wl-ms-7673:/home/wl/Desktop/c++# g++ execl.cpp-o execl

root@wl-ms-7673:/home/wl/Desktop/c++#./execl

-rw-r--r--1 root root 1801 November 09:46/etc/passwd

root@wl-ms-7673:/home/wl/Desktop/c++#

You can clearly see, the implementation of the/bin directory of LS, the first parameter is the program name LS, the second parameter is "-al", the third parameter is "/etc/passwd", but there is no output success!!

What is this for?

Execl function Features:

When a process invokes an EXEC function, the process is completely substituted by the new program, and the new program executes from its main function. Because calling exec does not create a new process, the process ID does not change before and after. exec simply replaces the body, data, heap, and stack segments of the current process with another new program.

Replaced the body, data, heap, and stack segments of the current process with another new program.

The body of the current process is replaced, then the EXECL statement, even if the execl exits, will not be executed.

Look at the code again: root@wl-ms-7673:/home/wl/Desktop/c++# cat-n execl_test.cpp

1 #include

2 #include

3 #include

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!n");

Ten exit (Exit_failure);

11

12}

Execl ("./file_creat", "File_creat", Argv[1],null) <0)

Perror ("Execl error!");

return 0;

16}

17

root@wl-ms-7673:/home/wl/Desktop/c++# cat-n file_creat.cpp

1 #include

2

3 #include

4

5 #include

6 #include

7 #include

8 void Create_file (char *filename)

9 {

Ten if (creat (filename,0666) <0)

11 {

printf ("Create file%s failure!n", filename);

The exit (exit_failure);

14}

Else

16 {

printf ("Create file%s success!n", filename);

18}

19}

20

int main (int argc,char *argv[])

22 {

if (argc<2)

24 {

printf ("You haven ' t input the filename,please try Again!n");

To exit (Exit_failure);

27}

Create_file (argv[1]);

Exit (exit_success);

30}

31

32

root@wl-ms-7673:/home/wl/Desktop/c++# g++ execl_test.cpp-o execl_test

root@wl-ms-7673:/home/wl/Desktop/c++# g++ file_c

File_copy File_copy.cpp File_creat.cpp

root@wl-ms-7673:/home/wl/Desktop/c++# g++ file_creat.cpp-o file_creat

root@wl-ms-7673:/home/wl/Desktop/c++#./execl_test

You haven,t input the filename,please try Again!

: Success

root@wl-ms-7673:/home/wl/Desktop/c++#./execl_test file

Create File 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.