【发布时间】:2015-09-24 21:49:24
【问题描述】:
我有一个问题,我使用以下程序(fork() 函数)创建子进程以使用 execl() 重载另一个程序。如果我想在 execl() 函数或其他类型的 exec() 函数中使用 echo 命令,我该怎么办?我使用以下程序,但失败了!终端给了我一个警告:echo: cannot access hello world!: No such file or directory 这是父母!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{ pid_t childpid;
childpid=fork();
if(childpid==-1){
perror("failed to fork");
return 1;
}
if(childpid==0)
{
execl("/bin/ls","echo","hello world!",NULL);
perror("child failed to exec");
return 1;
}
if(childpid!=wait(NULL)){
perror("parent failed to wait due to signal or error");
return 1;
}
sleep(5);
printf("this is parent!\n");
return 0;
}
【问题讨论】:
-
基本上我只是想在exec()函数中使用echo命令,并使用exec()来重载main函数,但是我不知道如何使用execl()函数来执行echo命令.我认为路径可能有问题?
-
stackoverflow.com/questions/8827939/…觉得这也能帮我解决问题!