【发布时间】:2014-03-14 10:50:21
【问题描述】:
我想使用fork 和exec 系统调用在Linux 中执行一个C 程序。
我写了一个程序 msg.c 并且运行良好。然后我写了一个程序msg1.c。
当我执行./a.out msg.c 时,它只是将msg.c 打印为输出,但不执行我的程序。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */
int main(int argc,char** argv)
{
/*Spawn a child to run the program.*/
pid_t pid=fork();
if (pid==0)
{ /* child process */
// static char *argv[]={"echo","Foo is my name.",NULL};
execv("/bin/echo",argv);
exit(127); /* only if execv fails */
}
else
{ /* pid!=0; parent process */
waitpid(pid,0,0); /* wait for child to exit */
}
return 0;
}
【问题讨论】:
-
你希望 /bin/echo 做什么?
-
我不知道。写什么?我试图理解它。
标签: c linux fork system-calls