您的位置:首页 > 其它

8_16.c-->exec

2015-10-27 10:45 399 查看
   只是取到了一个参数呀。

shell下执行which and echo , They will display the results of two parameters.

file /tmp 8_16.c

/tmp:           directory

8_16.c:         c program text

    1  #include "apue.h"

     2  #include <sys/wait.h>

     3

     4  char *env_init[] = {"USER=unknown","PATH = /tmp",NULL };

     5  int main()

     6  {

     7          pid_t pid;

     8          if ((pid = fork()) < 0){

     9                  err_sys("fork error");

    10          }

    11          else if (pid == 0){

    12                  if (execle("/home/tingbinz/bin/which","echo","ls",(char *)0,env_init) < 0)

    13                          err_sys("execle errpr");

    14          }

    15

    16          if (waitpid(pid,NULL,0) < 0)

    17                  err_sys("waitpid error");

    18

    19          if ((pid = fork()) < 0)

    20                  err_sys("fork error");

    21          else if (pid == 0){

    22                  if(execlp("file","/tmp","8_16.c",(char *)0) < 0)

    23                          err_sys("fork error");

    24

    25          }

    26          return 0;

    27
    28  }

2.running result:

<bldc:/home/tingbinz/apue.3e/SBSCODE/8>R*_*G:./exec

/usr/bin/ls

<bldc:/home/tingbinz/apue.3e/SBSCODE/8>R*_*G:8_16.c:            c program text
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息