Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a pipe in c++

I'm looking at the code for a c++ program which pipes the contents of a file to more. I don't quite understand it, so I was wondering if someone could write pseudocode for a c++ program that pipes something to something else? Why is it necessary to use fork?

like image 289
node ninja Avatar asked Apr 30 '11 06:04

node ninja


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.

How does one set up a pipe in C?

To create a simple pipe with C, we make use of the pipe() system call. It takes a single argument, which is an array of two integers, and if successful, the array will contain two new file descriptors to be used for the pipeline.

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.


2 Answers

create pipe
fork process
if child:
  connect pipe to stdin
  exec more
write to pipe

You need fork() so that you can replace stdin of the child before calling, and so that you don't wait for the process before continuing.

like image 63
Ignacio Vazquez-Abrams Avatar answered Oct 29 '22 14:10

Ignacio Vazquez-Abrams


You will find your answer precisely here

like image 45
vrajs5 Avatar answered Oct 29 '22 13:10

vrajs5