你如何编写一个 C 程序来执行另一个程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5460421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you write a C program to execute another program?
提问by wa-ha
In linux, I would like to write a C program that launches another program. When the program runs, the shell will be waiting for you to input a command that you have defined in you program. This command will launch the second program.
在 linux 中,我想编写一个 C 程序来启动另一个程序。当程序运行时,shell 将等待您输入您在程序中定义的命令。此命令将启动第二个程序。
For example, assume there is a simple C program called "hello" in the same directory as the invoking program. The "hello" program prints the output "hello, world". The first program would be run and the user would input the command "hello." The "hello" program would be executed and "hello, world." would be output to the shell.
例如,假设在与调用程序相同的目录中有一个名为“hello”的简单 C 程序。“hello”程序打印输出“hello, world”。将运行第一个程序,用户将输入命令“hello”。“你好”程序将被执行,“你好,世界”。将输出到shell。
I have done some search, and people suggested the "fork()" and "exec()" functions. Others said to use "system()". I have no knowledge about these functions. How do I call these functions? Are they appropriate to use?
我做了一些搜索,人们建议使用“fork()”和“exec()”函数。其他人说使用“system()”。我对这些功能一无所知。我如何调用这些函数?它们适合使用吗?
Example code with explanations would be most helpful. Other answers are also welcome. Your help is greatly appreciated.
带有解释的示例代码将是最有帮助的。也欢迎其他答案。非常感谢您的帮助。
回答by Svisstack
#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()
{
/*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;
}
回答by pmg
回答by Vladimir Ivanov
For the most simple case you should two compiled programs in one directory:
对于最简单的情况,您应该在一个目录中放置两个已编译的程序:
> ls
.
hello
second
In second program you just need to call system("hello");
在第二个程序中,您只需要调用 system("hello");
回答by Terminal
If you are new to fork, graphical representation about fork and exec might be helpful to you.
如果您不熟悉 fork,有关 fork 和 exec 的图形表示可能对您有所帮助。
Depiction of fork()
的描述 fork()
+-------------+
|main program |
+-------------+ (fork())
|___________________________
+-------------+ |
|main program | +-------------+
+-------------+ |main program |
| +-------------+
+-------------+ | (exec())
|main program | +-------------+
+-------------+ |hello program|
+-------------+
As you might have already read in a tutorial, after calling fork()
a duplicate copy of the existing program is created, and the exec()
after that replaces that copy with the new program, which you pass to it as arguments. Two execution units for two programs will be running after fork()
.
正如您可能已经在教程中读到的那样,在调用fork()
现有程序的exec()
副本后,会创建一个副本,然后用新程序替换该副本,您将其作为参数传递给它。两个程序的两个执行单元将在 之后运行fork()
。
回答by Basile Starynkevitch
I have done some search, and people suggested the
fork()
andexec()
functions. Others said to usesystem()
. I have no knowledge about these functions. How do I call these functions? Are they appropriate to use?
我已经做了一些搜索,人们建议了
fork()
和exec()
功能。别人说用system()
。我对这些功能一无所知。我如何调用这些函数?它们适合使用吗?
Yes. Read first the documentation (man
page), e.g. of fork(2), exec(3), system(3). Quite probably you have that documentation locally on your computer, using man(1). Notice that system
uses sh
(thru bash(1)or dash(1)), because it is fork
-ing, execve(2)-ing and waitpid(2)-ing the /bin/sh
POSIX shell.
是的。首先阅读文档(man
页面),例如fork(2)、exec(3)、system(3)。很可能您的计算机上有本地文档,使用man(1)。请注意,system
使用sh
(thru bash(1)或dash(1)),因为它是fork
-ing、execve(2)-ing 和waitpid(2)-ing /bin/sh
POSIX shell。
I think that fork
is difficult to understandbecause on success it returns "twice". I won't try to explain it here (I'll need many pages for that). I recommend reading fork (system call)wikipage at first. Then, read some good Linux programming book, e.g Advanced Linux Programming(freely downloadable).
我认为这fork
很难理解,因为成功后它会返回“两次”。我不会在这里解释它(为此我需要很多页)。我建议首先阅读fork(系统调用)wikipage。然后,阅读一些优秀的 Linux 编程书籍,例如Advanced Linux Programming(可免费下载)。
Read also about Virtual Address Space, and proc(5).
另请阅读Virtual Address Space和proc(5)。
You could also read Operating Systems: Three Easy Piecesfor a more general view.
您还可以阅读操作系统:三个简单的部分以获得更一般的视图。