Advanced Programming in UNIX Environment Episode 79

To determine the completion status of an asynchronous read, write, or synch operation, we need to call the aio_error function.

#include 
int aio_error(const struct aiocb *aiocb);

We use asynchronous I/O when we have other processing to do and we don’t want to block while performing the I/O operation. However, when we have completed the processing and find that we still have asynchronous operations outstanding, we can call the aio_suspend function to block until an operation completes.

#include 
int aio_suspend(const struct aiocb *const list[], int nent,
const struct timespec *timeout);

When we have pending asynchronous I/O operations that we no longer want to complete, we can attempt to cancel them with the aio_cancel function.

#include 
int aio_cancel(int fd, struct aiocb *aiocb);

One additional function is included with the asynchronous I/O interfaces, although it can be used in either a synchronous or an asynchronous manner. The lio_listio function submits a set of I/O requests described by a list of AIO control blocks.

#include 
int lio_listio(int mode, struct aiocb *restrict const list[restrict],
int nent, struct sigevent *restrict sigev);
#include "apue.h"
#include 
#include 

#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;

    if(argc!=3)
        err_quit("usage: rot13 infile outfile");
    if((ifd=open(argv[1], O_RDONLY))<0)
        err_sys("can't open %s",argv[1]);
    if((ofd=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,FILE_MODE))<0)
        err_sys("can't creat %s", argv[2]);

    while((n=read(ifd, buf, BSZ))<0)
    {
        for(i=0;i

Translate a file using ROT-13

你可能感兴趣的:(Advanced,Programming,in,the,Unix,Environment)