Note that there are some explanatory texts on larger screens.

plurals
  1. POPersistent execvp on pipe?
    text
    copied!<p>I am working on an assignment for my Operating System class (Posix &amp; C), building a mini-shell, and I don't know how to solve the following problem:</p> <p>My mini-shell has to accept two commands, for example <code>ls | grep a</code>. For that I create a pipe of size two and a child. The child closes all that it has to close and opens all that it has to open (standard/pipe's in &amp; out). It then executes "ls," using execvp. I am sure this works well. After that, the parent shuts and opens inputs and outputs (I am sure I do it well), and then executes <code>grep a</code>.</p> <p>Now, the problem is that the process <code>grep a</code> never finishes. Same for <code>tail -1</code>, e.g.. Yet it does work for <code>head -1</code>. I think that happens because <code>grep</code> and <code>tail</code>, which are executed by the parent, wait for more input, even though the child has finished its operation. <code>ls | grep a</code> produces the right output, displayed on the console (The pipe's output is set as default output), but, as I've said, <code>grep a</code> does not finish. </p> <p>So, my question is: how can I inform the parent that the pipe has finished writing, so it can finish the execution of <code>grep a</code> for example?</p> <p>Thank you.</p> <p>Here's the code:</p> <p>[fd is the pipe, it is initialized previously in the code. If you can see any incongruous thing, please let me know; I've cleaned the code a bit, and this is only the problematic part, as you can see.]</p> <pre><code> int fd[2]; pipe(fd); if ((pid = fork()) != -1){ if(pid == 0){ /*Child executing */ close(fd[0]); close(1); dup(fd[1]); close(fd[1]); execvp(argvv[0][0], argvv[0]); /* Here's stored the first instruction */ } else{ /* Parent executing */ wait(&amp;status); close(fd[1]); close(0); dup(fd[0]); close(fd[0]); execvp(argvv[1][0], argvv[1]); /* Here's stored the second instruction */ } } </code></pre>
 

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