Re: Problem to read on a serial port

Jens.Toerring_at_physik.fu-berlin.de
Date: 03/31/05


Date: 31 Mar 2005 13:28:54 GMT

Geoff Clare <geoff@clare.see-my-signature.invalid> wrote:
> Jens.Toerring@physik.fu-berlin.de wrote, on Thu, 31 Mar 2005:

>> What do you think about the
>>
>> fcntl(fd, F_SETFL, 0);
>>
>> Wouldn't that also clear out other flags than O_NDELAY, which
>> might have unintended effects (at least I don't know if there's
>> a consensus between systems which flags exist at all and which
>> get influenced by fcntl())? So, shouldn't one use e.g.
>>
>> fcntl(fd, F_SETFL, fcntl( fd, F_GET_FL ) & ~ O_NDELAY );
>>
>> to explicitely only reset the non-delay flag?

> In the general case the get-and-set method should always be used.
> According to POSIX fcntl(fd, F_SETFL, 0) would turn off O_APPEND,
> O_NONBLOCK, O_SYNC, O_DSYNC and O_RSYNC if any of them were set.
> Implementations could also have additional non-standard flags for
> use with fcntl F_[GS]ETFL (such as O_NDELAY) which would be
> turned off if they were set.

> However, if you know for sure that other flags were not set when
> the fd was opened, and have not been set since, then
> fcntl(fd, F_SETFL, 0) should work fine to turn off only the
> flags you know are set.

>> Also, I didn't find
>> 'FNDELAY' in POSIX, so wouldn't it be better to use O_NDELAY? Or
>> is there some magic involved with the use of 'FNDELAY'?

> Neither FNDELAY nor O_NDELAY are in POSIX. It is best to use
> O_NONBLOCK.

>> Another thing I am not sure about is what read() is supposed to
>> return in non-blocking mode when no data are available. Is it
>> really true that read() should return 0 in that case and not -1
>> with errno being set to EAGAIN?

> The zero return is the historical behaviour with O_NDELAY, and the
> fact that it is broken-by-design (because it can't be distinguished
> from EOF) is the reason POSIX invented O_NONBLOCK to replace O_NDELAY.
> When O_NONBLOCK is used read() must indicate that no data are
> available by returning -1 and setting errno to EAGAIN.

Thanks for the clarification! I actually always was wondering WTF
the difference between O_NDELAY and O_NONBLOCK is supposed to be
and started to think of them as the same thing. So you may have
spared me some future trouble;-)

                                   Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages

  • Re: Repost: Bug with select?
    ... On Jul 25, Marco Roeland wrote: ... > A select with no timeout, ... > with errno set to EAGAIN, meaning you should try again, which is the ... int flags, fd, len; fd_set writefds; ...
    (Linux-Kernel)
  • Re: 2.6.11-rc3-bk5: XFS: fcron: could not write() buf to disk: Resource temporarily unavailable
    ... >> let you know once I have a fix or have found the culprit change. ... Turns out it was actually XFS giving back this EAGAIN, ... propogate more sync write errors out to userspace. ... VOP_IFLUSH(vp, flags, error); ...
    (Linux-Kernel)
  • Re: Problem to read on a serial port
    ... > a consensus between systems which flags exist at all and which ... According to POSIX fcntlwould turn off O_APPEND, ... > 'FNDELAY' in POSIX, so wouldn't it be better to use O_NDELAY? ... available by returning -1 and setting errno to EAGAIN. ...
    (comp.unix.programmer)