Link to home
Start Free TrialLog in
Avatar of kArLiNhoS
kArLiNhoSFlag for Spain

asked on

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

Avatar of Infinity08
Infinity08
Flag of Belgium image

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."
Avatar of kArLiNhoS

ASKER

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...

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perhaps is the best choice Intinify, thank you very much :-)
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.