LinuxQuestions.org
Review your favorite Linux distribution.
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 05-23-2008, 09:28 AM   #1
Sathya
Member
 
Registered: Sep 2007
Posts: 33

Rep: Reputation: 15
Regarding behaviour of FIFO


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
 
Old 05-23-2008, 10:30 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
** accidental double post. please ignore **

Last edited by Hko; 05-23-2008 at 10:32 AM.
 
Old 05-23-2008, 10:32 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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 10:34 AM.
 
  


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

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

All times are GMT -5. The time now is 12:49 AM.

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