技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁(yè) > 教程 > 服務(wù)器類 >

Linux下execl學(xué)習(xí)

來(lái)源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2018-08-26 18:02┆點(diǎn)擊:

  Linux下頭文件

  #include

  函數(shù)定義

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

  函數(shù)說(shuō)明

  execl()其中后綴"l"代表list也就是參數(shù)列表的意思,第一參數(shù)path字符指針?biāo)赶蛞獔?zhí)行的文件路徑, 接下來(lái)的參數(shù)代表執(zhí)行該文件時(shí)傳遞的參數(shù)列表:argv[0],argv[1]... 最后一個(gè)參數(shù)須用空指針NULL作結(jié)束。

  函數(shù)返回值

  成功則不返回值, 失敗返回-1, 失敗原因存于errno中,可通過(guò)perror()打印

  實(shí)例1:

  root@wl-MS-7673:/home/wl/桌面/c++# cat -n execl.cpp

  1 /* 執(zhí)行 /bin/ls -al /ect/passwd */

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

  3 #include

  4 using namespace std;

  5 int main()

  6 {

  7 // 執(zhí)行/bin目錄下的ls, 第一參數(shù)為程序名ls, 第二個(gè)參數(shù)為"-al", 第三個(gè)參數(shù)為"/etc/passwd"

  8

  9 if(execl("/bin/ls", "ls", "-al", "/etc/passwd", (char *) 0) < 0)

  10

  11 {

  12 cout<<"execl error"<

  13 }

  14 else

  15 {

  16 cout<<"success"<

  17 }

  18 return 0;

  19 }

  root@wl-MS-7673:/home/wl/桌面/c++# g++ execl.cpp -o execl

  root@wl-MS-7673:/home/wl/桌面/c++# ./execl

  -rw-r--r-- 1 root root 1801 11月 28 09:46 /etc/passwd

  root@wl-MS-7673:/home/wl/桌面/c++#

  大家可以清楚的看到, 執(zhí)行/bin目錄下的ls, 第一參數(shù)為程序名ls, 第二個(gè)參數(shù)為"-al", 第三個(gè)參數(shù)為"/etc/passwd",但是沒(méi)有輸出success!!

  這是為什么呢?

  execl函數(shù)特點(diǎn):

  當(dāng)進(jìn)程調(diào)用一種exec函數(shù)時(shí),該進(jìn)程完全由新程序代換,而新程序則從其main函數(shù)開(kāi)始執(zhí)行。因?yàn)檎{(diào)用exec并不創(chuàng)建新進(jìn)程,所以前后的進(jìn)程ID并未改變。exec只是用另一個(gè)新程序替換了當(dāng)前進(jìn)程的正文、數(shù)據(jù)、堆和棧段。

  用另一個(gè)新程序替換了當(dāng)前進(jìn)程的正文、數(shù)據(jù)、堆和棧段。

  當(dāng)前進(jìn)程的正文都被替換了,那么execl后的語(yǔ)句,即便execl退出了,都不會(huì)被執(zhí)行。

  再看一段代碼:root@wl-MS-7673:/home/wl/桌面/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");

  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/桌面/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 {

  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

  21 int 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 }

  28 create_file(argv[1]);

  29 exit(EXIT_SUCCESS);

  30 }

  31

  32

  root@wl-MS-7673:/home/wl/桌面/c++# g++ execl_test.cpp -o execl_test

  root@wl-MS-7673:/home/wl/桌面/c++# g++ file_c

  file_copy file_copy.cpp file_creat.cpp

  root@wl-MS-7673:/home/wl/桌面/c++# g++ file_creat.cpp -o file_creat

  root@wl-MS-7673:/home/wl/桌面/c++# ./execl_test

  you haven,t input the filename,please try again!

  : Success

  root@wl-MS-7673:/home/wl/桌面/c++# ./execl_test file

  create file file success!

  root@wl-MS-7673:/home/wl/桌面/c++#