34

I have this code for reading from Serial in Linux , but i don't know what is the difference between blocking and non blocking in reading Serial Port and which one is better in which situation?

Mohsen Zahraee
  • 3,309
  • 5
  • 31
  • 45
  • 6
    It is fully depends on you application's architecture. Blocking is simpler, but blocking. Non-blocking needs slightly more coding, but gives you possibility to do another task in the same time. – Геннадий Казачёк Sep 23 '14 at 13:26

1 Answers1

84

The code you mention is IMO poorly coded and commented. That code does not conform to POSIX practices for portability as described in Setting Terminal Modes Properly and Serial Programming Guide for POSIX Operating Systems. That code does not mention that it uses non-canonical (aka raw) mode, and reuses the "blocking" and "nonblocking" terminology to describe the VMIN and VTIME attributes.

(The author of that code reports that it predates the POSIX standard, and hence its non-compliance. That is understandable, but to then post and advocate the use of old code that may not be portable (i.e. function as expected in an alternate situation) is questionable.)


The conventional definition of a "blocking" versus "nonblocking" read is based on "when" the read call will return to your program (and resume execute with the next statement) and whether there will be data stored in your program's read buffer. A blocking read is the default mode, unless non-blocking is requested by opening the serial terminal with the O_NONBLOCK or O_NDELAY flag.

Canonical mode
For a blocking canonical read call of a serial terminal, a line (aka record) of text will always be returned in the provided buffer (unless an error occurred). The read call will block (i.e. suspend execution of your program) for as long as it takes for a line termination character to be received and processed.

A nonblocking canonical read call of a serial terminal will always return "immediately". The read may or may not return any data.
If (since the previous read call) at least a line of text has been received and stored in the system buffer, then the oldest line will be removed from the system buffer and copied to the program's buffer. The return code will indicate the data length.
If (since the previous read call) a line-termination character has not been received and processed, then there is no (complete) line of text available. The read() will return an EAGAIN error (i.e. a -1 return code and errno set to EAGAIN). Your program can then perform some calculation, or request I/O from another device, or delay/sleep. Either after an arbitrary delay or by notification by poll() or select(), your program can retry the read().

An example program using blocking canonical mode for reads is included in this answer.

Non-canonical mode
When the serial terminal is configured for non-canonical mode, the termios c_cc array elements VMIN and VTIME should be used to control "blocking", but this requires that the terminal be opened in the default blocking mode, i.e. do not specify the O_NONBLOCK open flag.
Otherwise O_NONBLOCK will have precedence over the VMIN and VTIME specification, and read() will set errno to EAGAIN and immediately return -1 instead of 0 when there is no data available. (This is the behavior observed in recent Linux 3.x kernels; older 2.6.x kernels may behave differently.)

The termios man page describes (c_cc array index) VMIN as the "minimum number of characters for noncanonical read", and (c_cc array index) VTIME as the "timeout in deciseconds for noncanonical read".
VMIN should be adjusted by your program to accommodate the typical message or datagram length that is expected and/or the minimum size for data to retrieve & process per read().
VTIME should be adjusted by your program to accommodate the typical burstiness or arrival rate of serial data that is expected and/or the maximum time to wait for data or a datum.

The VMIN and VTIME values interact to determine the criterion for when read should return; their precise meanings depend on which of them are nonzero. There are four possible cases.
This web page explains it as:

  • VMIN = 0 and VTIME = 0

This is a completely non-blocking read - the call is satisfied immediately directly from the driver's input queue. If data are available, it's transferred to the caller's buffer up to nbytes and returned. Otherwise zero is immediately returned to indicate "no data". We'll note that this is "polling" of the serial port, and it's almost always a bad idea. If done repeatedly, it can consume enormous amounts of processor time and is highly inefficient. Don't use this mode unless you really, really know what you're doing.

  • VMIN = 0 and VTIME > 0

This is a pure timed read. If data are available in the input queue, it's transferred to the caller's buffer up to a maximum of nbytes, and returned immediately to the caller. Otherwise the driver blocks until data arrives, or when VTIME tenths expire from the start of the call. If the timer expires without data, zero is returned. A single byte is sufficient to satisfy this read call, but if more is available in the input queue, it's returned to the caller. Note that this is an overall timer, not an intercharacter one.

  • VMIN > 0 and VTIME > 0

A read() is satisfied when either VMIN characters have been transferred to the caller's buffer, or when VTIME tenths expire between characters. Since this timer is not started until the first character arrives, this call can block indefinitely if the serial line is idle. This is the most common mode of operation, and we consider VTIME to be an intercharacter timeout, not an overall one. This call should never return zero bytes read.

  • VMIN > 0 and VTIME = 0

This is a counted read that is satisfied only when at least VMIN characters have been transferred to the caller's buffer - there is no timing component involved. This read can be satisfied from the driver's input queue (where the call could return immediately), or by waiting for new data to arrive: in this respect the call could block indefinitely. We believe that it's undefined behavior if nbytes is less then VMIN.

Note when VMIN=1 that the VTIME specification will be irrelevant. The availability of any data will always satisfy the minimum criterion of a single byte, so the time criterion can be ignored (since it would be an intercharacter time specification with a nonzero VMIN). This special case was pointed out by @IanAbbot.


That code you mention configures "nonblocking" mode as VMIN=0 and VTIME=5. This will not cause the read() to return immediately like a nonblocking canonical read would; with that code a read() should always wait at least a half second before returning.
The conventional definition of a "nonblocking" is that your calling program is not preempted during the syscall and gets control back (almost) immediately.
To get an (unconditional and) immediate return (for a non-canonical read), set VMIN=0 and VTIME=0 (with the attendant warnings).

sawdust
  • 16,103
  • 3
  • 40
  • 50
  • 4
    Good explanation, but wow. That honestly is a pretty messy contract, compared to [what Windows does](http://msdn.microsoft.com/en-us/library/windows/desktop/aa363190%28v=vs.85%29.aspx) – Ben Voigt Sep 24 '14 at 01:20
  • 15
    That code pre-dates POSIX by several years, so it is no surprise that it doesn't match. – wallyk Feb 18 '15 at 01:00
  • If `O_NONBLOCK` is not to be specified in non-canonical mode, what does specifying `O_NONBLOCK` do to canonical mode? – CMCDragonkai Jan 21 '17 at 01:05
  • 3
    @CMCDragonkai -- The long paragraph that begins with *"A nonblocking canonical read..."* answers your question. – sawdust Jan 21 '17 at 03:05
  • 1
    @wallyk *"That code pre-dates POSIX by several years"* -- How old is that code? [Serial Programming Guide for POSIX Operating Systems](http://www.cmrr.umn.edu/~strupp/serial.html) was first copyrighted in 1994. – sawdust Jan 21 '17 at 03:08
  • 5
    @sawdust: That code was based on code for Unix from about 1985. The code I provided was last substantively updated about 1992 with various tweaks since to support Solaris, Linux, and some derivatives of Unix. – wallyk Jan 21 '17 at 06:36
  • 1
    Does non-blocking canonical mode really exists? Answer only points to canonical blocking examples... – 71GA Apr 01 '21 at 10:17
  • @71GA -- Yes, you can configure non-blocking canonical mode. The long paragraph in the answer that begins with *"A nonblocking canonical read..."* describes how it would perform. There's even a comment & my response above on this exact subject. There is no example because why would you want to use non-blocking canonical mode? – sawdust Apr 01 '21 at 22:31
  • You do not mention it explicitly but would using non-blocking canonical mode not also be a candidate of consuming much processor power? – grenix Dec 01 '21 at 17:08
  • @grenix -- Yes, either canonical or raw mode with nonblocking syscalls can become a CPU hog depending on the method for issuing read()s. – sawdust Dec 02 '21 at 04:19
  • @sawdust, I get the rhetoric, but why would you _not_ use non-blocking canonical mode? Expand on that..? – fde-capu May 10 '22 at 10:56
  • 1
    @fde-capu -- You would "*not use non-blocking canonical mode*" in order to utilize the efficient, event-driven scheduling built into the OS. The canonical read is waiting for an EOL character. The OS is efficient at performing such a wait. The user program that chooses non-blocking mode should have special requirements (e.g. a timeout, no threads, and/or needs to perform simultaneous processing) and careful implementation to avoid inefficient polling of the termios buffer. The reason for using non-blocking reads should not be that you think it is "faster" (since it's not). – sawdust May 11 '22 at 03:26