00001 /* 00002 Synchronized I/O test program 00003 AUP2, Sec. 2.16.2 (not in book) 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 #define NUM_WRITES 250 00024 00025 int main(void) 00026 { 00027 int try, fd, i; 00028 char buf[512], *caption; 00029 00030 #ifdef _POSIX_SYNCHRONIZED_IO 00031 printf("_POSIX_SYNCHRONIZED_IO is defined\n"); 00032 #else 00033 printf("_POSIX_SYNCHRONIZED_IO is undefined\n"); 00034 #endif 00035 00036 memset(buf, 0, sizeof(buf)); 00037 00038 for (try = 0; try < 4; try++) { 00039 if ((fd = creat("a.tmp", PERM_FILE)) == -1) 00040 syserr("creat"); 00041 timestart(); 00042 for (i = 0; i < NUM_WRITES; i++) { 00043 if (write(fd, buf, sizeof(buf)) == -1) 00044 syserr("write"); 00045 switch (try) { 00046 case 0: 00047 caption = "normal"; 00048 break; 00049 case 1: 00050 sync(); 00051 caption = "sync"; 00052 break; 00053 case 2: 00054 #if defined(_POSIX_SYNCHRONIZED_IO) || defined(FREEBSD) 00055 fsync(fd); 00056 caption = "fsync"; 00057 #else 00058 sync(); 00059 caption = "fsync not defined; using sync"; 00060 #endif 00061 break; 00062 case 3: 00063 #if defined(_POSIX_SYNCHRONIZED_IO) 00064 fdatasync(fd); 00065 caption = "fdatasync"; 00066 #else 00067 sync(); 00068 caption = "fdatasync not defined; using sync"; 00069 #endif 00070 } 00071 } 00072 timestop(caption); 00073 if (close(fd) == -1) 00074 syserr("close"); 00075 } 00076 00077 exit(EXIT_SUCCESS); 00078 }