ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a fifo file, where another process wrote some data. I want to read this data from this file in bash script I'm trying to write.
However, all calls like "cat", "more", "tail" or "cp" are blocking and waiting forever. (because as I understand it, FIFO file "has no end").
Any suggestion, how to solve this? (It is clear to me, that I could write some piece of code myself, but I'm wondering if there is already some useful utility I could use)
FIFO does have and end of file. The problem would be if you would be reading data from that file and the other process would be still writing it. Then the end of file is "on the move". And as the other process is writing it it moves the end of file line by line down and therefor you can't reach the end.
This would be the same as if you would run
cat /dev/urandom > /some/file
and try to cat /some/file to the screen.
You would never reach the end untill you stoped cat /dev/urandom > /some/file
The read process will block until the writing process closes its descriptor. That indicates the end of the data, or "EOF". When piping in a script without using "|" you generally have to set up the read operation in the background (e.g. cat mypipe &,) execute the write operation (e.g. ls -l > mypipe,) and then wait to make sure the background process is done before continuing.
IO blocking is a state set at the file descriptor level. In a C program you would change the default blocking to non-blocking with fcntl(file_descriptor, F_SETFL, O_NONBLOCK);. That's necessary for a lot of server-type programs. If you really need a version of cat that doesn't block, you could write one in just a few lines. Is it a time limit you are concerned with? Do you want it to only wait X number of seconds when no input is given?
Here is my non-blocking version of cat. You give it a time in seconds to wait between valid inputs and if it times out then it will switch files until it runs out of them. Yes, I think I can solve all problems by throwing C code at them
ta0kira
I did have that header in when I added the function, but then I looked at it when I was done and thought, "why is THAT in there?" After a quick look, I didn't see anything that needed it so I took it out, but it still managed to compile alright.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.