LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
Thread Tools
Old 05-23-2008, 10:28 AM   #1
Sathya
Member
 
Registered: Sep 2007
Posts: 33
Thanked: 0
Regarding behaviour of FIFO


[Log in to get rid of this advertisement]
I have the following problem with FIFO.

A process,

opens fifo in blocking mode for reading.
In a for loop
{
waits in select for 60 secs
if select returns > 0
Reads the data from fifo
On timer expiry continue with the for loop
}

When the select() looks for activity in the FIFO within 60 secs, suppose i remove the fifo manually, then the select does not recognise this situation and just returns 0. If this is the case, how can the code ever recognise that the FIFO is gone?

I removed the select logic, and just had the fread() within the loop. In this case also, after removing FIFO, fread() does not recognise that FIFO is gone and instead blocks permanently.

I tried the same scenario with a FILE instead of FIFO. A process opens a file in read mode, sleeps(20) secs, and then writes some string to file. When it sleeps for 20 sec, i manually deleted the file and after the process wakes up fread() fails with error "Bad File Descriptor".

Why is this not happening in case of FIFO. Why does select or fread not recoginise that FIFO is gone and set some errno appropriately.

Please clarify.

Thanks,
Sathya
Sathya is offline     Reply With Quote
Old 05-23-2008, 11:30 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
** accidental double post. please ignore **

Last edited by Hko; 05-23-2008 at 11:32 AM..
Hko is offline     Reply With Quote
Old 05-23-2008, 11:32 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
What happens is: when the fifo deleted while it still held open by one or more process(es), the fifo does remain to exist! You cannot open it again, since it has no name in the filesystem anymore, but it is still there (like a normal pipe) and used by the process(es) as if nothing happened.

Verify this by opening 3 terminals.
  • In the first make a fifo: mkfifo /tmp/fifo. Then execute od /dev/urandom >/tmp/fifo (this writes random digits to the fifo contineously).
  • In the second terminal run cat /tmp/fifo to read the digits from the fifo.
  • In the third terminal delete /tmp/fifo. You will notice how both the reading and the writing process keep running.
If you now kill the writing process, the reading process will still have a valid fifo open: no error occurs.

In your case the process writing to the fifo probably opens and closes the fifo after each string written. The reading process still has a valid (though useless) fifo open and will wait for more.

The same should happen with normal files: when a process has a file open for writing when it is deleted, it will keep it open without an error and will still be able to the file (which nobody will ever see again). Why this did not happen when you tried is a mistery to me. Maybe when using the append flag or so has this effect, or you program has some bug. Hard to say without the code.

Last edited by Hko; 05-23-2008 at 11:34 AM..
Hko is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Linux 2.6 FIFO vs Linux 2.4 FIFO gjpc Programming 4 03-19-2008 08:26 PM
fifo help !! matlin Linux - Newbie 1 02-21-2008 02:51 PM
snmptrapd FIFO eccsamba Linux - Networking 2 11-24-2004 09:52 AM
FIFO question gauge73 Linux - Newbie 1 03-01-2003 12:39 AM
nfs FIFO??? granny Linux - Newbie 1 12-16-2002 04:38 PM


All times are GMT -5. The time now is 10:38 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration