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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.