Solved

Problem with read() function

Posted on 2007-11-18
5
1,493 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perhaps is the best choice Intinify, thank you very much :-)
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now