fork() and pipes() in c

What is fork and what is pipe? Any scenarios explaining why their use is necessary will be appreciated. What are the differences between fork and pipe in C? Can we use them in 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?


Solution 1:

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

Just like other C functions you can use both fork and pipe in C++.

Solution 2:

there are stdin and stdout for common input and output.

A common style is like this:

input->process->output

But with pipe, it becomes:

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

pipe is the function that returns the two temporary tmp-input and tmp-output, i.e. fd[0] and fd[1].