Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with simple pipe communication in C
    text
    copied!<p>I have a problem with this exercise:</p> <p>Write a program in C that creates a child and between father and child, there will be two-way communication using pipes. His father would read a file (whose name will be given the user) and will send letters to the child.</p> <p>The child will count the number of words starting <s>from</s> with 'a'; if the number <s>is, whether X</s> of words, X, starting with 'a' is greater than 5, then the child will create his own child (grandchild). The grandchild of the establishment will send, by whatever means you deem possible, the grandfather <s>*</s> value of X <s>number</s> and exit. [<em>By inference: if the number X is less than 6, then the child simply exits, and the 'would be grandparent' also exits.</em>]</p> <ul> <li>Note: Grandfather = initial process of the father, that father of his father's grandchild</li> </ul> <p>Here is what I have done till now; please help me...</p> <pre><code>#include &lt;stdio.h&gt; #include&lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;fcntl.h&gt; int main(int argc, char *argv[]) { int fd1[2], fd2[2], pid, status,sum=0, i; char gram[100], x; char buff[100]; char rev[1000]; FILE *fp; if (pipe(fd1) == -1) { /* Create a pipe */ perror("pipe"); exit(1); } pid = fork(); switch (pid) { case -1: perror ("Fork error\n"); exit(99); // in case of error case 0: close(fd1[1]);//Close the writing side rev[1000]= read(fd1[0], buff, 1000); /* Read from the pipe */ /*for (i=0; i&lt; 1000 ; i++) { rev[i] = read(fd1[0], buff, sizeof(buff)); }*/ while( rev[i] != '\0') { if (buff[i] == 'a' ) { sum++; i++; } if (rev[i] == "") { if (rev[i+1]) //elenxei to epomeno tou kenou { sum++; i++; } } i++; } printf("%d", sum); exit(0); default: printf("dwse arxeio\n"); scanf("%s", gram); close(fd1[0]);//Close the reading side fp = fopen (gram,"r"); getc(fp); fclose(fp); write(fd1[1], buff, sizeof(buff)+1); close(fd1[1]);//Close the writing side wait(&amp;status); // waits till the child process ends } } </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