Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to read entirety of pipe

I am working on a program where the main program forks itself and the child process calls exec. I have set it up so that the parent process has 2 pipes StdOutPipe and StdInPipe, and the child process calls dup so that stdout writes to the StdOutPipe and stdin reads from StdInPipe. Then the parent process calls wait, after which i would like to read the entirety of the StdOutPipe into a buffer. I know you can do so by reading one character at a time, but is there a faster way to do so?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For performance reasons, one typically reads a chunk at a time, not a character at a time.

  1. Loop,
    1. Attempt to enlarge the buffer so it can fit CHUNK_SIZE more bytes.
    2. If an error occurred,
      1. Fail.
    3. Attempt to read CHUNK_SIZE bytes from the pipe into the unused part of the buffer.
    4. If an error occurred,
      1. Fail.
    5. If EOF was reached,
      1. Break.
    6. Increased the total number of bytes read by the number of bytes read.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading