I am reading Operating System Concept (9th edition) and at page 443 I have found interesting considerations about demand paging and how this can affect a process timing. Here is what it says:
Assume that pages are 128 words in size. Consider a C program whose function is to initialize to 0 each element of a 128-by-128 array. The following code is typical:
int i, j; int data[128][128]; for (j = 0; j < 128; j++) for (i = 0; i < 128; i++) data[i][j] = 0;
For pages of 128 words, each row takes one page. Thus, the preceding code zeros one word in each page, then another word in each page, and so on. If the operating system allocates fewer than 128 frames to the entire program, then its execution will result in 128 × 128 = 16,384 page faults. In contrast, suppose we change the code to
int i, j; int data[128][128]; for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i][j] = 0;
This code zeros all the words on one page before starting the next page, reducing the number of page faults to 128.
So I tried to do this on my OS, just to register the difference in time between the 2 approaches. Let's start with saying that I am working on Windows 10 and the page size I have set for this test is 16 MB, which is also the minimum allowed in my case. Unfortunately, the first piece of code is problematic:
int main(){
const int PAGE_SIZE = 16; //MB
const int ROW_SIZE = 127;
const int COL_SIZE = 1024 / sizeof(int) * 1024 * PAGE_SIZE;
int table[ROW_SIZE][COL_SIZE];
printf("test");
return 0;
}
It compiles(gcc
) and it does not throw any exception even at runtime, but it does not print test
. I thought it was really weird because it is supposed to print something at least. So I tried to debug it and I have found out that it does not even reach the main
, but it is received signal SIGSEGV, Segmentation fault.
I am pretty sure that this behaviour is bound to the reduction of the page size, but I can't explain myself why.
The error code returned is -1073741571
, but I am not an expert on windows and at first I thought that error parsing was similar (my readings kind of confirm that) to Linux's so I took the 16 less significant bits, but the parsed error code is 253
and it does not correspond to any valid error code.