LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2005, 11:10 AM   #1
MDBlueIce
Member
 
Registered: Aug 2004
Distribution: Suse Linux 9.1
Posts: 47

Rep: Reputation: 15
Coded FIFO issue


The behavior of the C program below is confusing me. It creates a FIFO, with permissions for the owner to read, write, and execute. It then opens the FIFO with an access mode flag of read only. It then attempts to read from the FIFO. Because no process has the pipe open for writing, I expect at this point for my program to return 0 on the read. This does not happen. The program hangs forever, waiting for info to read from the pipe. Is my expectation wrong or my code? The code appears below.


/* ====================Start Code====================*/
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>


#define FIFO_PERMS (S_IRWXU)
#define FIFO_FLAG (O_RDONLY)


int main ()
{
char t [10];
int fd;

if ((mkfifo("fifolsky", FIFO_PERMS) == -1) && (errno != EEXIST))
{
printf ("failed to create fifo\n");
return 1;
}

if ((fd = open ("fifolsky", FIFO_FLAG)) == -1)
{
printf ("failed to open fifo\n");
return 1;
}

if (read(fd, t, 10) == 0)
{
printf("could not read anything from fifo\n");
return 1;
}

}

/*===================End Code=======================*/
 
Old 07-15-2005, 01:16 PM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
It's more java I know but dabbling with C, anyway, read operates in a blocking fashion as far as I can tell, had a quick look at "man 2 read"

Edit: deleted nonsense bit of code
(waiting for the C people to slaughter me now )

Last edited by Looking_Lost; 07-15-2005 at 01:29 PM.
 
Old 07-15-2005, 01:44 PM   #3
MDBlueIce
Member
 
Registered: Aug 2004
Distribution: Suse Linux 9.1
Posts: 47

Original Poster
Rep: Reputation: 15
I learned the FIFOs, or more generally pipes, are special in that they block on reads unless there are no processes running which have them open for writing. If no processes have them open for writing, the read attempt returns 0, indicating end of file conditions.
 
Old 07-15-2005, 02:03 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I do realize that your question is about linux and the following information comes from a Solaris page, but since both of them are supposed to behave POSIX-ly with file handling, the information is likely still relevant. It also describes the situation you describe exactly.

Quote:
From Basic IO Functions: Sun Developer's Guide:

O_NONBLOCK or O_NDELAY

These flags affect future reads or writes to the file. If the file to be opened is a FIFO, then setting either O_NONBLOCK or O_NDELAY will cause a read-only open to return without delay; a write-only open will fail if no process currently has the FIFO open for reading. If both O_NONBLOCK and O_NDELAY are clear, then a read-only open will block until a process opens the FIFO for writing. Similarly, a write-only open of a FIFO will block until a process opens it for reading.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backing Up Coded DVD's in Linux coolbreeze7000 Debian 8 12-25-2006 03:11 AM
snmptrapd FIFO eccsamba Linux - Networking 2 11-24-2004 08:52 AM
mplayer [mpeg4 @ 0x861f2f0]vop not coded? GT_Onizuka Linux - Software 6 05-01-2004 10:07 AM
FIFO using pointers AquamaN Programming 2 03-21-2004 05:51 PM
Color Coded Vim podollb Linux - Software 5 10-16-2003 04:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration