关于linux:从C程序中执行程序

关于linux:从C程序中执行程序

Execute program from within a C program

如何在我的C程序中运行另一个程序?我需要能够将数据写入已启动程序的STDIN(并且可能从其的STDOUT中读取)

我不确定这是否是标准的C函数。我需要在Linux下可以使用的解决方案。


您要使用popen。它为您提供了一个单向管道,您可以使用该管道访问程序的stdin和stdout。

popen是现代unix和类unix操作系统的标准配置,其中Linux是其中之一:-)

类型

1
man popen

在终端上阅读有关它的更多信息。

编辑

popen是产生单向管道还是双向管道取决于实现。在Linux和OpenBSD中,popen产生单向管道,这些管道是只读或只写的。在OS X上,FreeBSD和NetBSD popen产生双向管道。


我为其他人写了一些示例C代码,展示了如何做到这一点。这是给你的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

void error(char *s);
char *data ="Some input data\
"
;

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\
"
, 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]子进程中要启动的程序文件。
  • 在父进程(其中fork返回子PID的进程)中,执行一个循环,将子stdout(select(...)poll(...)read(...))从子中读取到缓冲区中,直到
    子项终止(waitpid(...))。
  • 最终如果希望孩子在stdin上为其提供输入。
  • 完成close(...)管道后。

  • 对于简单的单向通信,popen()是一个不错的解决方案。但是,它对于双向通信没有用。

    国际海事组织(IMO),伊莫赫(Imjorge)(Jorge Ferreira)给出了双向通信的大部分答案(80%?),但省略了一些关键细节。

  • 至关重要的是,父进程必须关闭用于向子进程发送消息的管道的读取端。
  • 子进程关闭用于向子进程发送消息的管道的写端至关重要。
  • 父进程必须关闭用于向父进程发送消息的管道的写端,这一点至关重要。
  • 子进程关闭用于向父进程发送消息的管道的读取端至关重要。
  • 如果不关闭管道的未使用端,则当其中一个程序终止时,您将不会表现出明智的行为。例如,子级可能正在从其标准输入中进行读取,但是除非在子级中关闭管道的写端,否则它将永远不会得到EOF(读取的零字节),因为它仍然具有打开的管道并且系统认为即使某个管道当前处于挂起状态,正在等待从该管道读取某些内容,有时仍可能会写该管道。

    写入过程应考虑是否处理在没有读取过程的管道上写入时给出的SIGPIPE信号。

    您必须了解管道容量(取决于平台,可能只有4KB),并设计程序以避免死锁。


    您可以使用系统调用,阅读system(3)的联机帮助页。


    我认为您可以使用

    freopen

    为此。


    推荐阅读

      linux数据库升级命令?

      linux数据库升级命令?,系统,信息,时间,最新,网络,名字,地址,管理,简介,传播,l

      linux启动项加命令行?

      linux启动项加命令行?,系统,工作,项目,地址,命令,首页,数字,管理,服务,信息,

      脚本linux上运行命令?

      脚本linux上运行命令?,工具,代码,时间,密码,系统,环境,名字,位置,第三,下来,t

      linux下sh启动命令?

      linux下sh启动命令?,服务,标准,文件,工具,软件,权限,命令,脚本,终端,目录,Lin

      服务启动linux命令?

      服务启动linux命令?,服务,密码,信息,系统,名称,命令,文件,用户,下面,服务器,l

      linux运行命令的脚本?

      linux运行命令的脚本?,系统,服务,工具,脚本,意外,技术,分析,文件,方法,命令,s

      数据库导出linux命令?

      数据库导出linux命令?,密码,数据,数据库,情况,地址,系统,工具,网上,名字,命

      linux运行脚本的命令?

      linux运行脚本的命令?,系统,工具,代码,服务,脚本,状态,密码,环境,位置,暂停,l

      linux读取命令行参数?

      linux读取命令行参数?,系统,信息,数据,名称,软件,位置,标准,灵活,百度,资料,L

      linux命令行运行中断?

      linux命令行运行中断?,连续,工作,系统,信息,程序,命令,设备,工具,网络,情况,L

      vim运行linux命令?

      vim运行linux命令?,系统,工作,信息,地址,命令,标准,时间,情况,工具,基础,linu

      linux启动进命令行?

      linux启动进命令行?,系统,首页,密码,工具,终端,情况,电脑,数字,界面,命令,如

      linux命令与数据流?

      linux命令与数据流?,工作,地址,系统,信息,命令,目录,标准,网络,管理,常用命

      linux启动蓝牙命令?

      linux启动蓝牙命令?,设备,手机,系统,蓝牙,电脑,管理,网络,密码,通讯,信息,Lin

      linux启动进去命令行?

      linux启动进去命令行?,系统,工具,首页,电脑,终端,材料,密码,命令,快捷键,窗

      linux下并行运行命令?

      linux下并行运行命令?,系统,服务,工作,命令,环境,网络,暂停,文件,脚本,参数,l

      linux恢复数据库命令?

      linux恢复数据库命令?,工具,系统,软件,数据,盘中,密码,命令,备份,数据库,文

      linux启动显示命令行?

      linux启动显示命令行?,系统,密码,终端,状态,首页,情况,基础,电脑,信息,工具,l

      linux储存命令数据?

      linux储存命令数据?,系统,工作,地址,信息,标准,命令,工具,实时,数据,分析,lin