Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用popen同时输入和输出?_C_Linux_Unix_Pipe - Fatal编程技术网

C 如何使用popen同时输入和输出?

C 如何使用popen同时输入和输出?,c,linux,unix,pipe,C,Linux,Unix,Pipe,我使用psuedo代码来表达我想要做的事情: FILE* fd = popen("/bin/cat", ...); Write some data to the stdin of `/bin/cat` using fd; Read all data from the stdout of `/bin/cat` using fd; 可能吗?popen()只能从新进程中读取。如果你需要读和写 使用pipe()创建将连接到新进程的管道 使用Fork() 将进程的输入和输出重定向到先前使用dup2()

我使用psuedo代码来表达我想要做的事情:

FILE* fd = popen("/bin/cat", ...);
Write some data to the stdin of `/bin/cat` using fd;
Read all data from the stdout of `/bin/cat` using fd;
可能吗?

popen()只能从新进程中读取。如果你需要读和写

  • 使用
    pipe()
    创建将连接到新进程的管道
  • 使用
    Fork()
  • 将进程的输入和输出重定向到先前使用
    dup2()创建的管道
  • 使用
    exec
    系列函数对子进程(新进程)调用exec

此外,您需要使用多路输入和输出,例如,并且您可能需要一些