root/user/uw-testbin/vm-mix1-fork/vm-mix1-fork.c

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

DEFINITIONS

This source file includes following definitions.
  1. write_data
  2. read_data
  3. print_data
  4. do_work
  5. main

   1 #include <stdio.h>
   2 #include <stdlib.h>
   3 #include <string.h>
   4 #include <unistd.h>
   5 
   6 /* This is creating a program that has
   7  * a few more text pages than the average
   8  * program we usually have.
   9  */
  10 
  11 // #define DEBUG
  12 // #define DEBUG_PARENT
  13 // #define DEBUG_CHILD
  14 
  15 extern void     call_all();
  16 void write_data(unsigned int array[], unsigned int start);
  17 void read_data(unsigned int array[], unsigned int start, const char *array_name);
  18 void print_data(unsigned int array[]);
  19 void do_work(unsigned int start);
  20 
  21 #define PAGE_SIZE           (4096)
  22 #define DATA_BYTES          (3 * 1024 * 1024)
  23 #define PAGES               (DATA_BYTES / PAGE_SIZE)
  24 #define ELEM_SIZE           (sizeof(unsigned int))
  25 #define ELEMS               ((PAGE_SIZE * PAGES / sizeof(unsigned int)) / 2)
  26 #define ELEMS_PER_PAGE      (PAGE_SIZE / ELEM_SIZE)
  27 #define NUM_REFS            (2)
  28 
  29 #define STACK_PAGES_USED    (9)
  30 #define STACK_ARRAY_ELEMS   (PAGE_SIZE * STACK_PAGES_USED / sizeof(unsigned int))
  31 
  32 unsigned int init[] = {
  33   0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
  34  10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  35  20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  36  30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  37  40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  38  50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  39 };
  40 
  41 #define INIT_ARRAY_ELEMS     (sizeof(init) / sizeof(int))
  42 
  43 unsigned int array1[ELEMS];
  44 unsigned int array2[ELEMS];
  45 
  46 void
  47 write_data(unsigned int array[], unsigned int start)
  48 {
  49         unsigned int ref = 0;
  50         unsigned int i = 0;
  51 
  52         for (ref = 0; ref < NUM_REFS-1; ref++) {
  53                 for (i=0; i<ELEMS; i++) {
  54       array[i] = start + (i / ELEMS_PER_PAGE);
  55                 }
  56         }
  57 }
  58 
  59 void
  60 read_data(unsigned int array[], unsigned int start, const char *array_name)
  61 {
  62   unsigned int ref = 0;
  63         unsigned int i = 0;
  64 
  65         for (ref = 0; ref < NUM_REFS-1; ref++) {
  66                 for (i=0; i<ELEMS; i++) {
  67                         if (array[i] != (start + (i / ELEMS_PER_PAGE))) {
  68                                 printf("FAILED in file %s at line %d %s[%d] = %u != %u ref = %d\n", 
  69                 __FILE__, __LINE__, array_name, i, array[i], i, ref+1);
  70                                 exit(1);
  71                         }
  72                 }
  73   }
  74 }
  75 
  76 void
  77 print_data(unsigned int array[])
  78 {
  79         unsigned int i = 0;
  80   unsigned int count = 0;
  81 
  82         for (i=0; i<ELEMS; i+= (ELEMS_PER_PAGE)) {
  83                 printf("[%10u] = %10u ", i, array[i]);
  84     if (((count+1) % 4) == 0) {
  85       printf("\n");
  86     }
  87     count++;
  88         }
  89   printf("\n");
  90 }
  91 
  92 void
  93 do_work(unsigned int start)
  94 {
  95         unsigned int stack_array[STACK_ARRAY_ELEMS];
  96         unsigned int i = 0;
  97   unsigned int array1_start = start;
  98   unsigned int array2_start = start + (ELEMS / (ELEMS_PER_PAGE)) + 10;
  99 
 100   printf("Checking uninitialized array1 pid = %d\n", getpid());
 101         /* check the uninitialized array1 before initialization */
 102         for (i=0; i<ELEMS; i++) {
 103                 if (array1[i] != 0) {
 104                         printf("FAILED in file %s at line %d: array1[%d] = %u != %d\n", __FILE__, __LINE__, i, array1[i], 0);
 105                         exit(1);
 106                 }
 107         }
 108 
 109   printf("Checking uninitialized array2 pid = %d\n", getpid());
 110         /* check the uninitialized array2 before initialization */
 111         for (i=0; i<ELEMS; i++) {
 112                 if (array2[i] != 0) {
 113                         printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
 114                         exit(1);
 115                 }
 116         }
 117 
 118         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
 119                 stack_array[i] = i * 1000;
 120         }
 121 
 122         for (i=0; i<2; i++) {
 123           call_all();
 124           write_data(array1, array1_start);
 125           call_all();
 126     printf("Checking initialized array1 pid = %d\n", getpid());
 127           read_data(array1, array1_start, "array1");
 128         }
 129 
 130         /* check the uninitialized array2 again before initialization */
 131   printf("Checking initialized array2 again pid = %d\n", getpid());
 132         for (i=0; i<ELEMS; i++) {
 133                 if (array2[i] != 0) {
 134                         printf("FAILED in file %s at line %d: array2[%d] = %u != %d\n", __FILE__, __LINE__, i, array2[i], 0);
 135                         exit(1);
 136                 }
 137         }
 138 
 139   printf("Checking initialized stack_array pid = %d\n", getpid());
 140         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
 141                 if (stack_array[i] != i * 1000) {
 142                         printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
 143                         exit(1);
 144                 }
 145         }
 146 
 147   printf("Checking initialized init pid = %d\n", getpid());
 148         /* check the initialized array */
 149         for (i=0; i<INIT_ARRAY_ELEMS; i++) {
 150                 if (init[i] != i) {
 151                         printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
 152                         exit(1);
 153                 }
 154         }
 155 
 156         for (i=0; i<2; i++) {
 157           call_all();
 158           write_data(array2, array2_start);
 159           call_all();
 160     printf("Checking initialized array2 pid = %d\n", getpid());
 161           read_data(array2, array2_start, "array2");
 162         }
 163 
 164   printf("Checking initialized stack_array pid = %d\n", getpid());
 165         for (i=0; i<STACK_ARRAY_ELEMS; i++) {
 166                 if (stack_array[i] != i * 1000) {
 167                         printf("FAILED in file %s at line %d: stack_array[%d] = %u != %d\n", __FILE__, __LINE__, i, stack_array[i], i);
 168                         exit(1);
 169                 }
 170         }
 171 
 172         /* check the initialized array */
 173   printf("Checking initialized init pid = %d\n", getpid());
 174         for (i=0; i<INIT_ARRAY_ELEMS; i++) {
 175                 if (init[i] != i) {
 176                         printf("FAILED in file %s at line %d: init[%d] = %u != %d\n", __FILE__, __LINE__, i, init[i], i);
 177                         exit(1);
 178                 }
 179         }
 180 
 181 
 182   printf("Checking initialized array1 for the last time pid = %d\n", getpid());
 183         read_data(array1, array1_start, "array1");
 184   printf("Checking initialized array2 for the last time pid = %d\n", getpid());
 185         read_data(array2, array2_start, "array2");
 186 
 187 
 188         printf("Pid = %d SUCCEEDED\n", getpid());
 189 }
 190 
 191 int
 192 main()
 193 {
 194   int pid = 0;
 195   int rc = 0;
 196   int status = 0;
 197 
 198 #ifdef DEBUG
 199   printf("PAGE_SIZE = %d\n", PAGE_SIZE);
 200   printf("DATA_BYTES = %d\n", DATA_BYTES);
 201   printf("ELEMS = %d\n", ELEMS);
 202   printf("ELEMS_PER_PAGE = %d\n", ELEMS_PER_PAGE);
 203   printf("PAGES = %d\n", PAGES);
 204   printf("Array elements = %d\n", ELEMS);
 205   printf("Pages per array = %d\n", ((ELEMS * sizeof(unsigned int)) / PAGE_SIZE));
 206 #endif
 207 
 208   pid = fork();
 209   if (pid < 0) {
 210     printf("Unable to fork\n");
 211     exit(1);
 212   }
 213 
 214   if (pid == 0) {
 215                 printf("Child pid = %d calling do_work\n", getpid());
 216                 do_work(20);
 217 #ifdef DEBUG_CHILD
 218                 printf("array 1\n");
 219                 print_data(array1);
 220                 printf("array 2\n");
 221                 print_data(array2);
 222 #endif
 223     exit(0);
 224   }
 225 
 226   printf("Parent pid = %d calling do_work\n", getpid());
 227   do_work(1);
 228 #ifdef DEBUG_PARENT
 229         printf("array 1\n");
 230         print_data(array1);
 231         printf("array 2\n");
 232         print_data(array2);
 233 #endif
 234   rc = waitpid(pid, &status, 0);
 235   exit(0);
 236 }
 237 

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