Solved

Problem with read() function

Posted on 2007-11-18
5
1,499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

751 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