从C程序中执行程序

从C程序中执行程序,c,linux,C,Linux,如何从我的C程序中运行另一个程序,我需要能够将数据写入已启动程序的STDIN(在执行程序时,我必须多次通过STDIN提供输入)(并从其STDOUT逐行读取) 我需要在Linux下工作的解决方案 在浏览网络时,我发现以下代码: #include <sys/types.h> #include <unistd.h> #include <stdio.h> void error(char *s); char *data = "Some input data\n";

如何从我的C程序中运行另一个程序,我需要能够将数据写入已启动程序的STDIN(在执行程序时,我必须多次通过STDIN提供输入)(并从其STDOUT逐行读取)

我需要在Linux下工作的解决方案

在浏览网络时,我发现以下代码:

#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);
}
#包括
#包括
#包括
无效错误(字符*s);
char*data=“一些输入数据\n”;
main()
{
int in[2],out[2],n,pid;
char-buf[255];
/*在管道中,xx[0]用于读取,xx[1]用于写入*/
如果(管道(in)<0)错误(“管道in”);
如果(管道(输出)<0)错误(“管道输出”);
如果((pid=fork())==0){
/*这是子进程*/
/*关闭标准输入、标准输出、标准输出*/
关闭(0);
关闭(1);
关闭(2);
/*制造我们的管道,我们的新标准,标准和标准*/
dup2(in[0],0);
dup2(out[1],1);
dup2(out[1],2);
/*关闭父级将使用的管道的其他端点,因为如果
*我们在子级中保留这些打开状态,子级/父级将不会获得EOF
*当父/子对象关闭其管道末端时。
*/
关闭(在[1]中);
关闭(关闭[0]);
/*使用hextump二进制文件重写子进程*/
execl(“/usr/bin/hextump”、“hextump”、“-C”、(char*)NULL);
错误(“无法执行hextump”);
}
printf(“将'hextump-C'派生为pid%d\n'处的子进程”,pid);
/*这是父进程*/
/*关闭子对象用于读取/写入so的管道端点
*当我们关闭其他设备时,EOF将正确传输。
*/
关闭(在[0]中);
关闭(关闭[1]);
printf(“%s”,buf);
出口(0);
}
无效错误(字符*s)
{
佩罗尔(s);
出口(1);
}
但如果我的C程序(需要执行USNGEXEC)只从stdin读取一次输入并返回输出,那么这段代码工作正常 一次。但是如果我的Cprogram(需要执行usng exec)多次接收输入(不知道它将从stdin读取多少次输入) 并多次显示输出put mork(执行时在标准输出上逐行显示输出) 然后这个代码就崩溃了。有谁能建议如何解决这个问题? 实际上,我的C程序(需要usng exec执行)逐行显示一些输出,根据输出,我必须在标准输入上提供输入 并且这种读/写的次数不是恒定的

请帮助我解决此问题。

当您可以读取/写入文件描述符时,您可以使用api获得通知。 因此,您基本上会将读写调用放入一个循环中,然后运行select以了解外部程序何时消耗了一些字节或向stdout写入了一些内容。

您可以使用api在读取/写入文件描述符时获得通知。
因此,您基本上会将读写调用放入一个循环中,然后运行select以确定外部程序何时消耗了一些字节或向stdout写入了一些内容。

/*由于数据量较小,除非我们*关闭其输入流,否则子程序可能会阻塞。这将向其*stdin.*/你不这样做怎么样?明白你为什么要问同样的问题两次吗?詹姆斯,再问同样的问题也不会给你答案。你可以修改你的问题或者问一个更具体的问题以获得更好的回答,但是复制粘贴同样的问题对你没有帮助。你可能会发现,如果你1)保持你的问题简明扼要,2)不要在其中抛出大量代码,你会得到更多的回答。/*由于数据量小,除非我们*关闭孩子的输入流,否则孩子可能会阻塞。这将向其*stdin.*/你不这样做怎么样?明白你为什么要问同样的问题两次吗?詹姆斯,再问同样的问题也不会给你答案。你可以修改你的问题或者问一个更具体的问题以获得更好的回答,但是复制粘贴同样的问题对你没有帮助。你可能会发现,如果你1)保持你的问题简洁明了,2)不要在其中添加太多的代码,你会得到更多的回答。嗨,鲁迪,你能提供一些示例代码吗?我应该使用posix线程编码等吗?如果是,请提供一些与此问题相关的示例代码。我确实无法解决此问题problem@james:您的问题不是线程条件或任何东西,您的问题是没有执行写入->读取->写入->读取->的循环。。。序列另外,你正在关闭孩子的stdin,这意味着你与孩子的沟通渠道已经消失。你确定你的其他程序可以读取EOF以外的内容吗?嗨,鲁迪,你能提供一些示例代码吗?我应该使用posix线程编码等吗?如果是,请提供一些与此问题相关的示例代码。我确实无法解决此问题problem@james:您的问题不是线程条件或任何东西,您的问题是没有执行写入->读取->写入->读取->的循环。。。序列另外,你正在关闭孩子的stdin,这意味着你与孩子的沟通渠道已经消失。您确定您的其他程序可以读取EOF以外的内容吗?