0

I'm working on a PintOS Project(VM).

For Validation of what I implemented, I made a user program on which global array size of ten pages(4096 * 10). And I write a value on every first byte of the page.

What I intended was that Pintos occurs 10 page faults, but it happended only two times, and every values were stored well in only one page.

I thought ten virtual pages would be allocated for global array buf, but actually only one is allocated.

Can anybody tell me why my expectation was wrong? I think I have a wrong idea on how global arrays are stored in memory.


#define SIZE 10 * 4096
volatile char buf[SIZE] = {0}; 

int main (int argc, char**argv){
    int cnt = 0; 
    for(int i = 0; i < SIZE; i += 4096)
    {
        printf("%p", &buf[i]); 
        buf[i] = 'a' ;
    }

    for(int i = 0; i < SIZE; i += 4096)
    {
        printf("%d %c\n", cnt ++, buf[i]); 
    }

The Following output is as below.

hello from page fault.
hello from page fault.
0x804bce0
0x804cce0
0x804dce0
0x804ece0
0x804fce0
0x8050ce0
0x8051ce0
0x8052ce0
0x8053ce0
0x8054ce0
1 a
2 a
3 a
4 a
5 a
6 a
7 a
8 a
9 a

As the result shows, each &buf[i] requires different page(their indexes are 0x804b, 0x804c, ...)

Th

  • You say 10, the code says `100`, that's confusing. I'm not familiar with your platform, but wouldn't expect it to de-duplicate memory pages, that sounds far out and not like a realistic thing for an OS to be doing. Did you get the printouts? You should technically cast to `(void *)` since that is the type for `%p` by the way. – unwind Dec 02 '22 at 08:22
  • Also, make `buf` be `volatile` and inspect the generated code so you know that the writes really happen. This code is "edgy" since it depends on side-effects that are outside the compiler's world, stuff can get interesting. – unwind Dec 02 '22 at 08:29
  • Considering the little output the program generates, I would recommend you copy-paste (as text, please) the actual output into the question as well. – Some programmer dude Dec 02 '22 at 08:30
  • @Someprogrammerdude programmer dude I edited the code and represented the output. – JungHwanPark Dec 02 '22 at 09:06
  • "I thought ten virtual pages would be allocated for global array buf, but **actually only one is allocated.**" How do you check that? – Support Ukraine Dec 02 '22 at 09:07
  • 2
    If one page is used, then it's often likely that the next page will be used as well. So a single page-fault may map multiple pages to avoid too many page-faults. Have you read the page-fault handler to see what it really does? – Some programmer dude Dec 02 '22 at 09:08
  • @Someprogrammerdude You were right!! A single page-fault map multiple pages! Thank you. – JungHwanPark Dec 02 '22 at 10:42

0 Answers0