1

I am trying to understand using pipes in C, specifically writing and reading integers. I have a parent process that creates 3 child processes. Two of the child processes calculate numbers and write them to a pipe. The third process reads from both pipes and then displays. Sounds simple right?

I found this post: How to send integer with pipe between two processes! on how to send integers through pipes and followed it, but I am not producing the correct output.

Edit: for further clarification, I initiate the pipes like so:

int p1[2]; //pipe1 
int p2[2]; //pipe2
pipe(p1); //intialize pipe1 for between process 1 & 3
pipe(p2); //initialize pipe2 for between process 2 & 3

After some debugging, I notice that the wrong number is being written to the pipe. This is how I am writing an int to the pipe:

int c0 = 18;
write(p2[1], &c0, sizeof(c0));

and this is how I am reading:

int disp[4];
read(p1[0], &disp[0], sizeof(disp[0]));

and so on until the array is full.

Now instead of writing something like 14 to the pipe, it writes a large number like 17462. I am assuming that it is writing the address, right? If so, how would I write the actual integer to the pipe? Should I remove the '&' from the statements, because doing that gives me errors about casting. Any tips, advice, comments are always appreciated. Thanks.

Community
  • 1
  • 1
Seephor
  • 1,692
  • 3
  • 28
  • 50
  • Just in case; what type is `c0`? Also did you check the return value of `read`? It should return the number of bytes read if successful. – Matti Virkkunen Feb 07 '12 at 17:47
  • made an edit above. c0 is of type int. I have not checked the return value of read, but I have checked the value of disp[0] after the read command and it is a high number. – Seephor Feb 07 '12 at 17:54
  • 1
    Check the return value of read. The value in `disp[0]` is not valid unless it returns `sizeof(int)`. On Unix, *any* read or write -like system call can fail at any time, and that can result in a partial read or write. You must check the return value. – Matti Virkkunen Feb 07 '12 at 17:57
  • I notice the name of your pipefd array is different in the `read()` and `write()` call... I would have expected you to be suing the same pair of fds from the same `pipe()` call? Could just be that it got passed around and named differently, but worth asking. – FatalError Feb 07 '12 at 17:59
  • the names of the pipes are different because one process writes to pipe2 (p2) and the other writes to pipe 1(p2). I just copied code from the process that writes to p2, but in the third process it reads from both pipes which is why you see a read from p1. – Seephor Feb 07 '12 at 18:05
  • @Matti, i checked the value of read and it was -1, what does that mean? edit: i see that -1 means it was a failed read, but how? – Seephor Feb 07 '12 at 18:26

1 Answers1

-1

You probably need to get write locks on the pipe.

If process 1 and process 2 writes to the pipe simultaneously, you might get bytes mixed in the pipe such that the resulting read will be some strange number you've never seen.

Pochi
  • 2,026
  • 1
  • 15
  • 11
  • The pipe array gives me a feeling that he has separate pipes for each process. Just a hunch though. – Matti Virkkunen Feb 07 '12 at 18:05
  • in each process, I had two writes, one for each int I needed to write. I condensed it down to writing an int array rather than two separate writes, but now rather than having a huge value like before, it displays 1 – Seephor Feb 07 '12 at 18:06