Note that there are some explanatory texts on larger screens.

plurals
  1. POparent process, and a child process
    primarykey
    data
    text
    <p>I am trying to write a program that The parent process will take the arguments to main() and send the characters in them one at a time to the child process through a pipe (one call to write for each character). The child process will count the characters sent to it by the parent process and print out the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever. The child should return normally and not have the parent kill the child.</p> <p>Am i counting the arguments right? am i sending the arguments in one at a time, and am i reaping the child? </p> <pre><code>#include &lt;sys/wait.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;string.h&gt; #define size = 100; int main(int argc, char *argv[]) { int i, count =0; int c; int fdest[2]; // for pipe pid_t pid; //process IDs char buffer[BUFSIZ]; if (pipe(fdest) &lt; 0) /* attempt to create pipe */ perror( "pipe" ); if ((pid = fork()) &lt; 0) /* attempt to create child / parent process */ { perror( "fork" ); } /* parent process */ else if (pid &gt; 0) { close(fdest[0]); for (i=1; i &lt; argc; ++i) { for (c=0; c &lt; strlen(argv[i]); ++c) { write(fdest[1], &amp;argv[i][c], 1); } } close(fdest[1]); wait(NULL); exit(0); } else { /* child Process */ close(fdest[1]); while (read(fdest[0], &amp;buffer, 1) &gt; 0) { count++; } printf("\nchild: counted %d characters\n", count); } wait(NULL); exit(0); } </code></pre>
    singulars
    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.
    plurals
    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