00001 /* 00002 sio (used with feed) 00003 AUP2, Sec. 3.9.8 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 00023 /* 00024 FREEBSD seems to support AIO, even though it doesn't claim to with _POSIX_ASYNCHRONOUS_IO. We don't test that symbol so we can run this example on FREEBSD. DARWIN doesn't support it. 00025 */ 00026 #ifdef DARWIN 00027 int main(void) 00028 { 00029 printf("DARWIN doesn't support AIO.\n"); 00030 exit(EXIT_SUCCESS); 00031 } 00032 #else /* DARWIN */ 00033 #include <aio.h> 00034 /*[synchronous]*/ 00035 #define PATH "/aup/c3/datafile.txt" 00036 #define FREQ 8000 00037 00038 static void synchronous(void) 00039 { 00040 int fd, count = 0; 00041 ssize_t nread; 00042 char buf1[512], buf2[512]; 00043 00044 ec_neg1( fd = open(PATH, O_RDONLY) ) 00045 timestart(); 00046 while (true) { 00047 ec_neg1( nread = read(fd, buf1, sizeof(buf1)) ) 00048 if (nread == 0) 00049 break; 00050 if (count % FREQ == 0) 00051 ec_neg1( read(STDIN_FILENO, buf2, sizeof(buf2)) ) 00052 count++; 00053 } 00054 timestop("synchronous"); 00055 printf("read %d blocks\n", count); 00056 return; 00057 00058 EC_CLEANUP_BGN 00059 EC_FLUSH("synchronous") 00060 EC_CLEANUP_END 00061 } 00062 /*[asynchronous]*/ 00063 static void asynchronous(void) 00064 { 00065 int fd, count = 0; 00066 ssize_t nread; 00067 char buf1[512], buf2[512]; 00068 struct aiocb cb; 00069 const struct aiocb *list[1] = { &cb }; 00070 00071 memset(&cb, 0, sizeof(cb)); 00072 cb.aio_fildes = STDIN_FILENO; 00073 cb.aio_buf = buf2; 00074 cb.aio_nbytes = sizeof(buf2); 00075 cb.aio_sigevent.sigev_notify = SIGEV_NONE; 00076 ec_neg1( fd = open(PATH, O_RDONLY) ) 00077 timestart(); 00078 while (true) { 00079 ec_neg1( nread = read(fd, buf1, sizeof(buf1)) ) 00080 if (nread == 0) 00081 break; 00082 if (count % FREQ == 0) { 00083 if (count > 1) { 00084 ec_neg1( aio_suspend(list, 1, NULL) ) 00085 ec_rv( aio_error(&cb) ) 00086 } 00087 ec_neg1( aio_read(&cb) ) 00088 } 00089 count++; 00090 } 00091 timestop("asynchronous"); 00092 printf("read %d blocks\n", count); 00093 return; 00094 00095 EC_CLEANUP_BGN 00096 EC_FLUSH("asynchronous") 00097 EC_CLEANUP_END 00098 } 00099 /*[]*/ 00100 int main(void) 00101 { 00102 synchronous(); 00103 asynchronous(); 00104 exit(EXIT_SUCCESS); 00105 } 00106 00107 #endif /* DARWIN */