Unix Programming - block in open()

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2006 > block in open()





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author block in open()
freegnu

2006-10-24, 7:21 am

hi all, it strange, when i use open() to open a FIFO to write, but i find when i use
open(Fifo,O_WRONLY | O_CREAT | O_TRUNC);
it will block

i really confused
Nils O. Selåsdal

2006-10-24, 7:21 am

freegnu wrote:
> hi all, it strange, when i use open() to open a FIFO to write, but i
> find when i use
> open(Fifo,O_WRONLY | O_CREAT | O_TRUNC);
> it will block


Opening a fifo blocks until someone opens if for reading.
phil-news-nospam@ipal.net

2006-10-24, 7:17 pm

On Tue, 24 Oct 2006 13:53:02 +0200 "Nils O. Sel?sdal" <NOS@utel.no> wrote:

| freegnu wrote:
|> hi all, it strange, when i use open() to open a FIFO to write, but i
|> find when i use
|> open(Fifo,O_WRONLY | O_CREAT | O_TRUNC);
|> it will block
|
| Opening a fifo blocks until someone opens if for reading.

So how to make a program that can listen on more than one fifo? EAGAIN?

--
|---------------------------------------/----------------------------------|
| Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
| first name lower case at ipal.net / spamtrap-2006-10-24-1312@ipal.net |
|------------------------------------/-------------------------------------|
William Ahern

2006-10-24, 7:17 pm

On Tue, 24 Oct 2006 18:13:07 +0000, phil-news-nospam wrote:

> On Tue, 24 Oct 2006 13:53:02 +0200 "Nils O. Sel?sdal" <NOS@utel.no> wrote:
>
> | freegnu wrote:
> |> hi all, it strange, when i use open() to open a FIFO to write, but i
> |> find when i use
> |> open(Fifo,O_WRONLY | O_CREAT | O_TRUNC); it will block


O_CREAT and O_TRUNC are both nonsensical and dangerous to use with named
pipes.

> | Opening a fifo blocks until someone opens if for reading.
>
> So how to make a program that can listen on more than one fifo? EAGAIN?


open(Fifo, O_WRONLY | O_NONBLOCK);

Note that in Linux O_NONBLOCK only makes the open non-blocking, but I've
recently learned that in BSD it also makes all subsequent I/O operations
non-blocking.


Nils O. Selåsdal

2006-10-25, 1:33 am

phil-news-nospam@ipal.net wrote:
> On Tue, 24 Oct 2006 13:53:02 +0200 "Nils O. Sel?sdal" <NOS@utel.no> wrote:
>
> | freegnu wrote:
> |> hi all, it strange, when i use open() to open a FIFO to write, but i
> |> find when i use
> |> open(Fifo,O_WRONLY | O_CREAT | O_TRUNC);
> |> it will block
> |
> | Opening a fifo blocks until someone opens if for reading.
>
> So how to make a program that can listen on more than one fifo? EAGAIN?


Open it non-blocking, use select or poll.
Ralf Fassel

2006-10-25, 7:18 am

* William Ahern <william@25thandClement.com>
| open(Fifo, O_WRONLY | O_NONBLOCK);
|
| Note that in Linux O_NONBLOCK only makes the open non-blocking, but
| I've recently learned that in BSD it also makes all subsequent I/O
| operations non-blocking.

Hmm. My open(2) manpages says:

O_NONBLOCK or O_NDELAY
When possible, the file is opened in non-blocking
mode. Neither the open() nor any subsequent operations
on the file descriptor which is returned will cause the
calling process to wait. ...

ERRORS
...
ENXIO
O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and
no process has the file open for reading. ...

man fifo(4):
...
A process can open a FIFO in non-blocking mode. In this case,
opening for read only will succeed even if noone has opened on
the write side yet; opening for write only will fail with ENXIO
(no such device or address) unless the other end has already
been opened.

Under Linux, opening a FIFO for read and write will succeed
both in blocking and non-blocking mode. POSIX leaves this
behaviour undefined. This can be used to open a FIFO for
writing while there are no readers available. ...

So this seems highly system-dependent. :-/

R'
Kaz Kylheku

2006-10-26, 1:31 am

phil-news-nospam@ipal.net wrote:
> | Opening a fifo blocks until someone opens if for reading.
>
> So how to make a program that can listen on more than one fifo? EAGAIN?


You use the O_NDELAY flag when opening the fifo to prevent the blocking
behavior. If no reader is waiting, it will fail. If opening for reading
with O_NDELAY, it will succeed even if there is no writer.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com