fork()和管(c)中 [英] fork() and pipes() in c

查看:103
本文介绍了fork()和管(c)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么fork()的,什么是管道(),为什么我们同时使用?
任何之情况解释为什么同时使用的需要将AP preciated。
是什么fork()和管(c)中的区别?
我们可以同时使用在C ++中吗?

what is fork() and what is pipes(), why do we use both? Any scenerio explaining why the use of both is necessary will be appreciated. What is the difference between fork() and pipes() in c? Can we use both in c++ too?

我需要知道这是因为..
我想实现C ++中的程序,它可以访问实时视频输入,转换的格式,并将其写入文件。
有什么可以为这个最好的方法?
我用X264这一点。到目前为止,我已经实现了转换的文件格式的部分。
现在,我要实现它的实时流。它是用管道是一个好主意?捕获视频在另一个进程,并转达给其他的?
任何帮助将AP preciated

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 can 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? Any help will be appreciated

推荐答案

一个管道是进程间通信机制。由一个进程写入到管道的数据可以由另一个过程读取。原始创建一个管道是管道功能。这造成的读出和管道的写入两端。这不是非常有用的一个进程使用的管道交谈本身。在典型应用中,一个过程之前它的一个或多个子进程创建一个管道。然后管被用于通信或者父或子进程之间,或两个兄弟进程之间。这种沟通的一个类似的例子可以在所有的操作系​​统外壳可以看到。当你在shell键入的命令,那么将产生由该命令与呼叫psented可执行重新$ P $到。的管被打开到新的子过程和它的输出被读出和由shell打印。 此页面具有的一个完整的例子管道功能。为了您的方便,code抄录如下:

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 pipe function. 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 forks one 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 page has a full example of the fork and pipe functions. For your convenience, the code is reproduced below:

 #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;
     }
 }

就像你可以同时使用叉其他C函数管道在C ++中。

这篇关于fork()和管(c)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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