Linux下execl学习
来源:行业资讯 /
时间: 2024-12-13
Linux下头文件
#include <unistd.h><br/>
函数定义<br/>
int execl(const char *path, const char *arg, ...);<br/>
函数说明<br/>
execl()其中后缀"l"代表list也就是参数列表的意思,第一参数path字符指针所指向要执行的文件路径, 接下来的参数代表执行该文件时传递的参数列表:argv[0],argv[1]... 最后一个参数须用空指针NULL作结束。<br/>
<br/>
函数返回值<br/>
成功则不返回值, 失败返回-1, 失败原因存于errno中,可通过perror()打印 <br/>
实例1:<br/>
root@wl-MS-7673:/home/桌面/c++# cat -n execl.cpp <br/>
1 /* 执行 /bin/ls -al /ect/passwd */<br/>
2 #include <unistd.h>/*** File: execl.c**/<br/>
3 #include <iostream><br/>
4 using namespace std;<br/>
5 int main()<br/>
6 {<br/>
7 // 执行/bin目录下的ls, 第一参数为程序名ls, 第二个参数为"-al", 第三个参数为"/etc/passwd"<br/>
8<br/>
9 if(execl("/bin/ls", "ls", "-al", "/etc/passwd", (char *) 0) < 0)<br/>
10<br/>
11 {<br/>
12 cout<<"execl error"<<endl;<br/>
13 }<br/>
14 else<br/>
15 {<br/>
16 cout<<"success"<<endl;<br/>
17 }<br/>
18 return 0;<br/>
19 }<br/>
root@wl-MS-7673:/home/桌面/c++# g++ execl.cpp -o execl<br/>
root@wl-MS-7673:/home/桌面/c++# ./execl <br/>
-rw-r--r-- 1 root root 1801 11月 28 09:46 /etc/passwd<br/>
root@wl-MS-7673:/home/桌面/c++# <br/>
大家可以清楚的看到, 执行/bin目录下的ls, 第一参数为程序名ls, 第二个参数为"-al", 第三个参数为"/etc/passwd",但是没有输出success!!<br/>
这是为什么呢?<br/>
execl函数特点:<br/>
当进程调用一种exec函数时,该进程完全由新程序代换,而新程序则从其main函数开始执行。因为调用exec并不创建新进程,所以前后的进程ID并未改变。exec只是用另一个新程序替换了当前进程的正文、数据、堆和栈段。<br/>
用另一个新程序替换了当前进程的正文、数据、堆和栈段。<br/>
当前进程的正文都被替换了,那么execl后的语句,即便execl退出了,都不会被执行。<br/>
再看一段代码:<br/>
root@wl-MS-7673:/home/桌面/c++# cat -n execl_test.cpp <br/>
1 #include <unistd.h><br/>
2 #include <stdio.h><br/>
3 #include <stdlib.h><br/>
4<br/>
5 int main(int argc,char *argv[])<br/>
6 {<br/>
7 if(argc<2)<br/>
8 {<br/>
9 perror("you haven,t input the filename,please try again!
“);
10 exit(EXIT_FAILURE);<br/>
11<br/>
12 }<br/>
13 if(execl("./file_creat","file_creat",argv[1],NULL)<0)<br/>
14 perror("execl error!");<br/>
15 return 0;<br/>
16 }<br/>
17<br/>
root@wl-MS-7673:/home/桌面/c++# cat -n file_creat.cpp <br/>
1 #include <stdio.h> <br/>
2<br/>
3 #include <stdlib.h> <br/>
4<br/>
5 #include <syspes.h> <br/>
6 #include <sys/stat.h> <br/>
7 #include <fcntl.h> <br/>
8 void create_file(char *filename)<br/>
9 { <br/>
10 if(creat(filename,0666)<0)<br/>
11 { <br/>
12 printf("create file %s failure!
”,filename);
13 exit(EXIT_FAILURE); <br/>
14 }<br/>
15 else<br/>
16 { <br/>
17 printf("create file %s success!
“,filename);
18 } <br/>
19 } <br/>
20<br/>
21 int main(int argc,char *argv[])<br/>
22 { <br/>
23 if(argc<2)<br/>
24 { <br/>
25 printf("you haven't input the filename,please try again!
”);
26 exit(EXIT_FAILURE); <br/>
27 } <br/>
28 create_file(argv[1]); <br/>
29 exit(EXIT_SUCCESS); <br/>
30 }<br/>
31<br/>
32<br/>
root@wl-MS-7673:/home/桌面/c++# g++ execl_test.cpp -o execl_test<br/>
root@wl-MS-7673:/home/桌面/c++# g++ file_c<br/>
file_copy file_copy.cpp file_creat.cpp <br/>
root@wl-MS-7673:/home/桌面/c++# g++ file_creat.cpp -o file_creat<br/>
root@wl-MS-7673:/home/桌面/c++# ./execl_test <br/>
you haven,t input the filename,please try again!<br/>
: Success<br/>
root@wl-MS-7673:/home/桌面/c++# ./execl_test file<br/>
create file file success!