00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <unistd.h>
00009
00010
00011
00012
00013
00014 #define PROCS (4)
00015 #define BUF_SIZE (10)
00016 #define NUM_WRITES (500)
00017 #define TOTAL_WRITES (NUM_WRITES * PROCS)
00018
00019 char const *to_write = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
00020 static void do_writes(char c);
00021
00022 int
00023 main()
00024 {
00025 char buffer[BUF_SIZE];
00026 int status = -1;
00027 int rc[PROCS];
00028 pid_t pid[PROCS];
00029 int rval = -1;
00030 int id = -1;
00031 int i,k;
00032
00033 rval = open("TESTFILE", O_RDWR | O_CREAT | O_TRUNC);
00034
00035 if (rval < 0)
00036 {
00037 printf("### TEST FAILED: Unable to create file\n");
00038 exit(0);
00039 }
00040 close(rval);
00041
00042
00043 for (i=0; i<PROCS; i++) {
00044 pid[i] = fork();
00045 if (pid[i] < 0) {
00046 printf("Fork %d failed\n", i);
00047 exit(0);
00048 }
00049
00050
00051 if (pid[i] == 0) {
00052 do_writes(to_write[i]);
00053 printf("Process number %d is done\n", i);
00054 exit(0);
00055 }
00056 }
00057
00058
00059 for (i=0; i<PROCS; i++) {
00060 if (pid[i] < 0) {
00061 printf("### TEST FAILED: Unable to create processes\n");
00062 }
00063 }
00064
00065
00066 for (i=0; i<PROCS; i++) {
00067 rc[i] = waitpid(pid[i], &status, 0);
00068 if (rc[i] != pid[i]) {
00069 printf("### TEST FAILED: wait for processes failed\n");
00070 }
00071 printf("Done waiting for process number %d\n", i);
00072 }
00073
00074
00075 id = open("TESTFILE", O_RDWR);
00076 if (id < 0)
00077 {
00078 printf("### TEST FAILED: Unable to open file\n");
00079 exit(1);
00080 }
00081
00082
00083 for (i=0; i<TOTAL_WRITES; i++)
00084 {
00085 rval = read(id, buffer, BUF_SIZE);
00086
00087 for (k=0; k<(BUF_SIZE-1); k++) {
00088 if (buffer[k] != buffer[k+1]) {
00089 printf("### TEST FAILED; Writes were not atomic\n");
00090 printf("buffer[%d] = %c != buffer[%d] = %c\n",
00091 k, buffer[k], k+1, buffer[k+1]);
00092 close(id);
00093 exit(1);
00094 }
00095 }
00096 }
00097
00098 rval = close(id);
00099
00100 if (rval < 0)
00101 {
00102 printf("### TEST FAILED: Unable to close file\n");
00103 } else {
00104 printf("PASSED\n");
00105 }
00106 exit(0);
00107
00108 }
00109
00110 void
00111 do_writes(char c)
00112 {
00113 int rval = 0;
00114 int id = -1;
00115 int i = 0;
00116 int j = 0;
00117 char buffer[BUF_SIZE];
00118 int volatile total = 0;
00119
00120 for (j=0; j<BUF_SIZE; j++) {
00121 buffer[j] = c;
00122 }
00123
00124 id = open("TESTFILE", O_RDWR);
00125
00126 if (id < 0) {
00127 printf("### TEST FAILED: Unable to open file\n");
00128 _exit(1);
00129 }
00130
00131 for (i = 0; i < NUM_WRITES; i++) {
00132 rval = write(id, buffer, BUF_SIZE);
00133
00134 if (rval != BUF_SIZE) {
00135 printf("### TEST FAILED: Unable to write %d bytes to file\n", BUF_SIZE);
00136 close(id);
00137 _exit(1);
00138 }
00139
00140
00141 for (j=0; j<BUF_SIZE; j++) {
00142 buffer[j] = c;
00143 total += j;
00144 }
00145 }
00146
00147 rval = close(id);
00148
00149 if (rval < 0) {
00150 printf("### TEST FAILED: Unable to close file\n");
00151 _exit(1);
00152 }
00153
00154 }