Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you want bidirectional access to the process, you would have to do what popen does behind the scenes explicitly with pipes. I am not sure if any of this will change in C++, but here is a pure C example : </p> <pre><code>void piped(char *str){ int wpipefd[2]; int rpipefd[2]; int defout, defin; defout = dup(stdout); defin = dup (stdin); if(pipe(wpipefd) &lt; 0){ perror("Pipe"); exit(EXIT_FAILURE); } if(pipe(rpipefd) &lt; 0){ perror("Pipe"); exit(EXIT_FAILURE); } if(dup2(wpipefd[0], 0) == -1){ perror("dup2"); exit(EXIT_FAILURE); } if(dup2(rpipefd[1], 1) == -1){ perror("dup2"); exit(EXIT_FAILURE); } if(fork() == 0){ close(defout); close(defin); close(wpipefd[0]); close(wpipefd[1]); close(rpipefd[0]); close(rpipefd[1]); //Call exec here. Use the exec* family of functions according to your need } else{ if(dup2(defin, 0) == -1){ perror("dup2"); exit(EXIT_FAILURE); } if(dup2(defout, 1) == -1){ perror("dup2"); exit(EXIT_FAILURE); } close(defout); close(defin); close(wpipefd[1]); close(rpipefd[0]); //Include error check here write(wpipefd[1], str, strlen(str)); //Just a char by char read here, you can change it accordingly while(read(rpipefd[0], &amp;ch, 1) != -1){ write(stdout, &amp;ch, 1); } } } </code></pre> <p>Effectively you do this : </p> <ol> <li>Create pipes and redirect the stdout and stdin to the ends of the two pipes (note that in linux, pipe() creates unidirectional pipes, so you need to use two pipes for your purpose).</li> <li>Exec will now start a new process which has the ends of the pipes for stdin and stdout.</li> <li>Close the unused descriptors, write the string to the pipe and then start reading whatever the process might dump to the other pipe. </li> </ol> <p>dup() is used to create a duplicate entry in the file descriptor table. While dup2() changes what the descriptor points to.</p> <p>Note : As mentioned by Ammo@ in his solution, what I provided above is more or less a template, it will not run if you just tried to execute the code since clearly there is a exec* (family of functions) missing, so the child will terminate almost immediately after the fork().</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload