开发者

Write to pipe deadlocking program

开发者 https://www.devze.com 2022-12-30 12:09 出处:网络
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

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号