ppolicherla91
ppolicherla91

Reputation: 33

exchanging information between processes using pipes

so I am trying to get the program to pass info from one process to another. it is supposed to start at the child and subtract 5 then go to the parent and divide by 5. the problem I am seeming to have is that the output is coming out where the parent has the right value but the child does not and it comes out in some strange order.

Expected output:

x = 19530
ITERATION 1
Child : x = 19525
Parent : x = 3905
ITERATION 2
Child : x = 3900
Parent : x = 780
ITERATION 3
Child : x = 775
Parent : x = 155
ITERATION 4
Child : x = 150
Parent : x = 30
ITERATION 5
Child : x = 25
Parent : x = 5

here is my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

int main()
{
   int x = 19530;
   pid_t childpid;
   int child[2], parent[2];
   int i, j;

   if (pipe(parent) == -1 || pipe(child) == -1)
   {
       perror("failed to pipe");
       exit(-1);
   }

   if ((childpid = fork()) <= -1)
   {
       perror("failed to fork");
       exit(-1);
   }

   if (childpid == 0)
   {
        //close(parent[1]);
        //close(child[0]);
       for (j = 0; j < 5; j++)
       {
          x -= 5;

          if (write(child[1], &x, sizeof(x)) <= 0)
          {
            perror("Child: failed to write to the pipe");
            exit(-1);
          }
          printf("Child : x = %d\n", x);
        }
        //close(parent[0]);
        //close(child[1]);

        exit(-1);
    }

    else
    {
        //close(parent[0]);
        //close(child[1]);
        for (i = 0; i < 5; i++)
        {
            sleep(1);
            x /= 5;
            x -= 1;
            if (write(parent[1], &x, sizeof(x)) <= 0)
            {
                 perror("parent: failed");
                 exit(-1);
            }
            printf("Parent : x = %d\n", x);
         }
         //close(parent[1]);
         //close(child[0]);

         exit(-1);
     }

     return 0;

}

The output that I get: Child : x = 19525
Child : x = 19520
Child : x = 19515
Child : x = 19510
Child : x = 19505
Parent : x = 3905
Parent : x = 780
Parent : x = 155
Parent : x = 30
Parent : x = 5

not really sure where I am going wrong. The parent is giving the right value but the child is not. and not sure how to get them to print in the right order.

Upvotes: 1

Views: 135

Answers (1)

vim_
vim_

Reputation: 2170

You need to use read to get the updated value from the other process. Here is what happens in every iteration:

 Child:
   1. read x from parent pipe
   2. x = x - 5
   3. write x to child pipe
 Parent:
   1. write x to the parent pipe
   2. read x from child pipe
   3. x = x / 5

Below code provides correct output:

 #include <errno.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <string.h>
 #include <stdlib.h>

 int main() {
   int x = 19530;
   pid_t childpid;
   int child[2], parent[2];
   int i, j;

   if (pipe(parent) == -1 || pipe(child) == -1) {
     perror("failed to pipe");
     exit(-1);
   }

   if ((childpid = fork()) <= -1) {
     perror("failed to fork");
     exit(-1);
   }
   if (childpid == 0){  // child
     close(parent[1]);
     close(child[0]);
   }else {             // parent
     close(parent[0]);
     close(child[1]); 
   }
   for (int i = 0; i < 5; i ++){
      if (childpid == 0) {  // child
        sleep(1);
        printf("ITERATION %d\n", i+1);
        // 1. read 
        if (read(parent[0], &x, sizeof(x)) <= 0) {
          perror("Child: failed to read from the pipe");
          exit(-1);
        }
        // 2. subtract 
        x -= 5;

        // 3. write
        if (write(child[1], &x, sizeof(x)) <= 0) {
          perror("Child: failed to write to the pipe");
          exit(-1);
        }
        printf("Child : x = %d\n", x);
      } else {
        // 1. write 
        if (write(parent[1], &x, sizeof(x)) <= 0) {
          perror("parent: failed");
          exit(-1);
        }
        // 2. read
        if (read(child[0], &x, sizeof(x)) <= 0) {
          perror("Parent: failed to read from the pipe");
          exit(-1);
        }
        // 3. divide
        x /= 5;
        printf("Parent : x = %d\n", x);
      }
    }
   return 0;
 }

Upvotes: 1

Related Questions