C++ c语言中的fork()和pipes()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4812891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:37:13  来源:igfitidea点击:

fork() and pipes() in c

c++c

提问by nightWatcher

What is forkand what is pipe? Any scenarios explaining why their use is necessary will be appreciated. What are the differences between forkand pipein C? Can we use them in C++?

什么是fork什么pipe?任何解释为什么需要使用它们的场景都将不胜感激。CforkpipeC中的区别是什么?我们可以在 C++ 中使用它们吗?

I need to know this is because I want to implement a program in C++ which can access live video input, convert its format and write it to a file. What would be the best approach for this? I have used x264 for this. So far I have implemented the part of conversion on a file format. Now I have to implement it on a live stream. Is it a good idea to use pipes? Capture video in another process and feed it to the other?

我需要知道这是因为我想用 C++ 实现一个程序,该程序可以访问实时视频输入,转换其格式并将其写入文件。什么是最好的方法?我为此使用了 x264。到目前为止,我已经实现了文件格式的转换部分。现在我必须在直播中实现它。使用管道是个好主意吗?在另一个进程中捕获视频并将其提供给另一个进程?

回答by Vijay Mathew

A pipe is a mechanism for interprocess communication. Data written to the pipe by one process can be read by another process. The primitive for creating a pipe is the pipefunction. This creates both the reading and writing ends of the pipe. It is not very useful for a single process to use a pipe to talk to itself. In typical use, a process creates a pipe just before it forksone or more child processes. The pipe is then used for communication either between the parent or child processes, or between two sibling processes. A familiar example of this kind of communication can be seen in all operating system shells. When you type a command at the shell, it will spawn the executable represented by that command with a call to fork. A pipe is opened to the new child process and its output is read and printed by the shell. This pagehas a full example of the forkand pipefunctions. For your convenience, the code is reproduced below:

管道是进程间通信的机制。一个进程写入管道的数据可以被另一个进程读取。创建管道的原语是pipe函数。这将创建管道的读取端和写入端。单个进程使用管道与自身对话并不是很有用。在典型的使用中,进程会在forks一个或多个子进程之前创建一个管道。然后管道用于父进程或子进程之间或两个兄弟进程之间的通信。在所有操作系统 shell 中都可以看到这种通信的常见示例。当您在 shell 中键入命令时,它会通过调用该命令生成由该命令表示的可执行文件fork. 一个管道被打开到新的子进程,它的输出被 shell 读取和打印。此页面包含forkpipe功能的完整示例。为方便起见,代码复制如下:

 #include <sys/types.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>

 /* Read characters from the pipe and echo them to stdout. */

 void
 read_from_pipe (int file)
 {
   FILE *stream;
   int c;
   stream = fdopen (file, "r");
   while ((c = fgetc (stream)) != EOF)
     putchar (c);
   fclose (stream);
 }

 /* Write some random text to the pipe. */

 void
 write_to_pipe (int file)
 {
   FILE *stream;
   stream = fdopen (file, "w");
   fprintf (stream, "hello, world!\n");
   fprintf (stream, "goodbye, world!\n");
   fclose (stream);
 }

 int
 main (void)
 {
   pid_t pid;
   int mypipe[2];

   /* Create the pipe. */
   if (pipe (mypipe))
     {
       fprintf (stderr, "Pipe failed.\n");
       return EXIT_FAILURE;
     }

   /* Create the child process. */
   pid = fork ();
   if (pid == (pid_t) 0)
     {
       /* This is the child process.
          Close other end first. */
       close (mypipe[1]);
       read_from_pipe (mypipe[0]);
       return EXIT_SUCCESS;
     }
   else if (pid < (pid_t) 0)
     {
       /* The fork failed. */
       fprintf (stderr, "Fork failed.\n");
       return EXIT_FAILURE;
     }
   else
     {
       /* This is the parent process.
          Close other end first. */
       close (mypipe[0]);
       write_to_pipe (mypipe[1]);
       return EXIT_SUCCESS;
     }
 }

Just like other C functions you can use both forkand pipein C++.

就像其他 C 函数一样,您可以在 C++ 中使用forkpipe

回答by ZijunLost

there are stdinand stdoutfor common input and output.

stdinstdout用于公共输入和输出。

A common style is like this:

一个常见的样式是这样的:

input->process->output

But with pipe, it becomes:

但是有了管道,它变成了:

input->process1->(tmp_output)->(tmp-input)->process2->output

pipeis the function that returns the two temporary tmp-inputand tmp-output, i.e. fd[0]and fd[1].

pipe是返回两个临时tmp-input和的函数tmp-output,即fd[0]fd[1]