It is recommended that synchronous sg-based applications use the new
SG_IO ioctl() command. Existing applications (which are mainly synchronous)
can continue to use the older sg_header based interface which is still
supported.
Asynchronous usage allows multiple SCSI commands to be queued up to the
device. If the device supports command queuing then there can be a major
performance gain. Even if the device doesn't support command queuing (or
is temporarily busy) then queuing up commands in the mid level or the host
driver can be a minor performance win (since there will be a lower latency
to transmit the next command when the device becomes free).
Asynchronous usage usually starts with setting the O_NONBLOCK flag on open()
[or thereafter by using the fcntl(fd, SETFD, old_flags | O_NONBLOCK)
system call]. A similar effect can be obtained without using O_NONBLOCK
when POSIX threads are used. There are several strategies that can then
be followed:
set O_NONBLOCK and use a poll() loop
set O_NONBLOCK and use SIGPOLL signal to alert app when readable
use POSIX threads and a single sg file descriptor
use POSIX threads and multiple sg file descriptors to same device
The O_NONBLOCK flag also permits open(), write() and read() [but not
the ioctl(SG_IO)] to access a SCSI device even though it has been
marked offline. SCSI devices are marked offline when they are detected
and don't respond to the initial SCSI commands as expected, or,
some SCSI error condition is detected on that device and the
mid level error recovery logic is unable to "resurrect" the device.
A SCSI device that is being reset (and still settling) could be
accessed during this period by using the O_NONBLOCK flag; this
could lead to unexpected behaviour so the sg user should take care.
In Linux SIGIO and SIGPOLL are the same signal. If POSIX real time
signals are used (e.g. when SA_SIGINFO is used with sigaction() and
fcntl(fd, F_SETSIG, SIGRTMIN + <n>) ) then the file descriptor with which
the signal is associated is available to the signal handler. The
associated file descriptor is in the si_fd member of the siginfo_t
structure. The poll() system call that is often used after a signal
is received can thus be bypassed.