Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

516 Advanced I/O Chapter 14


We can determine the value ofAIO_LISTIO_MAXby calling thesysconffunction
with thenameargument set to_SC_IO_LISTIO_MAX.Similarly, we can determine the
value ofAIO_MAXby callingsysconfwith thenameargument set to_SC_AIO_MAX,
and we can get the value ofAIO_PRIO_DELTA_MAXby callingsysconf with its
argument set to_SC_AIO_PRIO_DELTA_MAX.
The POSIX asynchronous I/O interfaces wereoriginally introduced to provide real-
time applications with a way to avoid being blocked while performing I/O operations.
Now we’ll look at an example of how to use the interfaces.

Example


We don’t discuss real-time programming in this text, but because the POSIX
asynchronous I/O interfaces arenow part of the base specification in the Single UNIX
Specification, we’ll look at how to use them. To comparethe asynchronous I/O
interfaces with their conventional counterparts, we’ll look at the task of translating a file
from one format to another.
The program shown in Figure14.20 translates a file using the ROT-13 algorithm
that the USENET news system, popular in the 1980s, used to obscuretext that might be
offensive or contain spoilers or joke punchlines. The algorithm rotates the characters ’a’
to ’z’ and ’A’ to ’Z’ by 13 positions, but leaves all other characters unchanged.
#include "apue.h"
#include <ctype.h>
#include <fcntl.h>

#define BSZ 4096

unsigned char buf[BSZ];

unsigned char
translate(unsigned char c)
{
if (isalpha(c)) {
if (c >= ’n’)
c -= 13;
else if (c >= ’a’)
c += 13;
else if (c >= ’N’)
c -= 13;
else
c += 13;
}
return(c);
}

int
main(int argc, char* argv[])
{
int ifd, ofd, i, n, nw;
Free download pdf