tags:

views:

43

answers:

1

Hi,

I am having a problem in my program that uses pipes.

What I am doing is using pipes along with fork/exec to send data to another process

What I have is something like this:

//pipes are created up here

if(fork() == 0) //child process
{
  ...
  execlp(...);
}
else
{
  ...
  fprintf(stderr, "Writing to pipe now\n");
  write(pipe, buffer, BUFFER_SIZE);
  fprintf(stderr, "Wrote to pipe!");
  ...
}

This works fine for most messages, but when the message is very large, the write into the pipe deadlocks.

I think the pipe might be full, but I do not know how to clear it. I tried using fsync but that didn't work.

Can anyone help me?

+3  A: 

You need to close the read end of the pipe in the process doing the writing. The OS will keep data written to the pipe in the pipe's buffer until all processes that have the read end of the pipe open actually read what's there.

Paul Kuliniewicz
This fixed it, thanks, I wasn't closing the pipe until after I finished writing to it
avs3323