/* Program 5.5 */ #include #include #include #include #include #include #include #include #include #include #define BLKSIZE 1024 volatile char buf2[BLKSIZE]; volatile char buf1[BLKSIZE]; int fd_1 = 0; int fd_2 = 0; int fd1_error = 0; int fd2_error = 0; struct aiocb my_aiocb1; struct aiocb my_aiocb2; void my_aio_handler(int signo, siginfo_t *info, void *context) { int my_errno; int my_status; struct aiocb *my_aiocbp; int *errorp; my_aiocbp = info->si_value.sival_ptr; if (signo == SIGRTMAX) errorp = &fd1_error; else errorp = &fd2_error; if ((my_errno = aio_error(my_aiocbp)) != EINPROGRESS) { my_status = aio_return(my_aiocbp); if (my_status >= 0) { write(STDERR_FILENO, (char *)my_aiocbp->aio_buf, my_status); *errorp = aio_read(my_aiocbp); } else *errorp = 1; } } void main(int argc, char *argv[]) { sigset_t oldmask; struct sigaction newact; /* open the file descriptors for I/O */ if (argc != 3) { fprintf(stderr, "Usage: %s filename1 filename2\n", argv[0]); exit(1); } if ((fd_1 = open(argv[1], O_RDONLY)) == -1) { fprintf(stderr,"Could not open %s: %s\n", argv[1], strerror(errno)); exit(1); } if ((fd_2 = open(argv[2], O_RDONLY)) == -1) { fprintf(stderr,"Could not open %s: %s\n", argv[2], strerror(errno)); exit(1); } /* Set up handlers for SIGRTMAX and SIGRTMAX-1 */ sigemptyset(&newact.sa_mask); sigaddset(&newact.sa_mask, SIGRTMAX); sigaddset(&newact.sa_mask, SIGRTMAX-1); if (sigprocmask(SIG_BLOCK, &newact.sa_mask, &oldmask) == -1) { perror("Could not block SIGRTMAX or SIGRTMAX-1"); exit(1); } newact.sa_sigaction = my_aio_handler; newact.sa_flags = SA_SIGINFO; if (sigaction(SIGRTMAX, &newact, NULL) == -1) { perror("Could not set SIGRTMAX handler"); exit(1); } if (sigaction(SIGRTMAX-1, &newact, NULL) == -1) { perror("Could not set SIGRTMAX-1 handler"); exit(1); } /* Unblock the signals */ if (sigprocmask(SIG_UNBLOCK, &newact.sa_mask, NULL) == -1) { perror("Could not unblock SIGRTMAX or SIGRTMAX-1"); exit(1); } /* Start first I/O operation on fd_1 */ my_aiocb1.aio_fildes = fd_1; my_aiocb1.aio_offset = 0; my_aiocb1.aio_buf = (void *)buf1; my_aiocb1.aio_nbytes = BLKSIZE; my_aiocb1.aio_sigevent.sigev_notify = SIGEV_SIGNAL; my_aiocb1.aio_sigevent.sigev_signo = SIGRTMAX; my_aiocb1.aio_sigevent.sigev_value.sival_ptr = &my_aiocb1; fd1_error = aio_read(&my_aiocb1); if (fd1_error == -1) { if (errno == ENOSYS) fprintf(stderr," !!!!! Not supported yet\n"); else perror("The aio_read failed"); exit(1); } /* Start first I/O operation on fd_2 */ my_aiocb2.aio_fildes = fd_2; my_aiocb2.aio_offset = 0; my_aiocb2.aio_buf = (void *)buf2; my_aiocb2.aio_nbytes = BLKSIZE; my_aiocb2.aio_sigevent.sigev_notify = SIGEV_SIGNAL; my_aiocb2.aio_sigevent.sigev_signo = SIGRTMAX-1; my_aiocb2.aio_sigevent.sigev_value.sival_ptr = &my_aiocb2; fd2_error = aio_read(&my_aiocb2); if (fd1_error == -1) { perror("The aio_read failed"); exit(1); } /* proceed with overlapping computations */ while(!fd1_error || !fd2_error) /* do whatever */ ; exit(0); }