C- Read data from PIPE

margarit
margarit used Ask the Experts™
on
Hello,

I am using the following code to read from 2 PIPEs:
PIPE, PIPE_HTTP_REFERER.
I have the following problem:
When the function runs and there is no data in PIPE, the program stucks in read() function until it reads data from PIPE.
Should I add O_NONBLOCK option? If yes, how?

THANKS
Margarit
int main(int argn, char * argv[])
{
       int PIPE, PIPE_HTTP_REFERER;
       char PIPE_buffer[500];
       char PIPE_HTTP_buffer[500];
       int PIPE_size=0, PIPE_HTTP_size=0;
        PIPE = open("../web/cgi-bin/PIPE",O_RDWR);
        PIPE_HTTP_REFERER = open("../web/cgi-bin/PIPE_HTTP_REFERER",O_RDWR);
	
        if(PIPE == 0)
        {
                printf("\n ERROR open file PIPE\n");
        }
        if(PIPE_HTTP_REFERER == 0)
       {
                printf("\n ERROR open file PIPE_HTTP_REFERER \n");
        }
                 while(1)
                 {
		PIPE_size=read(PIPE,PIPE_buffer,500);
		PIPE_HTTP_size=read(PIPE_HTTP_REFERER,PIPE_HTTP_buffer,500);
		printf("\n size of PIPE buffer = %i ", PIPE_size);
		printf("\n size of PIPE HTTP buffer = %i ", PIPE_HTTP_size);
		if(PIPE_size > 0)
		{
			printf("\n PIPE buffer = %s",PIPE_buffer);
		}
		if (PIPE_HTTP_size > 0)
		{
			printf("PIPE HTTP buffer = %s",PIPE_HTTP_buffer);
		}    
	}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009
Commented:
On a POSIX compliant system, like Linux, you can do something like the following to make the stream non-blocking :
#include <unistd.h>
#include <fcntl.h>
 
int flags = fcntl(fd, F_GETFL, 0);           /* where fd is your file descriptor */
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
 
/* note : check the return value of the fcntl calls */
 
/* note 2 : you could also pass O_NONBLOCK as flag to 'open' directly */

Open in new window

Author

Commented:
Hello,
THANKS for the fast reply. I did the following:
PIPE = open("../web/cgi-bin/PIPE",O_RDWR | O_NONBLOCK);
And it seems to work OK. Is it the correct way?
And I put attantion for another problem:
When I put data to PIPE using: echo aaaa>PIPE. I succeed reading data using my function. But If I send data using "submit" option on my form I dont succed to read data using my function (if I use cat > PIPE, I do see my data). What can be a reason?
THANKS
 
 
Top Expert 2009

Commented:
>> And it seems to work OK. Is it the correct way?

Assuming your system supports O_NONBLOCK correctly (ie. is POSIX compliant), that's perfectly ok.

Do check the man page for open to get a better understanding of what's involved with using O_NONBLOCK.


>> But If I send data using "submit" option on my form

What kind of form . What does the submit do ? In any case, this is probably better posted as a separate question in the appropriate zones, to be sure that the right experts have a look at it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial