os161-1.99
 All Data Structures
sparse.c
00001 /* 
00002  * sparse.c
00003  *
00004  *      This program declares a large array, but only uses a small
00005  *      part of it.  The intention is that the full array will not
00006  *      fit into memory, but the used part of the array will fit.
00007  *
00008  *      This is similar to testbin/huge.
00009  *
00010  */
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 
00015 /* 
00016  * set these to match the page size of the 
00017  * machine and the number of pages of physical memory
00018  * in the machine.
00019  */
00020 #define PageSize        4096
00021 #define NumPages        128
00022 
00023 /* a large array, 2 times the size of physical memory */
00024 #define ArraySize (2*NumPages*PageSize)
00025 char sparse[ArraySize];
00026 
00027 int
00028 main()
00029 {
00030         int i,j;
00031 
00032         printf("Starting the sparse program\n");
00033 
00034         /* write some values into the array, but only
00035            touch one array entry on every 10th page */
00036         for (i=0; i<ArraySize; i+=(10*PageSize)) {
00037           sparse[i]= 'a';
00038         }
00039         
00040         printf("stage [1] done\n");
00041         
00042         /* increment each location 5 times */
00043         for(j=0; j<5; j++) {
00044           for (i=0; i<ArraySize; i+=(10*PageSize)) {
00045             sparse[i] += 1;
00046           }
00047         }
00048         
00049         printf("stage [2] done\n");
00050         
00051         /* check the values that were written */
00052         /* increment each location 5 times */
00053         for (i=0; i<ArraySize; i+=(10*PageSize)) {
00054           if (sparse[i] != ('a'+5)) {
00055             printf("Test failed! Unexpected value at array position %d\n", i);
00056             return(1);
00057           }
00058         }
00059 
00060         printf("SUCCESS\n");
00061         
00062         return 0;
00063 }
00064 
 All Data Structures