root/user/uw-testbin/conc-io/conc-io.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main
  2. do_writes

   1 
   2 /* Tim Brecht: 
   3  * Added to uw-testbin : Sat  5 Jan 2013 14:36:26 EST
   4  */
   5 
   6 #include <stdio.h>
   7 #include <stdlib.h>
   8 #include <unistd.h>
   9  
  10 /*
  11  * This tests concurrent access to a file. 
  12  */ 
  13 
  14 #define PROCS         (4)        
  15 #define BUF_SIZE      (10)
  16 #define NUM_WRITES    (500)
  17 #define TOTAL_WRITES  (NUM_WRITES * PROCS)
  18 
  19 char const *to_write = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  20 static void do_writes(char c);
  21 
  22 int 
  23 main()
  24 {
  25   char buffer[BUF_SIZE];
  26         int status = -1;
  27         int rc[PROCS];
  28   pid_t pid[PROCS];
  29   int rval = -1;
  30   int id = -1;
  31   int i,k;
  32 
  33   rval = open("TESTFILE", O_RDWR | O_CREAT | O_TRUNC);
  34 
  35   if (rval < 0)
  36   {
  37     printf("### TEST FAILED: Unable to create file\n"); 
  38     exit(0);
  39   }
  40         close(rval);
  41 
  42   /* do concurrent writes */ 
  43   for (i=0; i<PROCS; i++) {
  44     pid[i] = fork();
  45           if (pid[i] < 0) {
  46       printf("Fork %d failed\n", i); 
  47       exit(0);
  48           }
  49 
  50     /* This is the child have it do something */
  51     if (pid[i] == 0) {
  52       do_writes(to_write[i]);
  53       printf("Process number %d is done\n", i);
  54                   exit(0);
  55     }
  56   }
  57 
  58   /* Check that all processes were created */
  59   for (i=0; i<PROCS; i++) {
  60      if (pid[i] < 0) {
  61        printf("### TEST FAILED: Unable to create processes\n");
  62      }
  63   }
  64 
  65   /* Wait for all processes to finish */
  66   for (i=0; i<PROCS; i++) {
  67              rc[i] = waitpid(pid[i], &status, 0);
  68        if (rc[i] != pid[i]) {
  69          printf("### TEST FAILED: wait for processes failed\n");
  70        }
  71        printf("Done waiting for process number %d\n", i);
  72   }
  73 
  74   /* Now check that the file contains the proper contents */
  75   id = open("TESTFILE", O_RDWR);
  76   if (id < 0)
  77   {
  78     printf("### TEST FAILED: Unable to open file\n");
  79     exit(1);
  80   }
  81 
  82   /* verify writes were atomic */ 
  83   for (i=0; i<TOTAL_WRITES; i++)
  84   {
  85     rval = read(id, buffer, BUF_SIZE);
  86 
  87     for (k=0; k<(BUF_SIZE-1); k++) {
  88         if (buffer[k] != buffer[k+1]) { 
  89                 printf("### TEST FAILED; Writes were not atomic\n"); 
  90                                         printf("buffer[%d] = %c != buffer[%d] = %c\n", 
  91                                                         k, buffer[k], k+1, buffer[k+1]);
  92                                         close(id);
  93           exit(1); 
  94               }
  95     }
  96   }
  97 
  98   rval = close(id); 
  99 
 100   if (rval < 0)
 101   {
 102     printf("### TEST FAILED: Unable to close file\n"); 
 103   } else {
 104     printf("PASSED\n");
 105         }
 106   exit(0); 
 107 
 108 }
 109 
 110 void
 111 do_writes(char c)
 112 {
 113   int rval = 0;
 114   int id = -1;
 115   int i = 0; 
 116   int j = 0;
 117         char buffer[BUF_SIZE];
 118   int volatile total = 0;
 119 
 120   for (j=0; j<BUF_SIZE; j++) {
 121                 buffer[j] = c;
 122   }
 123 
 124   id = open("TESTFILE", O_RDWR);
 125   
 126   if (id < 0) {
 127     printf("### TEST FAILED: Unable to open file\n");
 128     _exit(1); 
 129   }
 130 
 131   for (i = 0; i < NUM_WRITES; i++) {
 132     rval = write(id, buffer, BUF_SIZE);
 133 
 134     if (rval != BUF_SIZE) {
 135       printf("### TEST FAILED: Unable to write %d bytes to file\n", BUF_SIZE);
 136                         close(id);
 137       _exit(1); 
 138     }
 139 
 140     /* Do something else to try to give other processes a change to run */
 141     for (j=0; j<BUF_SIZE; j++) {
 142                   buffer[j] = c;
 143       total += j;
 144     }
 145   }
 146 
 147   rval = close(id); 
 148 
 149   if (rval < 0) {
 150     printf("### TEST FAILED: Unable to close file\n"); 
 151     _exit(1); 
 152   }
 153 
 154 }

/* [<][>][^][v][top][bottom][index][help] */