Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fork() and pipes() in c

Tags:

c++

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?

like image 514
nightWatcher Avatar asked Jan 27 '11 04:01

nightWatcher


People also ask

What is pipe () in C?

3 years ago. pipe() is a Linux system function. The pipe() system function is used to open file descriptors, which are used to communicate between different Linux processes. In short, the pipe() function is used for inter-process communication in Linux.

What is fork () in C?

fork() in CThis fork system call is used to create a new process. This newly created process is known as child process. The current process which is creating another child process is called the parent process. A child process uses the same program counter, CPU register, same files that are used by the parent process.

What is fork () and why is it used?

fork() is how you create new processes in Unix. When you call fork , you're creating a copy of your own process that has its own address space. This allows multiple tasks to run independently of one another as though they each had the full memory of the machine to themselves.

How does pipe () work?

pipe() is a system call that facilitates inter-process communication. It opens a pipe, which is an area of main memory that is treated as a "virtual file". The pipe can be used by the creating process, as well as all its child processes, for reading and writing.


1 Answers

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++.

like image 91
Vijay Mathew Avatar answered Oct 04 '22 06:10

Vijay Mathew