[英]Execute program from within a C program
我应该如何从我的C
程序中运行另一个程序? 我需要能够将数据写入已启动程序的STDIN
(并且可能从它的STDOUT
中读取)
我不确定这是否是标准的 C function。 我需要应该在 Linux 下工作的解决方案。
不久前,我为其他人编写了一些示例 C 代码,展示了如何执行此操作。 这是给你的:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
void error(char *s);
char *data = "Some input data\n";
main()
{
int in[2], out[2], n, pid;
char buf[255];
/* In a pipe, xx[0] is for reading, xx[1] is for writing */
if (pipe(in) < 0) error("pipe in");
if (pipe(out) < 0) error("pipe out");
if ((pid=fork()) == 0) {
/* This is the child process */
/* Close stdin, stdout, stderr */
close(0);
close(1);
close(2);
/* make our pipes, our new stdin,stdout and stderr */
dup2(in[0],0);
dup2(out[1],1);
dup2(out[1],2);
/* Close the other ends of the pipes that the parent will use, because if
* we leave these open in the child, the child/parent will not get an EOF
* when the parent/child closes their end of the pipe.
*/
close(in[1]);
close(out[0]);
/* Over-write the child process with the hexdump binary */
execl("/usr/bin/hexdump", "hexdump", "-C", (char *)NULL);
error("Could not exec hexdump");
}
printf("Spawned 'hexdump -C' as a child process at pid %d\n", pid);
/* This is the parent process */
/* Close the pipe ends that the child uses to read from / write to so
* the when we close the others, an EOF will be transmitted properly.
*/
close(in[0]);
close(out[1]);
printf("<- %s", data);
/* Write some data to the childs input */
write(in[1], data, strlen(data));
/* Because of the small amount of data, the child may block unless we
* close it's input stream. This sends an EOF to the child on it's
* stdin.
*/
close(in[1]);
/* Read back any output */
n = read(out[0], buf, 250);
buf[n] = 0;
printf("-> %s",buf);
exit(0);
}
void error(char *s)
{
perror(s);
exit(1);
}
pipe(...)
创建两个管道,一个用于stdin
,一个用于stdout
。fork(...)
进程。fork(...)
返回 0 的那个) dup (...)
到stdin
/ stdout
的管道。exec[v][e]
子进程中要启动的programm文件。fork
返回子进程的 PID)执行一个循环,从子进程的stdout
( select(...)
或poll(...)
, read(...)
)中读取缓冲区,直到孩子终止( waitpid(...)
)。stdin
输入上为孩子提供输入。close(...)
管道。对于简单的单向通信,popen() 是一个不错的解决方案。 但是,它对双向通信没有用。
IMO,imjorge (Jorge Ferreira) 给出了双向通信的大部分答案(80%?) - 但省略了一些关键细节。
如果您不关闭管道的未使用端,那么当其中一个程序终止时,您将不会获得明智的行为; 例如,孩子可能正在从其标准输入读取,但除非 pipe 的写入端在孩子中关闭,否则它将永远不会获得 EOF(读取的零字节),因为它仍然有 pipe 打开并且系统认为它有时可能会开始写 pipe,即使它当前挂起等待从中读取内容。
写入进程应考虑是否处理在没有读取进程的 pipe 上写入时给出的 SIGPIPE 信号。
您必须了解 pipe 容量(取决于平台,可能只有 4KB)并设计程序以避免死锁。
您可以使用系统调用,阅读系统手册(3)
我认为你可以使用
freopen
为了这。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.