CoffeeRun
CoffeeRun

Reputation: 71

Read doesn't seems to block until something is written to a pipe in a program using fork

Basically I know how to solve the program so that it does what it's suppose to do correctly(which is calculating the multiplication times 10 of a number entered by the user) by adding wait and kill, but I don't understand why it doesn't work as it is. I though the read were blocking but in this case, it seems the read don't block since they don't seem to wait for entries in the pipe. The read of the parents should block until there is a value inside.

Here is the program I'm talking about, why does the entry 1 returns 1 instead of 10? In what order are applied the system call in that case? Is this because the read of the parent reads pfd[1] while var is written in the pipe before the write of the child as the time to take place? I thought the read of the child would clean what is in the pipe before the read of the parent take place.

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>

int multi(int var) {
   int multi;
   int pfd[2];
   pipe(pfd);
   int pid = fork();
   if (pid>0) {
       write(pfd[1], &var, sizeof(var));
       read(pfd[0], &multi, sizeof(multi));
       close(pfd[0]);
   } else {
       read(pfd[0], &var, sizeof(var));
       multi = 10 * var;
       write(pfd[1], &multi, sizeof(multi));
       close(pfd[1]);
   }
   return multi;
}

int main(void) {
   int x;
   fscanf(stdin, "%d", &x);
   printf("%d\n", multi(x));
}

Upvotes: 0

Views: 76

Answers (1)

Barmar
Barmar

Reputation: 780842

The order of execution of the parent and child is unpredictable. If the parent executes its read(pfd[0]) before the child does, it will simply read what it just wrote. Adding a call to wait() ensures that the parent won't try to read from the pipe until after the child has read and written the reply.

Another solution is to use two pipes, one for parent->child communication, and another for child->parent.

Upvotes: 1

Related Questions