Solved

Problem with read() function

Posted on 2007-11-18
5
1,498 Views
Last Modified: 2008-02-01
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
Comment
Question by:kArLiNhoS
  • 2
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20307864
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
 

Author Comment

by:kArLiNhoS
ID: 20308063
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 150 total points
ID: 20308117
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
 

Author Comment

by:kArLiNhoS
ID: 20308230
Perhaps is the best choice Intinify, thank you very much :-)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20308249
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question