使用字符串输入和输出运行过程 [英] Run process with string input and output

查看:99
本文介绍了使用字符串输入和输出运行过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于fork()和exec()的问题很多.我还没有找到真正使它们使用过程简单的方法,而使程序员的生活变得简单是目标.

There are plenty of questions on here related to fork() and exec(). I have not found one that really makes the process of using them simple though, and making programmer's lives simple is the goal.

我需要执行以下操作的C ++,Linux友好函数:

I need a C++, linux-friendly function that does the following:

string RunCommand(string command, string input){}

该函数应该能够运行诸如grep之类的shell命令,并将输入内容管道"到其中,并读取ouptut并将其返回.因此,如果我要在命令行执行以下操作:

This function should be able to run a shell command, like grep, and "pipe" the content of input into it and read the ouptut and return it. So if I would do the following at the command line:

ps -elf | grep somequerytext

我会在代码中这样做:

string psOutput = RunCommand("ps -elf","");
string grepOutput = RunCommand("grep somequerytext", psOutput);

* edit:问题是RunCommand函数的最佳实现是什么.

*edit: The question is what is the best implementation of the RunCommand function.

* edit:为简化起见,popen被认为是一种解决方案,但是popen限制您将数据输入管道或输出管道,但不能同时使用这两种方法.

*edit: popen was considered as a solution for simplicity, but popen restricts you to piping data in or piping data out, but not both.

推荐答案

您似乎需要一个函数来

  • 创建两个管道和叉子.
  • 子进程然后执行:
    • 复制适当的管道描述符,以使一个文件描述符为标准输入,而一个为标准输出
    • 关闭管道描述符
    • 将命令字符串拆分为参数
    • 使用参数运行命令
    • Create two pipes and fork.
    • The child process then does:
      • Duplicate appropriate descriptors of the pipes so that one file descriptor is standard input and one standard output
      • Close the pipe descriptors
      • Split up the command string into arguments
      • Run the command with the arguments
      • 关闭适当的管道文件描述符
      • 将输入字符串写入子级,并将管道关闭到子级的标准输入
      • 从子项读取输出字符串
      • 从子级的标准输出中关闭管道
      • 等待孩子死亡

      此轮廓的唯一潜在问题是,如果子级在完成读取其输入之前就写入了输出,并且写入了太多的输出以至于管道已满(它们的容量有限且通常很小).在这种情况下,进程将陷入僵局-父级试图向子级写入数据,而子级试图向父级写入数据,并且两者都停留在等待对方读取某些数据的过程中.您可以通过在父级中有两个线程来避免这种情况,一个线程处理写入,另一个线程处理读取.或者,您可以使用两个子进程,一个用于运行命令,一个用于写入标准输入,而父进程则从命令的标准输出中读取一个字符串.

      The only potential problem with this outline is if the child writes output before it is finished reading its input, and it writes so much output that the pipe is full (they have a finite and usually quite small capacity). In that case, the processes will deadlock - the parent trying to write to the child, and the child trying to write to the parent, and both stuck waiting for the other to read some data. You can avoid that by having two threads in the parent, one processing the writing, the other processing the reading. Or you can use two child processes, one to run the command and one to write to the standard input, while the parent reads from the command's standard output into a string.

      之所以没有标准功能来执行此操作,原因之一就是难以确定合适的语义.

      One of the reasons there isn't a standard function to do this is precisely the difficulty of deciding what are the appropriate semantics.

      我忽略了错误处理和信号处理问题;他们增加了这一切的复杂性.

      I've ignored error handling and signal handling issues; they add to the complexity of it all.

      这篇关于使用字符串输入和输出运行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆