Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1505
  • Last Modified:

Problem with read() function

Hi guys!



I have 5 processes connected through pipes in a ring structure (stdout of process #1 is stdin of process #2... and so on). I have to send integers through these pipes, and all the five process are the same executable. (I'm using Ansi C in Linux)

The problem is that my "sender process" send successfully the integer values, and the 2nd process receives them, but when it gets the last value, the process hangs, and I don't know exactly why...

As far as I've understood in the documentation, when read's result = 0, then it has finished reading, so it should exit the while loop, but it's doing nothing : when it gets the last value, the system call "read" hangs everything....

Am I doing anything wrong? Communication through pipes work in a different way?

Thanks in advance!
while ((result = read(STDIN_FILENO, &value, sizeof(int))) > 0)
{					
   // DO THINGS....
}

Open in new window

0
kArLiNhoS
Asked:
kArLiNhoS
  • 2
  • 2
1 Solution
 
Infinity08Commented:
From the reference page :

"If some process has the pipe open for writing and O_NONBLOCK is clear, read() shall block the calling thread until some data is written or the pipe is closed by all processes that had the pipe open for writing."
0
 
kArLiNhoSAuthor Commented:
Hi Infinity08, thanks for your reply.

The main process redirects its stdout to the stdin of the first child process, and so on... I'm not calling the Open function anywhere, so I suppose that I can't use the O_NONBLOCK, so I don't know if that is applicable to my problem...

0
 
Infinity08Commented:
As long as the pipe is open, the read won't return until a value is written on the pipe. So, either close the pipe, or send a terminator value and stop reading once you receive it.
0
 
kArLiNhoSAuthor Commented:
Perhaps is the best choice Intinify, thank you very much :-)
0
 
evilrixSenior Software Engineer (Avast)Commented:
I'm just wondering if you could use aio_read() to do this...

http://linux.die.net/man/3/aio_read

I've never used it myself so you'll need to take a look for yourself.

Alternatively, maybe you could clarify what your objective is as there may be a smarter way to achieve it.

-Rx.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now