Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I tell wc to stop reading?
    primarykey
    data
    text
    <p>I have a program that <code>fork()</code>s a new process and then overwrites that new process's <code>stdin</code> with my file descriptor pipe <code>fd</code>. In this process, I then use <code>execvp()</code> to execute <code>wc</code> and it should read its input from the parent. In the parent, I then write to the write end of the pipe and when done writing, close the pipe.</p> <p>The problem is that <code>wc</code> is still expecting input and des not exit.<br> Usually, I can stop <code>wc</code> by typing <kbd>CTRL</kbd><kbd>D</kbd> but I can't seem to send that signal in C/C++.</p> <p>How do I tell <code>wc</code> to stop reading?</p> <p><strong>EDIT</strong>: I did follow the pipe/fork/dup/exec idiom (I think that's what it's called.)<br> The streaming works with other programs that need input, its just that <code>wc</code> needs the special <code>EOF</code> to stop reading.</p> <pre><code>int in_fd[2]; //parent-child setup pipe(in_fd); child = fork(); if(child == 0) { close(in_fd[1]); dup2(in_fd[0], 0); close(in_fd[0]); execvp(cmdArg[0], cmdArg); } else { close(in_fd[0]); in_pid = fork_in_proc(); close(in_fd[1]); } //writing function pid_t fork_in_proc() { string line; pid_t in_pid; if((in_pid = fork()) == 0) { close(in_fd[0]); ifstream file(stream_file[STREAM_IN].c_str(), ios_base::in); if (file.bad()) { cerr &lt;&lt; "File read error\n"; return 1; } while(file.good()) { getline(file, line); if(file.good()) { write(in_fd[1], line.c_str(), line.length()); } } int end = 3; write(in_fd[1], &amp;end, sizeof(end)); file.close(); close(in_fd[1]); cout &lt;&lt; "PIPE IN" &lt;&lt; endl; exit(0); } else { return in_pid; } } </code></pre> <p>Sorry if the code seems a little disjointed. I had to pull it together from around the file.</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.
 

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